Sunday, 21 June 2009

Converting gzips to png and maybe cropping

The following routine was used to batch convert all \verb".gz" files in a
directory to .png. The format of the file inside the zipfile doesn't matter
as the conversion takes place automatically.

The routine is in \verb"ms.py"


import Image, gzip, os, StringIO

def unzip_file(filename):
fileObj = gzip.GzipFile(filename, 'rb');
fileContent = fileObj.read()
fileObj.close()
return(fileContent)

def gz2png():
# get the directory listing
files = os.listdir('.')

for i in files:
# import the zipfile
if '.gz' in i:
print i,
data = unzip_file(i)
# Convert this data into an image object.
im = Image.open(StringIO.StringIO(data))

# Use pil to convert to .png and save it for external compilation.
f2 = i.split('.')[0] + '.png'
im.save(f2)
print ' -> ', f2



To use, perform \verb">>> import imageconv; imageconv.convert()" in the
directory containing the files. A similar routine for converting to gif
exists by the name \verb"gz2gif". This routine is now in \verb"ms.py".

No comments:

Post a Comment