e558e4a ←3490c64antonApr 24, 2026 api

create resize.py via macOS

A /docs/undefined +61 -0
A /docs/undefined +61 -0
1"""
2Image resize: reads all images, creates thumbnails and a contact sheet.
3"""
4import modal
5
6app = modal.App("image-resize")
7image = modal.Image.debian_slim().pip_install("pillow")
8
9@app.function(image=image)
10def resize():
11 import os
12 from PIL import Image
13
14 THUMB_SIZE = (256, 256)
15 EXTENSIONS = {".png", ".jpg", ".jpeg", ".webp", ".gif", ".bmp"}
16
17 images = sorted(
18 f for f in os.listdir(".")
19 if os.path.splitext(f)[1].lower() in EXTENSIONS
20 )
21
22 if not images:
23 print("No images found")
24 return
25
26 os.makedirs("thumbs", exist_ok=True)
27
28 thumbs = []
29 report = ["# image processing report\n"]
30
31 for fname in images:
32 img = Image.open(fname)
33 report.append(f"- {fname}: {img.size[0]}x{img.size[1]}, {img.mode}")
34
35 # create thumbnail
36 thumb = img.copy()
37 thumb.thumbnail(THUMB_SIZE)
38 thumb_path = f"thumbs/{os.path.splitext(fname)[0]}_thumb.png"
39 thumb.save(thumb_path, "PNG")
40 thumbs.append(thumb)
41
42 # create contact sheet (grid of thumbnails)
43 if thumbs:
44 cols = min(4, len(thumbs))
45 rows = (len(thumbs) + cols - 1) // cols
46 sheet = Image.new("RGB", (cols * THUMB_SIZE[0], rows * THUMB_SIZE[1]), (255, 255, 255))
47 for i, thumb in enumerate(thumbs):
48 x = (i % cols) * THUMB_SIZE[0]
49 y = (i // cols) * THUMB_SIZE[1]
50 # center thumbnail in its cell
51 ox = (THUMB_SIZE[0] - thumb.size[0]) // 2
52 oy = (THUMB_SIZE[1] - thumb.size[1]) // 2
53 sheet.paste(thumb, (x + ox, y + oy))
54 sheet.save("contact_sheet.png", "PNG")
55 report.append(f"\n## output\n- {len(thumbs)} thumbnails in thumbs/")
56 report.append(f"- contact sheet: {cols}x{rows} grid")
57
58 with open("report.md", "w") as f:
59 f.write("\n".join(report))
60
61 print(f"Done: processed {len(images)} images")