Skip to content

Commit

Permalink
Add PIL tests
Browse files Browse the repository at this point in the history
  • Loading branch information
kg583 committed Sep 12, 2023
1 parent 391435b commit 889ddba
Show file tree
Hide file tree
Showing 3 changed files with 77 additions and 2 deletions.
1 change: 1 addition & 0 deletions tests/__init__.py
Original file line number Diff line number Diff line change
@@ -1 +1,2 @@
from .pil import *
from .tivars import *
69 changes: 69 additions & 0 deletions tests/pil.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,69 @@
import io
import unittest

from tivars.types.picture import *

try:
from PIL import Image
from tivars.PIL import *

except ImportError:
raise unittest.SkipTest("PIL not installed")


try:
import numpy as np

except ImportError:
raise unittest.SkipTest("NumPy not installed")


class PILTests(unittest.TestCase):
def test_8xi(self):
ti_img = TIMonoPicture()
ti_img.open("tests/data/var/BartSimpson.8xi")

arr = np.asarray(ti_img.array(), dtype=np.uint8)
img = Image.open("tests/data/var/BartSimpson.8xi")

self.assertEqual((np.asarray(Image.fromarray(arr, mode=ti_img.pil_mode)) ==
np.asarray(img)).all(), True)

img.save(buf := io.BytesIO(), "8xi")
buf.seek(0)

self.assertEqual(buf.read()[72:-2], ti_img.calc_data)

def test_8ci(self):
def the_palette_is_stinky(byte: int) -> bytes:
high, low = byte // 16, byte % 16

high *= high != 11
low *= low != 11

return bytes([16 * high + low])

ti_img = TIPicture()
ti_img.open("tests/data/var/Pic1.8ci")

arr = np.asarray(ti_img.array(), dtype=np.uint8)
img = Image.open("tests/data/var/Pic1.8ci")

self.assertEqual((np.asarray(Image.fromarray(arr, mode=ti_img.pil_mode)) ==
np.asarray(img)).all(), True)

img.save(buf := io.BytesIO(), "8ci")
buf.seek(0)

trans = b"".join(map(the_palette_is_stinky, range(256)))
self.assertEqual(buf.read()[72:-2].translate(trans), ti_img.calc_data.translate(trans))

def test_8ca(self):
ti_img = TIImage()
ti_img.open("tests/data/var/Image1.8ca")

arr = np.asarray(ti_img.array(), dtype=np.uint8)
img = Image.open("tests/data/var/Image1.8ca")

self.assertEqual((np.asarray(Image.fromarray(arr, mode=ti_img.pil_mode)) ==
np.asarray(img)).all(), True)
9 changes: 7 additions & 2 deletions tivars/PIL/common.py
Original file line number Diff line number Diff line change
Expand Up @@ -61,7 +61,7 @@ def _save(cls, im, fp, format=None, **params):
:param params: Additional encoder parameters (empty)
"""

ImageFile._save(im, fp, format, [(cls.format, (0, 0) + im.size, 0, im.mode)])
ImageFile._save(im, fp, [(cls.format, (0, 0) + im.size, 0, im.mode)])


class TIDecoder(ImageFile.PyDecoder):
Expand Down Expand Up @@ -102,7 +102,12 @@ def encode(self, bufsize):
"""

img = self._T()
img.load_array(np.asarray(self.im).reshape((img.height, img.width)).tolist())
shape = img.height, img.width

if img.pixel_type != int:
shape += 3,

img.load_array(np.asarray(self.im).reshape(shape).tolist())
data = img.export().bytes()

return len(data), 0, data

0 comments on commit 889ddba

Please sign in to comment.