Skip to content

Commit

Permalink
For PNG files, geo_mage.tags will be saved a PNG metadata
Browse files Browse the repository at this point in the history
  • Loading branch information
loerum committed Oct 8, 2014
1 parent bb87828 commit d269c64
Showing 1 changed file with 32 additions and 2 deletions.
34 changes: 32 additions & 2 deletions mpop/imageo/image.py
Original file line number Diff line number Diff line change
Expand Up @@ -501,7 +501,38 @@ def pil_save(self, filename, compression=6, fformat=None):
fformat = fformat or os.path.splitext(filename)[1][1:4]
fformat = check_image_format(fformat)

self.pil_image().save(filename, fformat)
params = {}

if fformat == 'png':
# Take care of GeoImage.tags.
params['pnginfo'] = self._pngmeta()

self.pil_image().save(filename, fformat, **params)

def _pngmeta(self):
"""It will return GeoImage.tags as a PNG metadata object.
Inspired by:
public domain, Nick Galbreath
http://blog.modp.com/2007/08/python-pil-and-png-metadata-take-2.html
"""
reserved = ('interlace', 'gamma', 'dpi', 'transparency', 'aspect')

try:
tags = self.tags
except AttributeError:
tags = {}

# Undocumented class
from PIL import PngImagePlugin
meta = PngImagePlugin.PngInfo()

# Copy from tags to new dict
for k__, v__ in tags.items():
if k__ not in reserved:
meta.add_text(k__, v__, 0)

return meta

def putalpha(self, alpha):
"""Adds an *alpha* channel to the current image, or replaces it with
Expand Down Expand Up @@ -1180,4 +1211,3 @@ def rgb2ycbcr(r__, g__, b__):
cr_ = 1. / (2 * (1 - kr_)) * (r__ - y__)

return y__, cb_, cr_

0 comments on commit d269c64

Please sign in to comment.