diff --git a/PIL/EpsImagePlugin.py b/PIL/EpsImagePlugin.py index e6842e91e..044d089bf 100644 --- a/PIL/EpsImagePlugin.py +++ b/PIL/EpsImagePlugin.py @@ -227,53 +227,56 @@ class EpsImageFile(ImageFile.ImageFile): # # Load EPS header - s = fp.readline().strip('\r\n') + s_raw = fp.readline() + s = s_raw.strip('\r\n') - while s: - if len(s) > 255: - raise SyntaxError("not an EPS file") + while s_raw: + if s: + if len(s) > 255: + raise SyntaxError("not an EPS file") - try: - m = split.match(s) - except re.error as v: - raise SyntaxError("not an EPS file") + try: + m = split.match(s) + except re.error as v: + raise SyntaxError("not an EPS file") - if m: - k, v = m.group(1, 2) - self.info[k] = v - if k == "BoundingBox": - try: - # Note: The DSC spec says that BoundingBox - # fields should be integers, but some drivers - # put floating point values there anyway. - box = [int(float(i)) for i in v.split()] - self.size = box[2] - box[0], box[3] - box[1] - self.tile = [("eps", (0, 0) + self.size, offset, - (length, box))] - except: - pass - - else: - m = field.match(s) if m: - k = m.group(1) + k, v = m.group(1, 2) + self.info[k] = v + if k == "BoundingBox": + try: + # Note: The DSC spec says that BoundingBox + # fields should be integers, but some drivers + # put floating point values there anyway. + box = [int(float(i)) for i in v.split()] + self.size = box[2] - box[0], box[3] - box[1] + self.tile = [("eps", (0, 0) + self.size, offset, + (length, box))] + except: + pass - if k == "EndComments": - break - if k[:8] == "PS-Adobe": - self.info[k[:8]] = k[9:] - else: - self.info[k] = "" - elif s[0] == '%': - # handle non-DSC Postscript comments that some - # tools mistakenly put in the Comments section - pass else: - raise IOError("bad EPS header") + m = field.match(s) + if m: + k = m.group(1) - s = fp.readline().strip('\r\n') + if k == "EndComments": + break + if k[:8] == "PS-Adobe": + self.info[k[:8]] = k[9:] + else: + self.info[k] = "" + elif s[0] == '%': + # handle non-DSC Postscript comments that some + # tools mistakenly put in the Comments section + pass + else: + raise IOError("bad EPS header") - if s[:1] != "%": + s_raw = fp.readline() + s = s_raw.strip('\r\n') + + if s and s[:1] != "%": break # diff --git a/Tests/images/zero_bb_emptyline.eps b/Tests/images/zero_bb_emptyline.eps new file mode 100644 index 000000000..4f4bb7103 Binary files /dev/null and b/Tests/images/zero_bb_emptyline.eps differ diff --git a/Tests/test_file_eps.py b/Tests/test_file_eps.py index 70930bf95..2313b292c 100644 --- a/Tests/test_file_eps.py +++ b/Tests/test_file_eps.py @@ -278,6 +278,16 @@ class TestFileEps(PillowTestCase): # Assert self.assertEqual(img.mode, "RGB") + def test_emptyline(self): + # Test file includes an empty line in the header data + emptyline_file = "Tests/images/zero_bb_emptyline.eps" + + image = Image.open(emptyline_file) + image.load() + self.assertEqual(image.mode, "RGB") + self.assertEqual(image.size, (460, 352)) + self.assertEqual(image.format, "EPS") + if __name__ == '__main__': unittest.main()