From 3ef981cd9848f7bb4dbf0122345aefa743160bee Mon Sep 17 00:00:00 2001 From: Tuomas Salo Date: Thu, 7 Jan 2016 00:24:23 +0200 Subject: [PATCH] Pillow 3.x removes fromstring() in favor of frombytes() --- swf/export.py | 8 +++++++- swf/tag.py | 10 ++++++++-- 2 files changed, 15 insertions(+), 3 deletions(-) diff --git a/swf/export.py b/swf/export.py index cbb0b3f..fe95188 100644 --- a/swf/export.py +++ b/swf/export.py @@ -14,6 +14,12 @@ import Image except ImportError: from PIL import Image + + # Fix Pillow 1.x -> 3.x incompatibility: + # (fromstring was renamed to frombytes) + if not hasattr(Image, 'frombytes'): + Image.frombytes = Image.fromstring + try: from cStringIO import StringIO except ImportError: @@ -425,7 +431,7 @@ def export_define_bits(self, tag): alpha = ord(tag.bitmapAlphaData.read(1)) rgb = list(image_data[i]) buff += struct.pack("BBBB", rgb[0], rgb[1], rgb[2], alpha) - image = Image.fromstring("RGBA", (image_width, image_height), buff) + image = Image.frombytes("RGBA", (image_width, image_height), buff) elif isinstance(tag, TagDefineBitsJPEG2): tag.bitmapData.seek(0) image = Image.open(tag.bitmapData) diff --git a/swf/tag.py b/swf/tag.py index a6f20a9..97856f0 100644 --- a/swf/tag.py +++ b/swf/tag.py @@ -7,6 +7,12 @@ import Image except ImportError: from PIL import Image + + # Fix Pillow 1.x -> 3.x incompatibility: + # (fromstring was renamed to frombytes) + if not hasattr(Image, 'frombytes'): + Image.frombytes = Image.fromstring + import struct try: @@ -888,7 +894,7 @@ def parse(self, data, length, version=1): self.image_buffer = s.getvalue() s.close() - im = Image.fromstring("RGBA", (self.padded_width, self.bitmap_height), self.image_buffer) + im = Image.frombytes("RGBA", (self.padded_width, self.bitmap_height), self.image_buffer) im = im.crop((0, 0, self.bitmap_width, self.bitmap_height)) elif self.bitmap_format == BitmapFormat.BIT_15: @@ -907,7 +913,7 @@ def parse(self, data, length, version=1): b = ord(temp.read(1)) s.write(struct.pack("BBBB", r, g, b, a)) self.image_buffer = s.getvalue() - im = Image.fromstring("RGBA", (self.bitmap_width, self.bitmap_height), self.image_buffer) + im = Image.frombytes("RGBA", (self.bitmap_width, self.bitmap_height), self.image_buffer) else: raise Exception("unhandled bitmap format! %s %d" % (BitmapFormat.tostring(self.bitmap_format), self.bitmap_format))