Image Utilities
Pillow-based utilities for deterministic pixel-level image operations. Use for resize, crop, composite, format conversion, watermarks, and other standard image processing tasks.
When to Use This Skill
- - Post-processing AI-generated images: Resize, crop, optimize for web after generation
- Format conversion: PNG ↔ JPEG ↔ WEBP with quality control
- Compositing: Overlay images, paste subjects onto backgrounds
- Batch processing: Resize to multiple sizes, add watermarks
- Web optimization: Compress and resize for fast delivery
- Social media preparation: Crop to platform-specific aspect ratios
When NOT to Use This Skill — Use bria-ai Instead
This skill handles deterministic pixel-level operations only. For any generative or AI-powered image work, use the bria-ai skill instead:
- - Generating images from text prompts → use INLINECODE2
- AI background removal or replacement → use INLINECODE3
- AI image editing (inpainting, object removal/addition) → use INLINECODE4
- Style transfer or AI-driven visual effects → use INLINECODE5
- Creating product lifestyle shots with AI → use INLINECODE6
- Image upscaling with AI super-resolution → use INLINECODE7
Rule of thumb: If the task requires creating new visual content or understanding image semantics, use bria-ai. If the task requires transforming existing pixels (resize, crop, format convert, watermark), use this skill.
If bria-ai is not available, install it with:
CODEBLOCK0
Quick Reference
| Operation | Method | Description |
|---|
| Loading | INLINECODE10 | Load from URL, path, bytes, or base64 |
|
load_from_url(url) | Download image from URL |
|
Saving |
save(image, path) | Save with format auto-detection |
| |
to_bytes(image, format) | Convert to bytes |
| |
to_base64(image, format) | Convert to base64 string |
|
Resizing |
resize(image, width, height) | Resize to exact dimensions |
| |
scale(image, factor) | Scale by factor (0.5 = half) |
| |
thumbnail(image, size) | Fit within size, maintain aspect |
|
Cropping |
crop(image, left, top, right, bottom) | Crop to region |
| |
crop_center(image, width, height) | Crop from center |
| |
crop_to_aspect(image, ratio) | Crop to aspect ratio |
|
Compositing |
paste(bg, fg, position) | Overlay at coordinates |
| |
composite(bg, fg, mask) | Alpha composite |
| |
fit_to_canvas(image, w, h) | Fit onto canvas size |
|
Borders |
add_border(image, width, color) | Add solid border |
| |
add_padding(image, padding) | Add whitespace padding |
|
Transforms |
rotate(image, angle) | Rotate by degrees |
| |
flip_horizontal(image) | Mirror horizontally |
| |
flip_vertical(image) | Flip vertically |
|
Watermarks |
add_text_watermark(image, text) | Add text overlay |
| |
add_image_watermark(image, logo) | Add logo watermark |
|
Adjustments |
adjust_brightness(image, factor) | Lighten/darken |
| |
adjust_contrast(image, factor) | Adjust contrast |
| |
adjust_saturation(image, factor) | Adjust color saturation |
| |
blur(image, radius) | Apply Gaussian blur |
|
Web |
optimize_for_web(image, max_size) | Optimize for delivery |
|
Info |
get_info(image) | Get dimensions, format, mode |
Requirements
CODEBLOCK1
Basic Usage
CODEBLOCK2
Resizing & Scaling
CODEBLOCK3
Cropping
CODEBLOCK4
Compositing
CODEBLOCK5
Format Conversion
CODEBLOCK6
Watermarks
CODEBLOCK7
Adjustments
CODEBLOCK8
Transforms
CODEBLOCK9
Borders & Padding
CODEBLOCK10
Web Optimization
CODEBLOCK11
Integration with Bria AI
Use alongside the bria-ai skill to post-process AI-generated images. Generate or edit images with Bria's API, then use image-utils for resizing, cropping, watermarking, and web optimization.
CODEBLOCK12
Batch Processing Example
CODEBLOCK13
API Reference
See image_utils.py for complete implementation with docstrings.
图像工具
基于Pillow的确定性像素级图像操作工具。用于调整大小、裁剪、合成、格式转换、水印及其他标准图像处理任务。
何时使用此技能
- - AI生成图像后处理:生成后调整大小、裁剪、优化网页显示
- 格式转换:PNG ↔ JPEG ↔ WEBP 格式转换及质量控制
- 图像合成:叠加图像、将主体粘贴到背景上
- 批量处理:调整到多种尺寸、添加水印
- 网页优化:压缩和调整大小以实现快速传输
- 社交媒体准备:裁剪为平台特定的宽高比
何时不使用此技能 — 改用 bria-ai
此技能仅处理确定性像素级操作。对于任何生成式或AI驱动的图像工作,请改用 bria-ai 技能:
- - 从文本提示生成图像 → 使用 bria-ai
- AI背景移除或替换 → 使用 bria-ai
- AI图像编辑(修复、对象移除/添加) → 使用 bria-ai
- 风格迁移或AI驱动的视觉效果 → 使用 bria-ai
- 使用AI创建产品生活场景图 → 使用 bria-ai
- 使用AI超分辨率放大图像 → 使用 bria-ai
经验法则:如果任务需要创建新的视觉内容或理解图像语义,使用 bria-ai。如果任务需要转换现有像素(调整大小、裁剪、格式转换、水印),使用此技能。
如果 bria-ai 不可用,请使用以下命令安装:
bash
npx skills add bria-ai/bria-skill
快速参考
| 操作 | 方法 | 描述 |
|---|
| 加载 | load(source) | 从URL、路径、字节或base64加载 |
|
load
fromurl(url) | 从URL下载图像 |
|
保存 | save(image, path) | 自动检测格式保存 |
| | to_bytes(image, format) | 转换为字节 |
| | to_base64(image, format) | 转换为base64字符串 |
|
调整大小 | resize(image, width, height) | 调整为精确尺寸 |
| | scale(image, factor) | 按比例缩放(0.5 = 一半) |
| | thumbnail(image, size) | 适配尺寸,保持宽高比 |
|
裁剪 | crop(image, left, top, right, bottom) | 裁剪到指定区域 |
| | crop_center(image, width, height) | 从中心裁剪 |
| | crop
toaspect(image, ratio) | 按宽高比裁剪 |
|
合成 | paste(bg, fg, position) | 在坐标处叠加 |
| | composite(bg, fg, mask) | Alpha合成 |
| | fit
tocanvas(image, w, h) | 适配到画布尺寸 |
|
边框 | add_border(image, width, color) | 添加实线边框 |
| | add_padding(image, padding) | 添加空白边距 |
|
变换 | rotate(image, angle) | 按角度旋转 |
| | flip_horizontal(image) | 水平镜像 |
| | flip_vertical(image) | 垂直翻转 |
|
水印 | add
textwatermark(image, text) | 添加文字叠加 |
| | add
imagewatermark(image, logo) | 添加Logo水印 |
|
调整 | adjust_brightness(image, factor) | 变亮/变暗 |
| | adjust_contrast(image, factor) | 调整对比度 |
| | adjust_saturation(image, factor) | 调整色彩饱和度 |
| | blur(image, radius) | 应用高斯模糊 |
|
网页 | optimize
forweb(image, max_size) | 优化传输 |
|
信息 | get_info(image) | 获取尺寸、格式、模式 |
环境要求
bash
pip install Pillow requests
基本用法
python
from image_utils import ImageUtils
从URL加载
image = ImageUtils.load
fromurl(https://example.com/image.jpg)
或从多种来源加载
image = ImageUtils.load(/path/to/image.png) # 文件路径
image = ImageUtils.load(image_bytes) # 字节
image = ImageUtils.load(data:image/png;base64,...) # Base64
调整大小并保存
resized = ImageUtils.resize(image, width=800, height=600)
ImageUtils.save(resized, output.webp, quality=90)
获取图像信息
info = ImageUtils.get_info(image)
print(f{info[width]}x{info[height]} {info[mode]})
调整大小与缩放
python
调整为精确尺寸
resized = ImageUtils.resize(image, width=800, height=600)
保持宽高比调整大小(适配边界内)
fitted = ImageUtils.resize(image, width=800, height=600, maintain_aspect=True)
仅按宽度调整大小(高度自动计算)
resized = ImageUtils.resize(image, width=800)
按比例缩放
half = ImageUtils.scale(image, 0.5) # 50% 大小
double = ImageUtils.scale(image, 2.0) # 200% 大小
创建缩略图
thumb = ImageUtils.thumbnail(image, (150, 150))
裁剪
python
裁剪到特定区域
cropped = ImageUtils.crop(image, left=100, top=50, right=500, bottom=350)
从中心裁剪
center = ImageUtils.crop_center(image, width=400, height=400)
按宽高比裁剪(用于社交媒体)
square = ImageUtils.crop
toaspect(image, 1:1) # Instagram
wide = ImageUtils.crop
toaspect(image, 16:9) # YouTube缩略图
story = ImageUtils.crop
toaspect(image, 9:16) # 故事/短视频
控制裁剪锚点
top
crop = ImageUtils.cropto_aspect(image, 16:9, anchor=top)
bottom
crop = ImageUtils.cropto_aspect(image, 16:9, anchor=bottom)
合成
python
将前景粘贴到背景上
result = ImageUtils.paste(background, foreground, position=(100, 50))
Alpha合成(前景必须具有透明度)
result = ImageUtils.composite(background, foreground)
将图像适配到画布并添加黑边
canvas = ImageUtils.fit
tocanvas(
image,
width=1200,
height=800,
background_color=(255, 255, 255, 255), # 白色
position=center # 或 top、bottom
)
格式转换
python
转换为不同格式
png
bytes = ImageUtils.tobytes(image, PNG)
jpeg
bytes = ImageUtils.tobytes(image, JPEG, quality=85)
webp
bytes = ImageUtils.tobytes(image, WEBP, quality=90)
获取base64用于数据URL
base64
str = ImageUtils.tobase64(image, PNG)
data
url = ImageUtils.tobase64(image, PNG, include
dataurl=True)
返回: data:image/png;base64,...
根据扩展名自动检测格式保存
ImageUtils.save(image, output.png)
ImageUtils.save(image, output.jpg, quality=85)
ImageUtils.save(image, output.webp, quality=90)
水印
python
文字水印
watermarked = ImageUtils.add
textwatermark(
image,
text=© 2024 我的公司,
position=bottom-right, # bottom-left, top-right, top-left, center
font_size=24,
color=(255, 255, 255, 128), # 半透明白色
margin=20
)
Logo/图像水印
logo = ImageUtils.load(logo.png)
watermarked = ImageUtils.add
imagewatermark(
image,
watermark=logo,
position=bottom-right,
opacity=0.5,
scale=0.15, # 图像宽度的15%
margin=20
)
调整
python
亮度(1.0 = 原始,<1 变暗,>1 变亮)
bright = ImageUtils.adjust_brightness(image, 1.3)
dark = ImageUtils.adjust_brightness(image, 0.7)
对比度(1.0 = 原始)
high
contrast = ImageUtils.adjustcontrast(image