I want to create a padded thumbnail, like described here
This command works:
convert src.png -thumbnail '200x200>' -gravity center -extent '200x200' dst.png
But this ruby code is not working: gravity is ignored
require 'mini_magick'
image = MiniMagick::Image.open('src.png')
image.thumbnail '200x200>'
image.gravity 'center'
image.extent '200x200'
image.write 'dst.png'
What's wrong with this code?
You need to use combine_options with MiniMagick to roll all three of your commands together before you write it:
require 'mini_magick'
image = MiniMagick::Image.open('src.png')
image.combine_options do |c|
c.thumbnail '200x200>'
c.gravity 'center'
c.extent '200x200'
end
image.write 'dst.png'
More info on the GitHub docs
Related
So I have this base image:
And in photoshop I do a basic layer color overlay, with the rgb colors:
r: 244, g: 93, b: 0
This gives me the amazingly vibrant:
What I'm trying to do is colorize the same image in rmagick, so if I do the following colorize:
img = Magick::Image.read('brush.png').first
img = img.colorize(100, 100, 100, Magick::Pixel.new(244, 93, 0, 1))
img.format = 'png'
img.to_blob
It gives me this really washed out orange image:
My questions is, how do I colorize this image with those rgb params in imagemagick / rmagick, to get the same vibrant color that I got in photoshop.
Thanks.
At the commandline, I think you want something like this:
convert brush.png \( +clone -fill "rgb(244,93,0)" -colorize 100% \) -compose colorize -composite out.png
So, with the +clone I am creating another layer the same size as your image and entirely filling it 100% with your orange colour and then composing it over your image with the -composite to blend the opacity and colour.
I really don't speak Ruby, but I think it will be along these lines:
#!/usr/bin/ruby
require 'RMagick'
include Magick
infile=ARGV[0]
img=Magick::Image.read(infile).first
w=img.columns
h=img.rows
ovl=Image.new(w,h){self.background_color=Magick::Pixel.new(244*256,93*256,0)}
img.composite!(ovl,0,0,Magick::ColorizeCompositeOp)
img.write('result.png')
Mark Setchell's command line works for me (Windows), with slight modifications...
convert greyscale.png +clone -fill "rgb(244,93,0)" -colorize 100% -compose colorize -composite colour.png
Found this link on recolouring with rmagick...
ftp://belagro.com/Redmine/ruby/lib/ruby/gems/1.8/gems/rmagick-2.12.0/doc/colorize.rb.html
Based on the code in the above link, with the greyscale conversion removed, does the example below work (I don't have ruby)?
# load the greyscale image
img = Magick::Image.read('greyscale.png').first
# Colorize with a 100% blend of the orange color
colorized = img.colorize(1, 1, 1, '#A50026')
# save the colour image
colorized.write('colour.png')
Used a colour picker to get the hex of your orange colour - rgb(244,93,0) = #A50026
I am trying to make a partially opaque png using MiniMagick, but I guess what it really boils down to is the syntax for using '-evaluate'
This works in the terminal:
convert input.jpg -alpha on -channel a -evaluate set 25% output.png
But I don't quite understand how to turn it into minimagick code
This is (the latest permutation of) what I'm trying:
require 'mini_magick'
img = MiniMagick::Image.open('input.jpg')
img.combine_options do |mogrify|
mogrify.alpha 'on'
mogrify.channel 'a'
mogrify.evaluate 'set', '25%'
puts mogrify.inspect
end
img.write('output.png')
The inspect output shows that #args is #args=["-alpha", "\"on\"", "-channel", "\"a\"", "-evaluate", "\"set\"", "\"25%\""]
No error messages, but all I get is an identical copy of input.jpg
You are pretty much all the way there except that you also need to let minimagick know that you are outputting in PNG format using:
img.format('png')
Try this instead:
require 'mini_magick'
img = MiniMagick::Image.open('input.jpg')
img.format('png')
img.combine_options do |mogrify|
mogrify.alpha 'on'
mogrify.channel 'a'
mogrify.evaluate 'set', '25%'
end
img.write('output.png')
Good day.
How to impose white_rectangle.jpg on logo.jpg in the image below
using Imagemagic.
And a bonus question: what's Ruby's method can make the task.
def (path_to_image)
# impose white_rectangle.jpg on logo
end
This can easily be accomplished using RMagick:
require 'RMagick'
logo = Magick::Image.read("logo.jpg").first
rect = Magick::Image.read("white_rectangle.jpg").first
result = logo.composite(rect, x, y, Magick::CopyCompositeOp)
result.write "result.jpg"
An alternative is to just draw a white rectangle without using a composite image:
image = Magick::Image.read("logo.jpg").first
gc = Magick::Draw.new
gc.stroke = 'white'
gc.fill = 'white'
gc.rectangle x_start, y_start, x_end, y_end
gc.draw(image)
image.write "result.jpg"
Using ImageMagick command line tools, you can overlay one image with another like this:
$ composite white_rectangle.jpg logo.jpg -geometry +x+y result.jpg
I want to cut a circle out of an image using rmagick.
Here's an example of what I'd like to be able to accomplish:
-->
It seems like I want to use http://studio.imagemagick.org/RMagick/doc/draw.html#circle to cut a circle, and then clip_path to mask it, but the docs aren't very clear. Would anyone be able to point me in the right direction?
require 'rmagick'
im = Magick::Image.read('walter.jpg').first
circle = Magick::Image.new 200, 200
gc = Magick::Draw.new
gc.fill 'black'
gc.circle 100, 100, 100, 1
gc.draw circle
mask = circle.blur_image(0,1).negate
mask.matte = false
im.matte = true
im.composite!(mask, Magick::CenterGravity, Magick::CopyOpacityCompositeOp)
im.write 'walter_circle.png'
This is how I would do it with Imagemagick and php:
// Canvas the same size as the final image
exec("convert -size 800x533 xc:white white.jpg");
// The mask
exec("convert -size 800x533 xc:none -draw \"fill black circle 400,265 400,50\" write_mask.png");
// Cut the whole out of the canvas
exec("composite -compose Dst_Out write_mask.png white.jpg -matte step.png");
// Put the canvas over the image and trim off excess white background
exec("convert IMG_5745.jpg step.png -composite -trim final.jpg");
You should be able to follow the process?
Cleanup tempory images afterwards - I tend to save the tempory images in a .miff format and then write a loop to delete all .miff images afterwards. Alternativly just leave them and if you use the same name for the tempory images they will be overwritten every time the code is run.
Is it possible to somehow do the following in rails?
Get the color of a specific pixel from a image (for example at location 10px by 10px)
Delete all occurrences of that color from the image (gets replaced with transparent pixels)
Crop image to exclude any outer transparent pixels
Any advise would be greatly appreciated!
Rails? No.
Ruby? Yes.
Check out RMagick: http://rmagick.rubyforge.org/
Here's the code of how I did it using RMagick (thanks Alex Wayne for pointing me to RMagick)...
require 'RMagick'
img = Magick::Image.read("sample.jpg").first
bgcolor = img.pixel_color(1,1)
img.format = "PNG"
img.fuzz = 0.05
img.trim!
img.resize_to_fit!(100, 40)
bg = Magick::Image.new(100,40) { self.background_color = bgcolor }
img = bg.composite(img, Magick::CenterGravity, Magick::OverCompositeOp)
img.write("modified.png")
For my initial requirement (to just remove the outer colors or blank borders), the following is all you need:
require 'RMagick'
img = Magick::Image.read("sample.jpg").first
img.trim!
img.write("sample.jpg")