This commit is contained in:
hugovk 2014-07-30 19:39:11 +03:00
parent 4c5a5c1f0e
commit 515bb6e14d

View File

@ -21,7 +21,8 @@ __version__ = "0.3"
from PIL import Image, ImageFile, _binary from PIL import Image, ImageFile, _binary
import os, tempfile import os
import tempfile
i8 = _binary.i8 i8 = _binary.i8
i16 = _binary.i16be i16 = _binary.i16be
@ -35,17 +36,20 @@ COMPRESSION = {
PAD = o8(0) * 4 PAD = o8(0) * 4
# #
# Helpers # Helpers
def i(c): def i(c):
return i32((PAD + c)[-4:]) return i32((PAD + c)[-4:])
def dump(c): def dump(c):
for i in c: for i in c:
print("%02x" % i8(i), end=' ') print("%02x" % i8(i), end=' ')
print() print()
## ##
# Image plugin for IPTC/NAA datastreams. To read IPTC/NAA fields # Image plugin for IPTC/NAA datastreams. To read IPTC/NAA fields
# from TIFF and JPEG files, use the <b>getiptcinfo</b> function. # from TIFF and JPEG files, use the <b>getiptcinfo</b> function.
@ -112,7 +116,7 @@ class IptcImageFile(ImageFile.ImageFile):
while True: while True:
offset = self.fp.tell() offset = self.fp.tell()
tag, size = self.field() tag, size = self.field()
if not tag or tag == (8,10): if not tag or tag == (8, 10):
break break
if size: if size:
tagdata = self.fp.read(size) tagdata = self.fp.read(size)
@ -129,10 +133,10 @@ class IptcImageFile(ImageFile.ImageFile):
# print tag, self.info[tag] # print tag, self.info[tag]
# mode # mode
layers = i8(self.info[(3,60)][0]) layers = i8(self.info[(3, 60)][0])
component = i8(self.info[(3,60)][1]) component = i8(self.info[(3, 60)][1])
if (3,65) in self.info: if (3, 65) in self.info:
id = i8(self.info[(3,65)][0])-1 id = i8(self.info[(3, 65)][0])-1
else: else:
id = 0 id = 0
if layers == 1 and not component: if layers == 1 and not component:
@ -143,16 +147,16 @@ class IptcImageFile(ImageFile.ImageFile):
self.mode = "CMYK"[id] self.mode = "CMYK"[id]
# size # size
self.size = self.getint((3,20)), self.getint((3,30)) self.size = self.getint((3, 20)), self.getint((3, 30))
# compression # compression
try: try:
compression = COMPRESSION[self.getint((3,120))] compression = COMPRESSION[self.getint((3, 120))]
except KeyError: except KeyError:
raise IOError("Unknown IPTC image compression") raise IOError("Unknown IPTC image compression")
# tile # tile
if tag == (8,10): if tag == (8, 10):
if compression == "raw" and self._is_raw(offset, self.size): if compression == "raw" and self._is_raw(offset, self.size):
self.tile = [(compression, (offset, size + 5, -1), self.tile = [(compression, (offset, size + 5, -1),
(0, 0, self.size[0], self.size[1]))] (0, 0, self.size[0], self.size[1]))]
@ -200,14 +204,17 @@ class IptcImageFile(ImageFile.ImageFile):
im.load() im.load()
self.im = im.im self.im = im.im
finally: finally:
try: os.unlink(outfile) try:
except: pass os.unlink(outfile)
except:
pass
Image.register_open("IPTC", IptcImageFile) Image.register_open("IPTC", IptcImageFile)
Image.register_extension("IPTC", ".iim") Image.register_extension("IPTC", ".iim")
## ##
# Get IPTC information from TIFF, JPEG, or IPTC file. # Get IPTC information from TIFF, JPEG, or IPTC file.
# #