From 1bd4919a359e87e82a92dc02c1852e6c3d6df036 Mon Sep 17 00:00:00 2001 From: Alexey Buzanov Date: Tue, 19 Aug 2014 17:53:51 +0400 Subject: [PATCH 1/3] Fix tga files with image ID field --- PIL/TgaImagePlugin.py | 13 +++++++------ 1 file changed, 7 insertions(+), 6 deletions(-) diff --git a/PIL/TgaImagePlugin.py b/PIL/TgaImagePlugin.py index 55790db08..049fb2dbb 100644 --- a/PIL/TgaImagePlugin.py +++ b/PIL/TgaImagePlugin.py @@ -19,6 +19,7 @@ __version__ = "0.3" +import os from PIL import Image, ImageFile, ImagePalette, _binary @@ -42,9 +43,6 @@ MODES = { } -def _accept(prefix): - return prefix[0:1] == b"\0" - ## # Image plugin for Targa files. @@ -58,7 +56,7 @@ class TgaImageFile(ImageFile.ImageFile): # process header s = self.fp.read(18) - id = i8(s[0]) + idlen = i8(s[0]) colormaptype = i8(s[1]) imagetype = i8(s[2]) @@ -70,7 +68,7 @@ class TgaImageFile(ImageFile.ImageFile): self.size = i16(s[12:]), i16(s[14:]) # validate header fields - if id != 0 or colormaptype not in (0, 1) or\ + if colormaptype not in (0, 1) or\ self.size[0] <= 0 or self.size[1] <= 0 or\ depth not in (1, 8, 16, 24, 32): raise SyntaxError("not a TGA file") @@ -103,6 +101,9 @@ class TgaImageFile(ImageFile.ImageFile): if imagetype & 8: self.info["compression"] = "tga_rle" + if idlen: + self.fp.seek(idlen, os.SEEK_CUR) + if colormaptype: # read palette start, size, mapdepth = i16(s[3:]), i16(s[5:]), i16(s[7:]) @@ -191,7 +192,7 @@ def _save(im, fp, filename, check=0): # -------------------------------------------------------------------- # Registry -Image.register_open("TGA", TgaImageFile, _accept) +Image.register_open("TGA", TgaImageFile) Image.register_save("TGA", _save) Image.register_extension("TGA", ".tga") From f94b6b4025e3ed7ab46841fd3f253d5244ff7162 Mon Sep 17 00:00:00 2001 From: Alexey Buzanov Date: Wed, 20 Aug 2014 10:32:06 +0400 Subject: [PATCH 2/3] flake8 and tests --- PIL/TgaImagePlugin.py | 25 +++++++++++++------------ Tests/images/tga_id_field.tga | Bin 0 -> 30050 bytes Tests/test_file_tga.py | 22 ++++++++++++++++++++++ 3 files changed, 35 insertions(+), 12 deletions(-) create mode 100644 Tests/images/tga_id_field.tga create mode 100644 Tests/test_file_tga.py diff --git a/PIL/TgaImagePlugin.py b/PIL/TgaImagePlugin.py index 049fb2dbb..46eafe8d0 100644 --- a/PIL/TgaImagePlugin.py +++ b/PIL/TgaImagePlugin.py @@ -19,7 +19,6 @@ __version__ = "0.3" -import os from PIL import Image, ImageFile, ImagePalette, _binary @@ -77,7 +76,7 @@ class TgaImageFile(ImageFile.ImageFile): if imagetype in (3, 11): self.mode = "L" if depth == 1: - self.mode = "1" # ??? + self.mode = "1" # ??? elif imagetype in (1, 9): self.mode = "P" elif imagetype in (2, 10): @@ -102,24 +101,24 @@ class TgaImageFile(ImageFile.ImageFile): self.info["compression"] = "tga_rle" if idlen: - self.fp.seek(idlen, os.SEEK_CUR) + self.info["id_section"] = self.fp.read(idlen) if colormaptype: # read palette start, size, mapdepth = i16(s[3:]), i16(s[5:]), i16(s[7:]) if mapdepth == 16: - self.palette = ImagePalette.raw("BGR;16", - b"\0"*2*start + self.fp.read(2*size)) + self.palette = ImagePalette.raw( + "BGR;16", b"\0"*2*start + self.fp.read(2*size)) elif mapdepth == 24: - self.palette = ImagePalette.raw("BGR", - b"\0"*3*start + self.fp.read(3*size)) + self.palette = ImagePalette.raw( + "BGR", b"\0"*3*start + self.fp.read(3*size)) elif mapdepth == 32: - self.palette = ImagePalette.raw("BGRA", - b"\0"*4*start + self.fp.read(4*size)) + self.palette = ImagePalette.raw( + "BGRA", b"\0"*4*start + self.fp.read(4*size)) # setup tile descriptor try: - rawmode = MODES[(imagetype&7, depth)] + rawmode = MODES[(imagetype & 7, depth)] if imagetype & 8: # compressed self.tile = [("tga_rle", (0, 0)+self.size, @@ -128,7 +127,7 @@ class TgaImageFile(ImageFile.ImageFile): self.tile = [("raw", (0, 0)+self.size, self.fp.tell(), (rawmode, 0, orientation))] except KeyError: - pass # cannot decode + pass # cannot decode # # -------------------------------------------------------------------- @@ -146,6 +145,7 @@ SAVE = { "RGBA": ("BGRA", 32, 0, 2), } + def _save(im, fp, filename, check=0): try: @@ -186,7 +186,8 @@ def _save(im, fp, filename, check=0): if colormaptype: fp.write(im.im.getpalette("RGB", "BGR")) - ImageFile._save(im, fp, [("raw", (0,0)+im.size, 0, (rawmode, 0, orientation))]) + ImageFile._save( + im, fp, [("raw", (0, 0) + im.size, 0, (rawmode, 0, orientation))]) # # -------------------------------------------------------------------- diff --git a/Tests/images/tga_id_field.tga b/Tests/images/tga_id_field.tga new file mode 100644 index 0000000000000000000000000000000000000000..a3d666848d1833725ed3078782a676bcaa44bc0e GIT binary patch literal 30050 zcmeI)F-ikL6b9hM!ZX;X)*GaZ5D*1d#a0R125fSJ+#n~&1#*BKU=NT!efre;$xOw?Tja0RjXF5FkK+009C72oNAZ zfB*pk1PBlyK!5-N0t5&UAV7cs0RjXF5FkK+009C72oNAZfB*pk1PBlyK!5-N0t5&U zATYGR>2#V-r<2KKv)R;jz1!^$heK}n`+dnLC^cq6`45SvX@;(PnDw{YZ3IHJTCEZU zDkDL-j>ltp8qX5MEphV0)H?L4*?u8PA$_a!`CLj%cLgfFl)f6e*x}a?MYk}v16ZQJ#F9nZ|^J~hrD2svmO@rgk32~IN6E6KR!80I#~%pWII z*>zsmAu4gPN}Mk0lKpAjtE?o)eC@2pjBw%J(_^Fk26Z8kX#@xmAV7cs0RjXF5FkK+ z009C72oNAZfB*pk1PBlyK!5-N0t5&UAV7csf&UW7Z=`?6%k%5(ZC=lx9&g|8=MS^H F>IX^pF_8cO literal 0 HcmV?d00001 diff --git a/Tests/test_file_tga.py b/Tests/test_file_tga.py new file mode 100644 index 000000000..6affba780 --- /dev/null +++ b/Tests/test_file_tga.py @@ -0,0 +1,22 @@ +from helper import unittest, PillowTestCase + +from PIL import Image + + +class TestFileSun(PillowTestCase): + + def test_sanity(self): + # tga file with id field + test_file = "Tests/images/tga_id_field.tga" + + # Act + im = Image.open(test_file) + + # Assert + self.assertEqual(im.size, (100, 100)) + + +if __name__ == '__main__': + unittest.main() + +# End of file From 5dd0d377c9437e4251f85590dda17dceb83d2f10 Mon Sep 17 00:00:00 2001 From: Alexey Buzanov Date: Wed, 20 Aug 2014 10:39:11 +0400 Subject: [PATCH 3/3] rename test --- Tests/test_file_tga.py | 6 ++---- 1 file changed, 2 insertions(+), 4 deletions(-) diff --git a/Tests/test_file_tga.py b/Tests/test_file_tga.py index 6affba780..ea94dee64 100644 --- a/Tests/test_file_tga.py +++ b/Tests/test_file_tga.py @@ -3,9 +3,9 @@ from helper import unittest, PillowTestCase from PIL import Image -class TestFileSun(PillowTestCase): +class TestFileTga(PillowTestCase): - def test_sanity(self): + def test_id_field(self): # tga file with id field test_file = "Tests/images/tga_id_field.tga" @@ -18,5 +18,3 @@ class TestFileSun(PillowTestCase): if __name__ == '__main__': unittest.main() - -# End of file