mercoledì 8 luglio 2009

Some code for writing some metadata within a png with PIL

Some functions I've written adapting some code of Nick Galbreath from http://mail.python.org/pipermail/image-sig/2007-August/004575.html .

#convert an image to png format
def convert_to_png(imagename, destPath):

filename = os.path.abspath(destPath)+'\\'+(os.path.basename(imagename)[0:-4])+".png"

if(os.path.basename(imagename)[-3:]!="png"):

cmmd = "convert "+imagename+" "+filename
os.system(cmmd)

return filename


#save the metadata dictionary to be loaded within the sourceimage into a new name file
def pngsave(sourceImage, metadataDict, filename):

from PIL import PngImagePlugin
#reserved = sourceImage.info

meta = PngImagePlugin.PngInfo()


# copy from Image.info to new dict
for k,v in metadataDict.iteritems():

#if k in reserved: continue
meta.add_text(k, str(v), 0)

# and save
sourceImage.save(filename, "PNG", pnginfo=meta)

I should write some fixing for jpg metadata.

Nessun commento:

Posta un commento