Useful terminal commands to convert image types
When you work with images regularly, it’s often helpful to convert them to other picture formats. You may open and save files in a variety of formats using graphical tools like the GIMP. If you deal with a lot of pictures, however, this might be time-consuming. Image editing tools such as ImageMagick and Inkscape provide a command-line interface to image conversion.
rsvg-convert
On a Mac, I found rsvg-convert
to be a simple and effective tool.
It can be found in brew
with brew install librsvg
and is used like so:
# rsvg-convert
rsvg-convert input.svg > output.png
rsvg-convert -h 32 icon.svg > icon-32.png
ImageMagick
With ImageMagick convert,
you can change any ImageMagick-supported image
format to any other ImageMagick-supported image format. The complete list of
supported formats is rather long—you may consult the ImageMagick man page for a
full list, these include BMP
, CMYK
, GIF
, JPEG
, PBM
, PNG
, RGB
,
SVG
, TIFF
, and XPM
.
# ImageMagick
convert my_image.bmp my_image.png
Another advantage of utilizing a command-line image conversion tool is that it is very easy to script. Consider the following command, which converts an entire directory of BMP files to JPEG:
# ImageMagick
for i in *.bmp; do j=`echo $i | sed -e 's/\.bmp/\.jpg/'`; \
convert $i $j; done;
There are a few other cool features like Flipping, adding boarders and animating images. You can see more about it from ImageMagick’s docuementation
Inkscape
Inkscape is another great tool for authoring and edit graphics. Inkscape uses the standardized SVG file format, and so with its command line, we can also convert images from SVG really well
Using the --export-type=TYPE[,TYPE]*
flag we can specify the file type to
export. Possible formats include: svg
, png
,ps
, eps
, pdf
, emf
, wmf
and every file type for which an export extension exists. It is possible to
export more than one file type at a time.
Note that PostScript does not support transparency, so any transparent objects in the original SVG will be automatically rasterized.
# Inkscape
inkscape --export-filename=my_image.png -w 1449 -h 2050 my_image.svg
See Inkscape’s command line documentation and wiki page to learn about its capabilities