home | codereading | contact | github | math | misc | notes | patches | tech | README

ImageMagick notes

ImageMagick is a a set of wonderful program that manipulates images in batch. The tool I most use from ImageMagick is convert. I personally recommend the Usage page for lots of examples.

Below are some one line commands on how to use it to solve common problems.


Crop ("cut part of") an image

$ convert -crop 1100x760+150+40 input.png output.png

Pay attention to the geometry part. The syntax of the command is:

$ convert -crop {width}x{height}+{x}+{y} input_file output_file

Scale an image

$ convert -scale 50% input.png output.png

Remove EXIF content from an image

$ convert input.png -strip output.png

This is specially useful to take off information that editors don't recognize and can lead to confusing, like the rotation of the camera.

I thank this link to this tip.

Trim space around an image

There is the -trim option to remove all pixels that are the same colour of the border pixels:

$ convert -trim input.png output.png

You might also want to leave a border, like a white one:

$ convert -trim -border 10%x10% -borderColor White input.png output.png

This and this links helped me to solve this problem.

Convert PDF to JPG and preserve quality

Try the -density option. If you don't use it, the result image can have lower quality, even if you use -scale or -resize. Example:

$ convert -density 200 input.pdf output.jpg

This page helped me.

Important: if I understood correctly, the -density parameter refers to the density of the file passed as the next argument. Because of that, it needs to come before the PDF filename.

Change brightness

$ convert -modulate <number> input.jpg output.jpg

<number> is the brightness level. 100 is the normal image level so, if you want it to be more bright, chose a number that is greater than 100. 0 will make the image black.

More information in Color Modifications ImageMagick page.

Croping pages of a PDF document

Maybe (for reasons we'll not explain) you have a scanned PDF document whose canves is bigger than the document part. It might be a good idea to trim that but you don't have Adobe Acrobat or other proprietary tool. Let's use ImageMagick!

Something like that may work:

$ convert -density 300 -crop 3610x2790+1340+0 +repage input.pdf output.pdf

Note that the +repage operator comes later.

Unfortunatelly it might be that the resulting PDF is too big (somebody know why? If so, please, let me know), so it might be a good idea to convert them to images first and then converting them back to PDF:

$ convert -density 300 -crop 3610x2790+1340+0 input.pdf output.png

You might have to check the other of generated files because, because, if it generates 11 files, those will be:

$ ls output-*.png

Realize that the shell sorts output-10.png just after output-1.png, which can give you sort problems. Rename file correctly and convert them back to PDF:

$ convert +repage output-*.png output.pdf

See that the +repage operator is important. See in this thread more about that.

Converting XCF (Gimp) files to PNG and merging all layers

Just use the -flatten option like in this page:

$ convert -flatten input.xcf output.png

Converting a blurred photo of text to a decent black and white image

If you took a photography of a document but, when you saw it, it was blurred, and you want to convert it to a black and white image (like if you have used a professional scanner), you can use the -lat option of ImageMagick, that performs adaptive threshold, based on local pixels:

$ convert input.jpg -colorspace gray -lat "15x15-5%" output.jpg

It is perfect for photographies of documents, since every letter must be compared with its surrounds, only. Adjust the parameters to -lat accordingly.

See Fred's ImageMagick Scripts THRESHOLD COMPARISON STUDY for a great comparison between threshold algorithms.

Now, your photo can be blurred. In that case, you can use -sharpen option:

$ convert input.jpg  -sharpen 0x4 -colorspace gray -lat "15x15-5%" output.jpg

Also, take a look at Bluring and Sharpening page of ImageMagick.