Pictures with compressed quality < 100 (JPEG variant of format 4)

Header byte 1 = quality; data type 0 = baseline JPEG of the picture padded with black to
multiples of 16 and then rotated for the display direction (libjpeg/Pillow, 4:2:0, 96 dpi),
byte-identical to the Editor for opaque pictures (18/18); alpha runs cover the padded picture.
add_picture(quality=), CLI add-picture -q, MCP add_picture quality; decoder in to_image.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
mangavalkandClaude Opus 5.5 committed 2026-11-16 13:22:25 +01:00
1 parent 55c2f9d6b6
commit c102157ac2
8 files changed
+152 -23

No files matched your search

+31
View File
@@ -95,6 +95,37 @@ class Format4(unittest.TestCase):
self.assertEqual(img.getpixel((1, 0))[3], 226) # 227 * 127 // 255 = 113 -> 226
self.assertEqual(img.getpixel((2, 0))[3], 2)
@unittest.skipUnless(HAVE_PIL, "needs Pillow")
def test_jpeg_quality(self):
# "Compressed quality setting" < 100: JPEG, padded to 16 px with black, rotated by direction,
# header byte 1 = quality (layout byte-identical to Nextion Editor 1.68 for opaque pictures)
import io
from PIL import Image
f = lambda x, y: (x * 6, y * 8, (x * y) % 256)
blob = pictures.encode_jpeg(f, 40, 30, 0, 50)
self.assertEqual(blob[:2], bytes([0x0A, 0x32]))
self.assertTrue(pictures.is_jpeg(blob))
self.assertEqual(struct.unpack_from("<III", blob, 24), (0, 48, 32))
src = Image.new("RGB", (48, 32))
for y in range(30):
for x in range(40):
src.putpixel((x, y), f(x, y))
b = io.BytesIO(); src.save(b, "JPEG", quality=50, subsampling=2, dpi=(96, 96))
self.assertEqual(blob[44:], b.getvalue())
portrait = pictures.encode_jpeg(f, 40, 30, 1, 50)
self.assertEqual(struct.unpack_from("<III", portrait, 24), (0, 32, 48)) # padded, then rotated
back = pictures.to_image(portrait)
self.assertEqual(back.size, (40, 30))
smooth = pictures.to_image(pictures.encode_jpeg(lambda x, y: (x * 6, y * 8, 100), 40, 30, 1, 90))
self.assertLess(sum(abs(a - b) for a, b in zip(smooth.getpixel((20, 15)), (120, 120, 100))), 12)
@unittest.skipUnless(HAVE_PIL, "needs Pillow")
def test_jpeg_alpha_covers_padding(self):
blob = pictures.encode_jpeg(lambda x, y: (255, 0, 0, 128), 17, 5, 0, 30)
aoff, = struct.unpack_from("<I", blob, 20)
self.assertEqual(blob[24 + aoff:], bytes([63, 17, 0, 15]) * 4 + bytes([63, 17, 0, 255, 0, 112]))
self.assertEqual(pictures.to_image(blob).getpixel((3, 3))[3], 126)
def test_overhang_columns(self):
# Font Creator fonts: map bytes 3/4 = extra columns left/right of the advance width
rows = [[1, 0, 0, 1, 1]] * 2