image - Reading a JPEG in Python (PIL) with broken header -
i'm trying open jpeg file in python 2.7,
from pil import image im = image.open(filename)
which didn't work me,
>>> im = image.open(filename) traceback (most recent call last): file "<pyshell#810>", line 1, in <module> im = image.open(filename) file "/usr/lib/python2.7/dist-packages/pil/image.py", line 1980, in open raise ioerror("cannot identify image file") ioerror: cannot identify image file
though when trying out on external viewers, opened fine. digging in bit, turns out jpegimagefile._open
method pil
's jpegimageplugin.py
file raises syntaxerror
exception due several extraneous 0x00
bytes before 0xffda
marker in jpeg's file header,
corrupt jpeg data: 5 extraneous bytes before marker 0xda
that is, while other programs tried ignored unknown 0x00
marker towards end of header, pil
prefered raise exception, not allowing me open image.
question: apart editing pil
's code directly, there workaround opening jpegs problematic headers?
the relevant code jpegimagefile
class raises exception appears below, convenience:
def _open(self): s = self.fp.read(1) if ord(s[0]) != 255: raise syntaxerror("not jpeg file") # create attributes self.bits = self.layers = 0 # jpeg specifics (internal) self.layer = [] self.huffman_dc = {} self.huffman_ac = {} self.quantization = {} self.app = {} # compatibility self.applist = [] self.icclist = [] while 1: s = s + self.fp.read(1) = i16(s) if marker.has_key(i): name, description, handler = marker[i] # print hex(i), name, description if handler not none: handler(self, i) if == 0xffda: # start of scan rawmode = self.mode if self.mode == "cmyk": rawmode = "cmyk;i" # assume adobe conventions self.tile = [("jpeg", (0,0) + self.size, 0, (rawmode, ""))] # self.__offset = self.fp.tell() break s = self.fp.read(1) elif == 0 or == 65535: # padded marker or junk; move on s = "\xff" else: raise syntaxerror("no marker found")
pil doesn't corrupt data in header , falls on you've discovered.
i've made pull request pillow (the friendly pil fork) should fix problem.
it's not yet been accepted, it'll there version 2.5.0 due out in couple of months. in meantime, can try out here: https://github.com/python-imaging/pillow/pull/647
as workaround, use imagemagick first convert problematic images png, , use them in pil/pillow.
Comments
Post a Comment