#!/usr/bin/env python3 """Read Figure 7 of Tacza et al. (2026), J. Space Weather Space Clim. 16, 36 (CC BY 4.0), out of the paper's own PDF. Used by fig7_digitize.py, fig7_crosscheck_cloud.py, fig7_evo_check.py and fig4_digitize.py; needs Pillow. The PDF is the journal's swsc250080.pdf (open access; 12,166,745 bytes; SHA-256 c9faeb3b...1929), which you download yourself: the journal's site refused our client. Figure 7 is embedded as one CMYK JPEG, 2604 x 3732 pixels. We take that stream exactly as embedded (no rendering), check its SHA-256 against the one we digitized, and convert it to RGB with Pillow. The JPEG carries an Adobe marker, so its CMYK values are stored inverted; Pillow does not undo that, so we invert first. """ import hashlib import io import re import sys from PIL import Image, ImageChops PDF_SHA256 = "c9faeb3bf483f0a00cc3d09d75b8becd12f52835efd0b91170b7ba9621551929" FIG7_JPEG_SHA256 = "65c120161991cac2c9350f6d8a1eda215b62a4efe1d4676e83c96f902051d864" def embedded_image(pdf_path, width, height, sha256=None, strict=True): """The embedded JPEG of the given size, exactly as stored, as an RGB image (Adobe CMYK inversion undone).""" data = open(pdf_path, "rb").read() if hashlib.sha256(data).hexdigest() != PDF_SHA256: print("note: this PDF is not byte-identical to the one we read; continuing with the image check", file=sys.stderr) for m in re.finditer(rb"<<(.{0,600}?)>>\s*stream\r?\n", data, re.S): d = m.group(1) if b"/Width %d" % width in d and b"/Height %d" % height in d and b"/DCTDecode" in d: n = int(re.search(rb"/Length (\d+)", d).group(1)) jpeg = data[m.end():m.end() + n] got = hashlib.sha256(jpeg).hexdigest() if sha256 and got != sha256: msg = "the %d x %d image differs from the one we read" % (width, height) if strict: raise SystemExit(msg) print("warning: " + msg, file=sys.stderr) if not sha256: print("image %d x %d, SHA-256 %s" % (width, height, got), file=sys.stderr) im = Image.open(io.BytesIO(jpeg)) if im.mode == "CMYK": im = ImageChops.invert(im) return im.convert("RGB") raise SystemExit("no %d x %d JPEG in %s" % (width, height, pdf_path)) def figure7(pdf_path, strict=True): return embedded_image(pdf_path, 2604, 3732, FIG7_JPEG_SHA256, strict) if __name__ == "__main__": im = figure7(sys.argv[1]) print("Figure 7:", im.size, im.mode)