Skip to content

Instantly share code, notes, and snippets.

@MohamedAlaa
Last active April 13, 2024 21:53
Show Gist options
  • Save MohamedAlaa/d9b54cd856b3edce1510 to your computer and use it in GitHub Desktop.
Save MohamedAlaa/d9b54cd856b3edce1510 to your computer and use it in GitHub Desktop.
ImageMagick Snippets

Remove white background color of an image in ImageMagick

$ convert  your.jpg  -transparent white  your.png

Flatten a transparent image with a white background:

convert -flatten img1.png img1-white.png

Make an image transparent

convert -transparent '#FFFFFF' nontransparent.gif transparent.png

convert an image into tiles

convert -size 3200x3200 tile:single_tile.png final.png

making a montage from a collection of images

montage -geometry +0+0 -background transparent *png montage.png

inverting colors

convert before.png -negate after.png

generating a favicon

convert large_image.png -resize 16x16! favicon.ico

Resize keep aspect ratio

convert input.jpg -resize 250x90 output.jpg

Resize keep minimum image size

convert input.jpg -resize '250x90^' output.jpg

Clone

This code reads in the image, copys it with +clone, resizes and saves it. Then resizes and saves the next size. The part of the code within the ( ) has no effect on any of the other part of the code.

convert input.jpg \( +clone -resize 600x600 -write first_save.jpg +delete \) -resize 60x60 thumbnail.jpg

Speed up jpg resize

Defining the size of the jpg when loading it will speed up the resizing process. This is done by adding the -size option.

convert -define jpeg:size=250x90 input.jpg -resize 250x90 output.jpg

adding numbers to a tiled image

I also once needed to add numbers to a individual parts of a tiled image. I found it easier to script this with Ruby rather than use the shell's looping constructs:

cmd = (0..324).to_a.inject([]) do |cmd,n| 
  y=(n/25*32)+15; x=((n%25)*32)+15
  cmd << "-draw 'fill red text #{x},#{y} \"#{n}\"'"
end

convert img.png #{cmd.join(' ')} annotated_img.png
@kakatoji
Copy link

good sir

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment