How to convert Imagemagick's -morphology Erode Square into Rmagick? - imagemagick

I have tried the following code in imagemagick:
convert input.jpg -morphology Erode Square output.jpg
I need to convert it into RMagick so that I can use in rails application

You can always write it using the << operator:
MiniMagick::Tool::Convert.new do |convert|
convert << 'input.jpg'
convert << '-morphology' << 'Erode' << 'Square'
convert << 'output.jpg'
end

Related

imagemagick convert -crop with pipe before it

I want to use imagemagick to crop an image.
However, imagemagick will be receiving the image data to crop through a pipe before it:
exiftool -b -RawThermalImage image1.jpg | convert .....-crop.....
I am not sure what to put in the ".....-crop.....", to crop the image data received to a specific area. Please guide.
Usually, after convert, an image is specified for cropping like:
convert rose: -crop 1x1+3+3 cropped.gif
But, in this case, I am confused as to how to complete this command given that the image is coming in from the pipe.
ImageLink:
https://drive.google.com/file/d/14h3z0yFK_9_f2puzbhLUm3D50MDMMnu2/view?usp=sharing
Updated Answer
It transpires that the problem was caused by inadvertently using GraphicsMagick rather than ImageMagick.
Original Answer
You should be able to use a dash - to refer to the stdin if that stream has a well-known magic number (signature) at the start:
exiftool -b -RawThermalImage image1.jpg | convert - -crop ... result.jpg
If the stream is raw, or doesn't have a known magic number/signature, you will need to give ImageMagick a hint, so if it is raw greyscale 8-bit data with shape 640x480, use:
exiftool -b -RawThermalImage image1.jpg | convert -size 640x480 -depth 8 GRAY:- -crop ... result.jpg
If it's RGB888 data with size 80x80, use:
exiftool -b -RawThermalImage image1.jpg | convert -depth 8 -size 80x80 RGB:- -crop ... result.jpg

Applying ImageMagick operators in a specific sequence

I have two images that are 2048x1638 which I want to append together in the following manner:
save(append(crop(image1.jpg, geometry), image2.jpg), image3.jpg)
that is, I want to perform this sequence of operations:
crop image1.jpg to geometry
append the result of the crop to image2.jpg
save the result of the append to image3.jpg
Is there a way to do this?
I've looked at the usage guide and tried
convert image1.jpg -crop 2048x819+0+105 -append image2.jpg image3.jpg
but it doesn't do the crop, and if I rearrange the order
convert -append -crop 2048x819+0+105 image1.jpg image2.jpg image3.jpg
then it crops both images and then appends them.
Haha, I was almost there; this works:
convert image1.jpg -crop 2048x819+0+105 image2.jpg -append image3.jpg
and it looks like parentheses (escaped at shell) can localize operators if needed:
convert \( image1.jpg -crop 2048x819+0+105 \) image2.jpg -append image3.jpg

ImageMagic resize over background image

I need to resize images in PaperClip which uses ImageMagick over a background image. I made custom PaperClip processors (https://gist.github.com/xxx/1051938 and https://github.com/thoughtbot/paperclip#custom-attachment-processors) and they are working but I dont know which commands to use to compose the images.
This is process I would like:
User uploads image X
Processor runs and resizes the image X to given dimensions with tranparent fill - Y
Next the background image is cropped to given dimensions - Z
Y is overlayed over the Z centered.
Thanks!
Well after few days of struggle the answer is here:
For IM command line version step by step (a.png is input, tiled.png is source of background)
convert a.png -thumbnail 100x100 -background none -gravity center -extent 100x100 resized.png
convert tiled.png -crop 100x100+0+0 +repage cropped.png
convert cropped.png out.png -composite result.png
For IM command line combined code
convert tiled.png -crop 1000x1000+0+0 +repage a.png -thumbnail 1000x1000 -background none -gravity center -extent 1000x1000 -composite result.png
For use in image magick copy the default Thumbnail processor and make changes to the make method (or just create your own processor)
def make
src = #file
dst = Tempfile.new([#basename, #format ? ".#{#format}" : ''])
dst.binmode
begin
tgeom = #target_geometry.width.to_s + 'x' + #target_geometry.height.to_s
tiled_path = Rails.root.join('public','system','tiled.png')
parameters = []
parameters << tiled_path
parameters << '-crop'
parameters << tgeom + '+0+0 +repage'
parameters << ':source'
parameters << '-thumbnail ' + tgeom
parameters << '-background none'
parameters << '-gravity center'
parameters << '-extent ' + tgeom
parameters << '-composite'
parameters << ':dest'
parameters = parameters.flatten.compact.join(' ').strip.squeeze(' ')
success = convert(parameters, :source => "#{File.expand_path(src.path)}#{'[0]' unless animated?}", :dest => File.expand_path(dst.path))
rescue Cocaine::ExitStatusError => e
raise Paperclip::Error, "There was an error processing the thumbnail for #{#basename}" if #whiny
rescue Cocaine::CommandNotFoundError => e
raise Paperclip::Errors::CommandNotFoundError.new("Could not run the `convert` command. Please install ImageMagick.")
end
dst
end

MiniMagick - add texture to image

Given:
A texture and a photograph (with no transparent background)
How do I give the photograph the texture?
To be specific, I want the following steps:
Tile the texture. From here, I have: convert -size 1056x576 tile:Castillo_001.gif Castillo_tiled.gif
Invert the result.
Do a composite with the photograph equivalent to the "Color Dodge" blending mode in Photoshop.
Any clues for MiniMagick? Or any clue for the answer in ImageMagick?
Thanks a bunch.
Answer
ImageMagick:
convert IMG_1970.JPG \( -size 2816x1584 tile:background.png -negate \) -compose ColorDodge -composite out.jpg
Full answer:
# I just negatived the background already
img = MiniMagick::Image.open(File.join('IMG_1970.JPG'))
background = MiniMagick::Image.open(File.join('background_negative.png'))
background.combine_options do |c|
c.size "#{img['width']}x#{img['height']}"
c.tile background.path
end
img = img.composite(background) do |c|
c.compose "ColorDodge"
end
You are correct in creating the texture image with the tile: format. Simply apply -negate option to invert the result. After which, a simple compose & composite command to apply the "Color Dodge" effect to any given image. See the Compose Examples article.
convert \( -size 1056x576 tile:Castillo_001.gif -negate \) \
source_image.jpg -compose Lighten -composite out_image.jpg

Imagemagick Mogrify crop output name

When using mogrify -crop 500x500 *.jpg
I get output as *-1.jpg, *-2.jpg, *-3.jpg, *-4.jpg
Is it possible to change so that it gets a different separator. eg. underscore?
output: *_1.jpg, *_2.jpg, *_3.jpg, *_4.jpg,
You can do this with convert:
convert *.jpg -crop 500x500 -set filename:f "%t_%p" '%[filename:f].jpg'
See this page for more info.

Resources