Matlab figures have, by default, a white space border around them. This is fine if you want to view them by themselves, but wastes space if you want to add them to a text document, where the white borders are "added" to the document margins. It is possible to adjust the border width in Matlab, but if you already have exported the figures to an image file, it's too late. (Note: When exporting to EPS the border is not included by default).
If you want to remove this white space for a large number of image files in one go, ImageMagick is your friend. For example,
mogrify -trim +repage *.png
will remove the white border of all PNG files in the current directory, using the "smart crop" trim option. Consult the ImageMagick documentation on cropping for further options.
Showing posts with label imagemagick. Show all posts
Showing posts with label imagemagick. Show all posts
Monday, 12 November 2012
Monday, 20 February 2012
Batch converting images files from one format to another using ImageMagick
Let's say you have 500 JPG images and want to convert them all to PNG in one go. The mogrify function in the ImageMagick software suite comes in handy here:
mogrify -format png *.jpg
This produces a copy of each image in the new format. It's possible to combine the conversion with other operations, for example resizing and conversion to monochrome:
mogrify -resize 50% -monochrome -format png *.jpg
I often create PDF vector graphics which I use together with PDFLaTeX. However, sometimes I need bitmap versions of the vector graphics images. Mogrify can convert from PDF to bitmap formats (for example PNG), but it's important to set the density of the sampling grid in the conversion (dots per inch), otherwise the bitmap image might have a too low resolution. Example:
mogrify -format png -density 600 *.pdf
The commands above can be executed directly in the terminal if you are inside the folder containing the images. The folder can also be specified explicitly, for example
mogrify -format png /home/martin/images/*.jpg
mogrify -format png *.jpg
This produces a copy of each image in the new format. It's possible to combine the conversion with other operations, for example resizing and conversion to monochrome:
mogrify -resize 50% -monochrome -format png *.jpg
I often create PDF vector graphics which I use together with PDFLaTeX. However, sometimes I need bitmap versions of the vector graphics images. Mogrify can convert from PDF to bitmap formats (for example PNG), but it's important to set the density of the sampling grid in the conversion (dots per inch), otherwise the bitmap image might have a too low resolution. Example:
mogrify -format png -density 600 *.pdf
The commands above can be executed directly in the terminal if you are inside the folder containing the images. The folder can also be specified explicitly, for example
mogrify -format png /home/martin/images/*.jpg
Sunday, 11 December 2011
Changing the bit depth of images using ImageMagick
Today I wanted to create a simple black-and-white version of a grayscale image that I have. This is pretty easily done in Matlab (you can scale the image to values between 0 and 1 and use round()), but I was guessing there was probably already some simple way to do it in ImageMagick - and there is!
This is the original version of the image (from the great graphical novel series "Bone"):
This is the original version of the image (from the great graphical novel series "Bone"):
To convert it to a 3-bit image (8 levels), I used the -depth switch with the convert command:
convert bone.jpg -colorspace gray -depth 3 bone3bit.jpg
And similarly, for black-and-white (1 bit image):
convert bone.jpg -colorspace gray -depth 1 bone1bit.jpg
Wednesday, 5 October 2011
Converting images from PDF to PNG using ImageMagick
I usually prefer vector formats like PDF for my figures, but some times I need to make a PNG version. ImageMagick is my tool of choice to do this, since it can be called from the command line. My initial try was something like this:
convert figure.pdf figure.png
which indeed converts the figure from PDF to PNG, but with a much too low resolution in the PNG image. After reading up on the subject on the Magick-users mailing list and the ImageMagick documentation, I found out that the default resolution at which the PDF file is read is 72 DPI. To read with a higher resolution, for example 600 DPI, use the -density option:
convert figure.pdf figure.png
which indeed converts the figure from PDF to PNG, but with a much too low resolution in the PNG image. After reading up on the subject on the Magick-users mailing list and the ImageMagick documentation, I found out that the default resolution at which the PDF file is read is 72 DPI. To read with a higher resolution, for example 600 DPI, use the -density option:
convert -density 600 figure.pdf figure.png
As a little side comment, I could mention that I first tried to do this because when I saved a figure from Inkscape as PNG, the resolution was very poor. The workaround was then to save as PDF and convert to PNG. However, afterwards I remembered that Inkscape also has a "export bitmap" option, which will allow you to set the density before saving to PNG. Oh well, the ImageMagick conversion may still come in useful some day...
Thursday, 15 September 2011
Using ImageMagick to convert images to grayscale
Although color images are nice, I try to avoid using them in articles, as that always costs extra. Sometimes I also prefer grayscale images for presentations, since the colors of the projector can often be quite different from what I see on my screen.
Anyway, here is a simple way to convert an image from color to grayscale using the command line tool ImageMagick:
convert inputFile -colorspace Gray outputFile
Anyway, here is a simple way to convert an image from color to grayscale using the command line tool ImageMagick:
convert inputFile -colorspace Gray outputFile
Thursday, 1 September 2011
Cropping an image from the command line (/Matlab)
I recently had to crop some figures which had a lot of unnecessary white space on the sides. The figures were made in Matlab, and I wanted to find some way to do the cropping from the command line, to avoid manually editing each image.
As always when it comes to image editing, ImageMagick provides a solution, as explained here. The "convert" command is used to crop images with the following syntax:
convert -crop (width)x(height)+(horzOffset)+(vertOffset) infile outfile
where (horzOffset) and (vertOffset) denote the pixel coordinates of the upper left corner of the cropping window (sideways, down). The parentheses are not included in the syntax. Example:
convert -crop 1800x1597+700+0 im1.png imCropped.png
crops im1.png to size 1800x1597, with 700 horizontal offset and 0 pixels vertical offset. To do the same from Matlab, simply use the "system" command:
> system('convert -crop 1800x1597+700+0 im1.png imCropped.png')
Subscribe to:
Posts (Atom)



