Skip to content

Commit 889ddba

Browse files
committed
Add PIL tests
1 parent 391435b commit 889ddba

File tree

3 files changed

+77
-2
lines changed

3 files changed

+77
-2
lines changed

tests/__init__.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1 +1,2 @@
1+
from .pil import *
12
from .tivars import *

tests/pil.py

Lines changed: 69 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,69 @@
1+
import io
2+
import unittest
3+
4+
from tivars.types.picture import *
5+
6+
try:
7+
from PIL import Image
8+
from tivars.PIL import *
9+
10+
except ImportError:
11+
raise unittest.SkipTest("PIL not installed")
12+
13+
14+
try:
15+
import numpy as np
16+
17+
except ImportError:
18+
raise unittest.SkipTest("NumPy not installed")
19+
20+
21+
class PILTests(unittest.TestCase):
22+
def test_8xi(self):
23+
ti_img = TIMonoPicture()
24+
ti_img.open("tests/data/var/BartSimpson.8xi")
25+
26+
arr = np.asarray(ti_img.array(), dtype=np.uint8)
27+
img = Image.open("tests/data/var/BartSimpson.8xi")
28+
29+
self.assertEqual((np.asarray(Image.fromarray(arr, mode=ti_img.pil_mode)) ==
30+
np.asarray(img)).all(), True)
31+
32+
img.save(buf := io.BytesIO(), "8xi")
33+
buf.seek(0)
34+
35+
self.assertEqual(buf.read()[72:-2], ti_img.calc_data)
36+
37+
def test_8ci(self):
38+
def the_palette_is_stinky(byte: int) -> bytes:
39+
high, low = byte // 16, byte % 16
40+
41+
high *= high != 11
42+
low *= low != 11
43+
44+
return bytes([16 * high + low])
45+
46+
ti_img = TIPicture()
47+
ti_img.open("tests/data/var/Pic1.8ci")
48+
49+
arr = np.asarray(ti_img.array(), dtype=np.uint8)
50+
img = Image.open("tests/data/var/Pic1.8ci")
51+
52+
self.assertEqual((np.asarray(Image.fromarray(arr, mode=ti_img.pil_mode)) ==
53+
np.asarray(img)).all(), True)
54+
55+
img.save(buf := io.BytesIO(), "8ci")
56+
buf.seek(0)
57+
58+
trans = b"".join(map(the_palette_is_stinky, range(256)))
59+
self.assertEqual(buf.read()[72:-2].translate(trans), ti_img.calc_data.translate(trans))
60+
61+
def test_8ca(self):
62+
ti_img = TIImage()
63+
ti_img.open("tests/data/var/Image1.8ca")
64+
65+
arr = np.asarray(ti_img.array(), dtype=np.uint8)
66+
img = Image.open("tests/data/var/Image1.8ca")
67+
68+
self.assertEqual((np.asarray(Image.fromarray(arr, mode=ti_img.pil_mode)) ==
69+
np.asarray(img)).all(), True)

tivars/PIL/common.py

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -61,7 +61,7 @@ def _save(cls, im, fp, format=None, **params):
6161
:param params: Additional encoder parameters (empty)
6262
"""
6363

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

6666

6767
class TIDecoder(ImageFile.PyDecoder):
@@ -102,7 +102,12 @@ def encode(self, bufsize):
102102
"""
103103

104104
img = self._T()
105-
img.load_array(np.asarray(self.im).reshape((img.height, img.width)).tolist())
105+
shape = img.height, img.width
106+
107+
if img.pixel_type != int:
108+
shape += 3,
109+
110+
img.load_array(np.asarray(self.im).reshape(shape).tolist())
106111
data = img.export().bytes()
107112

108113
return len(data), 0, data

0 commit comments

Comments
 (0)