openmedialibrary_platform/Darwin/lib/python3.5/site-packages/PIL/IptcImagePlugin.py

268 lines
6.8 KiB
Python
Raw Normal View History

2013-10-11 17:28:32 +00:00
#
# The Python Imaging Library.
# $Id$
#
# IPTC/NAA file handling
#
# history:
# 1995-10-01 fl Created
# 1998-03-09 fl Cleaned up and added to PIL
# 2002-06-18 fl Added getiptcinfo helper
#
# Copyright (c) Secret Labs AB 1997-2002.
# Copyright (c) Fredrik Lundh 1995.
#
# See the README file for information on usage and redistribution.
#
2014-09-30 16:15:32 +00:00
from __future__ import print_function
2013-10-11 17:28:32 +00:00
2014-09-30 16:15:32 +00:00
from PIL import Image, ImageFile, _binary
2015-11-25 01:25:01 +00:00
import os
import tempfile
__version__ = "0.3"
2013-10-11 17:28:32 +00:00
2014-09-30 16:15:32 +00:00
i8 = _binary.i8
i16 = _binary.i16be
i32 = _binary.i32be
o8 = _binary.o8
2013-10-11 17:28:32 +00:00
COMPRESSION = {
1: "raw",
5: "jpeg"
}
2014-09-30 16:15:32 +00:00
PAD = o8(0) * 4
2013-10-11 17:28:32 +00:00
2015-11-25 01:25:01 +00:00
2013-10-11 17:28:32 +00:00
#
# Helpers
def i(c):
return i32((PAD + c)[-4:])
2015-11-25 01:25:01 +00:00
2013-10-11 17:28:32 +00:00
def dump(c):
for i in c:
2014-09-30 16:15:32 +00:00
print("%02x" % i8(i), end=' ')
print()
2013-10-11 17:28:32 +00:00
2015-11-25 01:25:01 +00:00
2013-10-11 17:28:32 +00:00
##
# Image plugin for IPTC/NAA datastreams. To read IPTC/NAA fields
# from TIFF and JPEG files, use the <b>getiptcinfo</b> function.
class IptcImageFile(ImageFile.ImageFile):
format = "IPTC"
format_description = "IPTC/NAA"
def getint(self, key):
return i(self.info[key])
def field(self):
#
# get a IPTC field header
s = self.fp.read(5)
if not len(s):
return None, 0
2014-09-30 16:15:32 +00:00
tag = i8(s[1]), i8(s[2])
2013-10-11 17:28:32 +00:00
# syntax
2014-09-30 16:15:32 +00:00
if i8(s[0]) != 0x1C or tag[0] < 1 or tag[0] > 9:
raise SyntaxError("invalid IPTC/NAA file")
2013-10-11 17:28:32 +00:00
# field size
2014-09-30 16:15:32 +00:00
size = i8(s[3])
2013-10-11 17:28:32 +00:00
if size > 132:
2014-09-30 16:15:32 +00:00
raise IOError("illegal field length in IPTC/NAA file")
2013-10-11 17:28:32 +00:00
elif size == 128:
size = 0
elif size > 128:
size = i(self.fp.read(size-128))
else:
size = i16(s[3:])
return tag, size
def _open(self):
# load descriptive fields
2014-09-30 16:15:32 +00:00
while True:
2013-10-11 17:28:32 +00:00
offset = self.fp.tell()
tag, size = self.field()
2015-11-25 01:25:01 +00:00
if not tag or tag == (8, 10):
2013-10-11 17:28:32 +00:00
break
if size:
tagdata = self.fp.read(size)
else:
tagdata = None
2014-09-30 16:15:32 +00:00
if tag in list(self.info.keys()):
2013-10-11 17:28:32 +00:00
if isinstance(self.info[tag], list):
self.info[tag].append(tagdata)
else:
self.info[tag] = [self.info[tag], tagdata]
else:
self.info[tag] = tagdata
# print tag, self.info[tag]
# mode
2015-11-25 01:25:01 +00:00
layers = i8(self.info[(3, 60)][0])
component = i8(self.info[(3, 60)][1])
if (3, 65) in self.info:
id = i8(self.info[(3, 65)][0])-1
2013-10-11 17:28:32 +00:00
else:
id = 0
if layers == 1 and not component:
self.mode = "L"
elif layers == 3 and component:
self.mode = "RGB"[id]
elif layers == 4 and component:
self.mode = "CMYK"[id]
# size
2015-11-25 01:25:01 +00:00
self.size = self.getint((3, 20)), self.getint((3, 30))
2013-10-11 17:28:32 +00:00
# compression
try:
2015-11-25 01:25:01 +00:00
compression = COMPRESSION[self.getint((3, 120))]
2013-10-11 17:28:32 +00:00
except KeyError:
2014-09-30 16:15:32 +00:00
raise IOError("Unknown IPTC image compression")
2013-10-11 17:28:32 +00:00
# tile
2015-11-25 01:25:01 +00:00
if tag == (8, 10):
self.tile = [("iptc", (compression, offset),
(0, 0, self.size[0], self.size[1]))]
2013-10-11 17:28:32 +00:00
def load(self):
if len(self.tile) != 1 or self.tile[0][0] != "iptc":
return ImageFile.ImageFile.load(self)
type, tile, box = self.tile[0]
encoding, offset = tile
self.fp.seek(offset)
# Copy image data to temporary file
2014-09-30 16:15:32 +00:00
o_fd, outfile = tempfile.mkstemp(text=False)
o = os.fdopen(o_fd)
2013-10-11 17:28:32 +00:00
if encoding == "raw":
# To simplify access to the extracted file,
# prepend a PPM header
o.write("P5\n%d %d\n255\n" % self.size)
2014-09-30 16:15:32 +00:00
while True:
2013-10-11 17:28:32 +00:00
type, size = self.field()
if type != (8, 10):
break
while size > 0:
s = self.fp.read(min(size, 8192))
if not s:
break
o.write(s)
2014-09-30 16:15:32 +00:00
size -= len(s)
2013-10-11 17:28:32 +00:00
o.close()
try:
try:
# fast
self.im = Image.core.open_ppm(outfile)
except:
# slightly slower
im = Image.open(outfile)
im.load()
self.im = im.im
finally:
2015-11-25 01:25:01 +00:00
try:
os.unlink(outfile)
except:
pass
2013-10-11 17:28:32 +00:00
2015-11-25 01:25:01 +00:00
Image.register_open(IptcImageFile.format, IptcImageFile)
2013-10-11 17:28:32 +00:00
2015-11-25 01:25:01 +00:00
Image.register_extension(IptcImageFile.format, ".iim")
2013-10-11 17:28:32 +00:00
##
# Get IPTC information from TIFF, JPEG, or IPTC file.
#
# @param im An image containing IPTC data.
# @return A dictionary containing IPTC information, or None if
# no IPTC information block was found.
def getiptcinfo(im):
2014-09-30 16:15:32 +00:00
from PIL import TiffImagePlugin, JpegImagePlugin
import io
2013-10-11 17:28:32 +00:00
data = None
if isinstance(im, IptcImageFile):
# return info dictionary right away
return im.info
elif isinstance(im, JpegImagePlugin.JpegImageFile):
# extract the IPTC/NAA resource
try:
app = im.app["APP13"]
2015-11-25 01:25:01 +00:00
if app[:14] == b"Photoshop 3.0\x00":
2013-10-11 17:28:32 +00:00
app = app[14:]
# parse the image resource block
offset = 0
2015-11-25 01:25:01 +00:00
while app[offset:offset+4] == b"8BIM":
2014-09-30 16:15:32 +00:00
offset += 4
2013-10-11 17:28:32 +00:00
# resource code
2015-11-25 01:25:01 +00:00
code = i16(app, offset)
2014-09-30 16:15:32 +00:00
offset += 2
2013-10-11 17:28:32 +00:00
# resource name (usually empty)
2014-09-30 16:15:32 +00:00
name_len = i8(app[offset])
2015-11-25 01:25:01 +00:00
# name = app[offset+1:offset+1+name_len]
2013-10-11 17:28:32 +00:00
offset = 1 + offset + name_len
if offset & 1:
2014-09-30 16:15:32 +00:00
offset += 1
2013-10-11 17:28:32 +00:00
# resource data block
2015-11-25 01:25:01 +00:00
size = i32(app, offset)
2014-09-30 16:15:32 +00:00
offset += 4
2013-10-11 17:28:32 +00:00
if code == 0x0404:
# 0x0404 contains IPTC/NAA data
data = app[offset:offset+size]
break
offset = offset + size
if offset & 1:
2014-09-30 16:15:32 +00:00
offset += 1
2013-10-11 17:28:32 +00:00
except (AttributeError, KeyError):
pass
elif isinstance(im, TiffImagePlugin.TiffImageFile):
# get raw data from the IPTC/NAA tag (PhotoShop tags the data
# as 4-byte integers, so we cannot use the get method...)
try:
2014-09-30 16:15:32 +00:00
data = im.tag.tagdata[TiffImagePlugin.IPTC_NAA_CHUNK]
2013-10-11 17:28:32 +00:00
except (AttributeError, KeyError):
pass
if data is None:
2015-11-25 01:25:01 +00:00
return None # no properties
2013-10-11 17:28:32 +00:00
# create an IptcImagePlugin object without initializing it
2015-11-25 01:25:01 +00:00
class FakeImage(object):
2013-10-11 17:28:32 +00:00
pass
im = FakeImage()
im.__class__ = IptcImageFile
# parse the IPTC information chunk
im.info = {}
2014-09-30 16:15:32 +00:00
im.fp = io.BytesIO(data)
2013-10-11 17:28:32 +00:00
try:
im._open()
except (IndexError, KeyError):
2015-11-25 01:25:01 +00:00
pass # expected failure
2013-10-11 17:28:32 +00:00
return im.info