Something went wrong. Try again.
small gleam coding and persistent agent daemon with a detachable cli and a canvas ui forked from okami.mom/albedo
Something went wrong. Try again.
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596"""Check JPEG geometry and visible orientation against the unencoded scene."""
from pathlib import Pathimport runpyimport sysimport tempfileimport unittestfrom unittest.mock import patch
import hypothesis_profilesfrom hypothesis import given, settings, strategies as stfrom PIL import Image, ImageOpsfrom pillow_heif import register_heif_opener
CONVERTER = Path(__file__).resolve().parents[1] / "normalize-heic.py"COLORS = [(225, 30, 30), (25, 215, 50), (30, 45, 230), (220, 205, 25)]
class NormalizeHeicTests(unittest.TestCase): def test_rotated_tiled_still_keeps_the_full_frame(self): # The 2176x1152 scene was encoded with heif-enc --cut-tiles 512 --rotate-cw 90. fixture = Path(__file__).parent / "fixtures/rotated-grid.heic" with tempfile.TemporaryDirectory() as directory: dest = Path(directory) / "preview.jpg" with patch.object(sys, "argv", [str(CONVERTER), str(fixture), str(dest)]): runpy.run_path(str(CONVERTER), run_name="__main__") self.assertLessEqual(dest.stat().st_size, 5 * 1024 * 1024) with Image.open(dest) as output: self.assertEqual(output.format, "JPEG") self.assertEqual(output.height, 2000) self.assertAlmostEqual( output.width / output.height, 1152 / 2176, delta=0.001 ) corners = [(1, 1), (3, 1), (1, 3), (3, 3)] colors = [(0, 128, 0), (255, 0, 0), (255, 255, 0), (0, 0, 255)] for (x, y), expected in zip(corners, colors, strict=True): actual = output.getpixel( (output.width * x // 4, output.height * y // 4) ) for channel, value in zip(actual, expected, strict=True): self.assertAlmostEqual(channel, value, delta=20)
@settings(max_examples=hypothesis_profiles.cases(24)) @given( width=st.one_of( st.integers(64, 256), st.sampled_from([1999, 2000, 2001, 3072]) ), height=st.integers(64, 160), orientation=st.integers(1, 8), primary_last=st.booleans(), ) def test_primary_still_keeps_display_orientation_and_scale( self, width, height, orientation, primary_last ): register_heif_opener() quadrants = Image.new("RGB", (2, 2)) quadrants.putdata(COLORS) scene = quadrants.resize((width, height), Image.Resampling.NEAREST) scene.getexif()[274] = orientation scene.info["exif"] = scene.getexif().tobytes() upright = ImageOps.exif_transpose(scene) with tempfile.TemporaryDirectory() as directory: src, dest = ( Path(directory) / name for name in ("still.heic", "preview.jpg") ) if primary_last: cover = Image.new("RGB", (64, 64), "black") cover.save(src, save_all=True, append_images=[scene], primary_index=1) else: scene.save(src) # iso8 is the major brand; HEIC support is declared by compatible brands. with src.open("r+b") as encoded: encoded.seek(8) encoded.write(b"iso8") with patch.object(sys, "argv", [str(CONVERTER), str(src), str(dest)]): runpy.run_path(str(CONVERTER), run_name="__main__") with Image.open(dest) as output: self.assertEqual(output.format, "JPEG") self.assertEqual(max(output.size), min(2000, max(upright.size))) scale = max(output.size) / max(upright.size) for actual, original in zip(output.size, upright.size, strict=True): self.assertLessEqual(abs(actual - original * scale), 1) for x, y in [(1, 1), (3, 1), (1, 3), (3, 3)]: expected = upright.getpixel( (upright.width * x // 4, upright.height * y // 4) ) actual = output.getpixel( (output.width * x // 4, output.height * y // 4) ) for a, b in zip(actual, expected, strict=True): self.assertLessEqual(abs(a - b), 20)
if __name__ == "__main__": unittest.main()