"""
check_figure.py — will this image pass a journal's resolution check?

Hu Lab · https://hulab.pages.dev/en/guides/journal-figure-size-dpi-guide/
Requires: pillow   (pip install pillow)

    python check_figure.py Fig1.tif            # assumes single column (90 mm)
    python check_figure.py Fig1.tif 190        # double column (190 mm)
"""
import math
import sys
from PIL import Image

Image.MAX_IMAGE_PIXELS = None
path = sys.argv[1]
width_mm = float(sys.argv[2]) if len(sys.argv) > 2 else 90.0

with Image.open(path) as im:
    w, h = im.size
    mode = im.mode
    meta = im.info.get("dpi")

effective = w / (width_mm / 25.4)          # pixels per inch at the printed width
print(f"File          : {path}")
print(f"Pixels        : {w} x {h}")
print(f"Colour mode   : {mode}" + ("  <- remove transparency (convert to RGB)" if mode in ("RGBA", "LA", "P") else ""))
print(f"DPI metadata  : {float(meta[0]):.0f}" if meta else "DPI metadata  : (none)")
print(f"Printed width : {width_mm:.0f} mm  ->  effective resolution {effective:.0f} dpi")
for need, kind in ((300, "photo / micrograph"), (600, "line art (typical)"), (1000, "line art (Elsevier)")):
    need_px = math.floor(width_mm / 25.4 * need)
    ok = "OK " if w >= need_px else "LOW"
    print(f"  [{ok}] {need:>4} dpi  {kind:<20} needs {need_px} px wide")
