Skip to content

Commit

Permalink
Restore Python 3 compatibility
Browse files Browse the repository at this point in the history
  • Loading branch information
encukou committed Oct 20, 2013
1 parent 1959f43 commit d91bf7d
Show file tree
Hide file tree
Showing 4 changed files with 14 additions and 15 deletions.
4 changes: 2 additions & 2 deletions tmxlib/canvas.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@

from __future__ import division

from StringIO import StringIO
from six import BytesIO

try:
from PIL import Image
Expand Down Expand Up @@ -72,7 +72,7 @@ def draw_image(self, image, pos=(0, 0)):
try:
pil_image = parent.pil_image
except AttributeError:
input = StringIO(parent._repr_png_())
input = BytesIO(parent._repr_png_())
pil_image = Image.open(input)
if crop:
pil_image = pil_image.crop((image.x, image.y,
Expand Down
6 changes: 3 additions & 3 deletions tmxlib/image_pil.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@

from __future__ import division

import StringIO
from six import BytesIO

from PIL import Image

Expand All @@ -22,7 +22,7 @@ def load_image(self):
self._pil_image
return self.size
except AttributeError:
self._pil_image = Image.open(StringIO.StringIO(self.data))
self._pil_image = Image.open(BytesIO(self.data))
self._pil_image = self._pil_image.convert('RGBA')
w, h = self._pil_image.size
if self._size:
Expand Down Expand Up @@ -52,6 +52,6 @@ def _repr_png_(self, _crop_box=None):
image = self.pil_image.crop(_crop_box)
else:
image = self.pil_image
buf = StringIO.StringIO()
buf = BytesIO()
image.save(buf, "PNG")
return buf.getvalue()
5 changes: 2 additions & 3 deletions tmxlib/image_png.py
Original file line number Diff line number Diff line change
@@ -1,8 +1,7 @@

from __future__ import division

from array import array
from StringIO import StringIO
from six import BytesIO

import png

Expand Down Expand Up @@ -47,7 +46,7 @@ def _repr_png_(self, _crop_box=None):
if _crop_box:
left, up, right, low = _crop_box
data = [l[left * 4:right * 4] for l in self.image_data[up:low]]
out = StringIO()
out = BytesIO()
png.from_array(data, 'RGBA').save(out)
return out.getvalue()
return self.data
14 changes: 7 additions & 7 deletions tmxlib_test/test_image.py
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
from __future__ import division
from __future__ import division, print_function

import os
import warnings
from StringIO import StringIO
from six import BytesIO

import pytest

Expand Down Expand Up @@ -285,16 +285,16 @@ def test_region_hierarchy(colorcorners_image, colorcorners_image_type):
def assert_png_repr_equal(image, filename):
data = image._repr_png_()
a = pil_image_open(get_test_filename(filename))
b = pil_image_open(StringIO(data))
b = pil_image_open(BytesIO(data))
assert b.format == 'PNG'
abytes = a.convert('RGBA').tobytes()
bbytes = b.convert('RGBA').tobytes()
if abytes != bbytes:
from tmxlib_test.image_to_term import image_to_term256
print "Expected: ({im.size[0]}x{im.size[1]})".format(im=a)
print image_to_term256(a)
print "Got: ({im.size[0]}x{im.size[1]})".format(im=b)
print image_to_term256(b)
print("Expected: ({im.size[0]}x{im.size[1]})".format(im=a))
print(image_to_term256(a))
print("Got: ({im.size[0]}x{im.size[1]})".format(im=b))
print(image_to_term256(b))
assert abytes == bbytes


Expand Down

0 comments on commit d91bf7d

Please sign in to comment.