Batch convert images to another format
Ran into a situation where I required to convert all the images used in my paper to .eps format. I was dreading having to do this manually using Gimp, when I realized that I could easily script Imagemagick to do this for me. I came up with the following piece of code that does the conversion for me. It is very quick and dirty, but it works – all figures in formats jpg, jp2, png, pdf in the directory in which the script is run get converted to Eps. Of course, you might be interested in some other format – feel free to take the code and modify accordingly.
Script to convert from various image formats to eps
#!/usr/bin/env python
# Author: Arvind Pereira
# Date : Oct 24, 2013
import os, re, sys
dirList = os.listdir( '.' )
try:
os.mkdir( 'EpsFigs' )
except:
pass
for f in dirList:
m = re.match('([\w\-]+).(|jpg|jp2|png|pdf|)$',f)
if m:
cmd = 'convert %s EpsFigs/%s.eps'%( f, m.group(1) )
os.system( cmd )This requires ImageMagick and Python to work.