Skip to content

GLM-5.3-Flash EXL3 on 2x DGX Spark with TensorFold

Python

Patch 0110: an iPhone MPO JPEG is read from its first frame (issue #201)

MiaAI-Lab39bc482fa7da
diff --git a/patches/0110-vision-mpo-first-frame.patch b/patches/0110-vision-mpo-first-frame.patch
new file mode 100644
index 0000000..053d9ca
--- /dev/null
+++ b/patches/0110-vision-mpo-first-frame.patch
@@ -0,0 +1,18 @@
+# Phone photos (iPhone MPO JPEGs) are accepted (issue #201): the decode guard refused any image whose Pillow
+# n_frames was not 1, and an MPO file reports 2 or more (the picture plus a preview, depth map or gain map) although
+# only its first frame is ever loaded. An MPO is now let through; animated WebP and multi-frame PNG images are
+# still refused, and the size and pixel limits above the guard still apply to the (first-frame) dimensions.
+# MiaAI-Lab, Apache-2.0.
+--- tensorfold/vision/images.py
++++ tensorfold/vision/images.py
+@@ -211,7 +211,9 @@
+ if (min(width, height) <= 0 or max(width, height) > limits.max_dimension
+ or width * height > min(limits.max_pixels, remaining_pixels)):
+ raise ImageInputError("image dimensions exceed the decoded pixel limit")
+- if getattr(source, "n_frames", 1) != 1:
++ # An MPO (what iPhones and many Android phones save as .jpg) is a JPEG whose first frame is the
++ # picture; its other frames (preview, depth, gain map) are never read: only frame 0 is loaded below.
++ if getattr(source, "n_frames", 1) != 1 and source.format != "MPO":
+ raise ImageInputError("animated and multipage images are unsupported; send a single frame")
+ source.verify()
+ with image.open(io.BytesIO(data), formats=_FORMATS) as source:
diff --git a/tools/test_mpo_first_frame.py b/tools/test_mpo_first_frame.py
new file mode 100644
index 0000000..e8832ec
--- /dev/null
+++ b/tools/test_mpo_first_frame.py
@@ -0,0 +1,67 @@
+"""CPU-only check that a phone photo (an MPO JPEG: the picture plus an auxiliary frame) is read (patch 0110, issue #201).
+
+ python3 -B tools/test_mpo_first_frame.py --source-root /path/to/patched/src
+Use --expect-stock on the source without 0110 to see the 400 the issue reports. Needs Pillow; no torch, GPU or sockets.
+"""
+from __future__ import annotations
+
+import argparse
+import base64
+import io
+import sys
+
+
+def main():
+ ap = argparse.ArgumentParser()
+ ap.add_argument("--source-root", required=True)
+ ap.add_argument("--expect-stock", action="store_true")
+ args = ap.parse_args()
+ sys.path.insert(0, args.source_root)
+ from PIL import Image
+ from tensorfold.vision.images import ImageSource, load_images
+ from tensorfold.vision.images_http import ImageInputError
+
+ def url(data: bytes, media: str) -> str:
+ return f"data:{media};base64,{base64.b64encode(data).decode()}"
+
+ def save(fmt: str, frames: list, **kw) -> bytes:
+ buf = io.BytesIO()
+ frames[0].save(buf, format=fmt, save_all=len(frames) > 1, append_images=frames[1:], **kw)
+ return buf.getvalue()
+
+ def load(data: bytes, media: str):
+ try:
+ return load_images([ImageSource(url(data, media))]), None
+ except ImageInputError as exc:
+ return None, str(exc)
+
+ picture = Image.new("RGB", (64, 48), (200, 30, 30))
+ auxiliary = Image.new("RGB", (16, 12), (30, 30, 200))
+ mpo = save("MPO", [picture, auxiliary])
+ assert Image.open(io.BytesIO(mpo)).n_frames == 2 and Image.open(io.BytesIO(mpo)).format == "MPO", "not an MPO"
+ got, error = load(mpo, "image/jpeg")
+ if args.expect_stock:
+ assert error and "multipage" in error, f"stock source accepted an MPO ({error})"
+ print(f"stock: the MPO is refused, as reported ({error})")
+ return
+ assert error is None, error
+ image = got[0]
+ assert (image.width, image.height) == (64, 48), (image.width, image.height)
+ r, g, b = image.pixels[:3]
+ assert r > 150 and g < 90 and b < 90, ("first frame was not the picture", (r, g, b))
+ # an auxiliary frame far larger than the limits is never decoded: the picture's own size is what is checked
+ big = save("MPO", [picture, Image.new("RGB", (8192, 8192), (0, 0, 0))])
+ assert load(big, "image/jpeg")[1] is None, "an MPO with a large auxiliary frame was refused"
+ # a plain JPEG and a PNG are unchanged; other multi-frame images are still refused
+ assert load(save("JPEG", [picture]), "image/jpeg")[1] is None
+ assert load(save("PNG", [picture]), "image/png")[1] is None
+ for fmt, media in (("WEBP", "image/webp"), ("PNG", "image/png")):
+ frames = [picture, Image.new("RGB", (64, 48), (0, 200, 0))]
+ kw = {"duration": 100, "loop": 0} if fmt == "WEBP" else {}
+ error = load(save(fmt, frames, **kw), media)[1]
+ assert error and "multipage" in error, (fmt, error)
+ print("MPO: the first frame is read, auxiliary frames are not decoded, animated WebP and APNG are still refused")
+
+
+if __name__ == "__main__":
+ main()