Image Highlight Cropper
Extract the 5 most visually interesting detail regions from a large image, crop them as squares, display them in the chat (2 per row), and save them as downloadable files.
Workflow
Step 1 – Analyse the image
Look carefully at the uploaded image. Identify the 5 most interesting regions based on:
- - Visual richness: texture, fine detail, intricate patterns
- Artistic significance: focal points, expressive faces, key objects
- Contrast and color drama
- Unique or surprising elements the viewer might miss at first glance
⚠️ Signature rule: If a painter's signature is visible anywhere in the image, it must always be one of the 5 highlights. Center the crop precisely on the signature so it is fully visible and not cut off.
⚠️ Strict 1:1 square-fit rule: Every highlight must be a subject that naturally fills a square frame. Before selecting a region, ask: "Does this subject fit well into a square?"
- - ✅ Good: a face, a bouquet of flowers, a single tree, a lamp post with surroundings, a signature, a wheel, a window, a doorway, a small group of figures
- ❌ Bad: a wide panoramic skyline, a long horizontal street scene, a tall thin tower spanning the full image height — these are inherently non-square and will look like an awkward strip crop
- If a subject is too wide or too tall to feel natural in a 1:1 frame, skip it and choose something else that genuinely fits a square
- The goal: each crop looks intentional and well-composed, as if it were a standalone photograph
For each region, record:
- - A short label (e.g. "Gesicht links", "Goldornament", "Signatur")
- Center point (cx, cy) of the subject and a half-size (half the desired square side length)
- A one-sentence explanation of why this area is interesting
Step 2 – Crop with Python (Pillow)
Use the bash_tool + Python to:
- 1. Load the image from INLINECODE1
- For each region: use the
save_crop helper below — it handles edge cases automatically - Save each crop to
/mnt/user-data/outputs/highlight_1.jpg … INLINECODE4
Choosing the half-size: Claude decides per region:
- - Tiny ornament or signature → half = 100–200 px
- Face or small group → half = 200–350 px
- Large scene or texture area → half = 350–600 px
- Rule of thumb: the crop should feel like a natural close-up. Always use center-based coordinates.
Edge case — subject at image border: If the desired crop extends beyond the image boundary, save_crop takes whatever image content is available and places it centered on a white square canvas (side = longest available side). This keeps the result square and clean with no distortion.
CODEBLOCK0
Step 3 – Display in chat
After saving, use present_files to make all 5 crops downloadable.
Then write a short markdown summary:
CODEBLOCK1
Show the crops 2 per row by presenting them via present_files and listing them clearly with their labels. Users can download each file individually.
Step 4 – Invite feedback
Ask the user: "Soll ich andere Bereiche auswählen, oder die Größe der Crops anpassen?"
Tips for choosing good crops
- - Avoid overlap between the 5 regions as much as possible
- Spread across the image — don't cluster all crops in one corner
- For paintings: prioritize faces, hands, symbolic objects, and texture-rich backgrounds
- For technical drawings: prioritize labels, detail views, and complex intersections
- half-size should be roughly 10–20% of the shorter image dimension so crops feel like genuine close-ups, not tiny stamps
Error handling
- - If the image cannot be opened, tell the user and ask them to re-upload
- If Pillow is not installed: INLINECODE8
图像高亮裁剪器
从大图中提取5个视觉上最有趣的细节区域,裁剪为正方形,在聊天中显示(每行2个),并保存为可下载文件。
工作流程
第1步 – 分析图像
仔细观察上传的图像。根据以下标准识别5个最有趣的区域:
- - 视觉丰富度:纹理、精细细节、复杂图案
- 艺术重要性:焦点、富有表现力的面部、关键物体
- 对比度和色彩戏剧性
- 观众第一眼可能忽略的独特或令人惊讶的元素
⚠️ 签名规则: 如果图像中任何位置可见画家的签名,则签名必须始终是5个高亮区域之一。将裁剪中心精确对准签名,使其完全可见且不被切断。
⚠️ 严格1:1正方形适配规则: 每个高亮区域必须是一个能自然填充正方形画幅的主体。在选择区域之前,请问自己:这个主体适合放入正方形吗?
- - ✅ 好的:一张脸、一束花、一棵树、带环境的路灯、签名、轮子、窗户、门口、一小群人
- ❌ 不好的:宽阔的全景天际线、长条水平街景、贯穿整个图像高度的细长高塔——这些本质上不适合正方形,裁剪后会显得像尴尬的条状
- 如果主体太宽或太高,在1:1画幅中显得不自然,跳过它,选择其他真正适合正方形的主体
- 目标:每次裁剪看起来都经过精心设计且构图良好,仿佛是一张独立的照片
对于每个区域,记录:
- - 简短的标签(例如左侧面部、金色装饰、签名)
- 主体的中心点 (cx, cy) 和半边长(所需正方形边长的一半)
- 一句话解释说明该区域为何有趣
第2步 – 使用Python(Pillow)裁剪
使用bash_tool + Python:
- 1. 从/mnt/user-data/uploads/<文件名>加载图像
- 对于每个区域:使用下面的savecrop辅助函数——它会自动处理边缘情况
- 将每个裁剪保存到/mnt/user-data/outputs/highlight1.jpg … highlight_5.jpg
选择半边长: Claude根据区域决定:
- - 微小装饰或签名 → 半边长 = 100–200像素
- 面部或小群体 → 半边长 = 200–350像素
- 大场景或纹理区域 → 半边长 = 350–600像素
- 经验法则:裁剪应感觉像自然的特写。始终使用基于中心的坐标。
边缘情况——主体位于图像边界: 如果所需裁剪超出图像边界,save_crop会取可用的图像内容,并将其居中放置在白色正方形画布上(边长 = 最长可用边长)。这样保持结果正方形且干净,无变形。
python
from PIL import Image
import os
img = Image.open(/mnt/user-data/uploads/IMAGE_FILENAME)
w, h = img.size
def save_crop(img, w, h, cx, cy, half, path):
x1 = max(0, cx - half)
x2 = min(w, cx + half)
y1 = max(0, cy - half)
y2 = min(h, cy + half)
rect_w = x2 - x1
rect_h = y2 - y1
crop = img.crop((x1, y1, x2, y2))
if rectw == recth:
# 已经是正方形——直接保存
crop.save(path, quality=92)
else:
# 放置在白色正方形画布上,水平和垂直居中
canvassize = max(rectw, rect_h)
canvas = Image.new(RGB, (canvassize, canvassize), (255, 255, 255))
pastex = (canvassize - rect_w) // 2
pastey = (canvassize - rect_h) // 2
canvas.paste(crop, (pastex, pastey))
canvas.save(path, quality=92)
通过中心点 (cx, cy) 和半边长定义裁剪
crops = [
# (标签, cx, cy, 半边长)
(highlight_1, cx1, cy1, half1),
(highlight_2, cx2, cy2, half2),
(highlight_3, cx3, cy3, half3),
(highlight_4, cx4, cy4, half4),
(highlight_5, cx5, cy5, half5),
]
os.makedirs(/mnt/user-data/outputs, exist_ok=True)
for label, cx, cy, half in crops:
save_crop(img, w, h, cx, cy, half, f/mnt/user-data/outputs/{label}.jpg)
print(完成)
第3步 – 在聊天中显示
保存后,使用present_files使所有5个裁剪可下载。
然后编写简短的markdown摘要:
🎨 图像中的5个高亮区域
1. [标签] – [解释为何有趣]
2. [标签] – ...
...
通过present_files展示裁剪,每行显示2个,并清晰列出其标签。用户可以单独下载每个文件。
第4步 – 邀请反馈
询问用户:是否需要选择其他区域,或调整裁剪的大小?
选择良好裁剪的技巧
- - 避免重叠——5个区域之间尽可能不重叠
- 分散在图像中——不要将所有裁剪集中在同一个角落
- 对于绘画:优先选择面部、手部、象征性物体和纹理丰富的背景
- 对于技术图纸:优先选择标签、细节视图和复杂的交叉点
- 半边长应约为较短图像尺寸的10–20%,使裁剪感觉像真正的特写,而不是小图章
错误处理
- - 如果无法打开图像,告知用户并要求重新上传
- 如果未安装Pillow:pip install Pillow --break-system-packages