Files
IOPaint/iopaint/tests/test_save_quality.py
let5sne 1b87a98261 🎨 完整的 IOPaint 项目更新
## 主要更新
-  更新所有依赖到最新稳定版本
- 📝 添加详细的项目文档和模型推荐
- 🔧 配置 VSCode Cloud Studio 预览功能
- 🐛 修复 PyTorch API 弃用警告

## 依赖更新
- diffusers: 0.27.2 → 0.35.2
- gradio: 4.21.0 → 5.46.0
- peft: 0.7.1 → 0.18.0
- Pillow: 9.5.0 → 11.3.0
- fastapi: 0.108.0 → 0.116.2

## 新增文件
- CLAUDE.md - 项目架构和开发指南
- UPGRADE_NOTES.md - 详细的升级说明
- .vscode/preview.yml - 预览配置
- .vscode/LAUNCH_GUIDE.md - 启动指南
- .gitignore - 更新的忽略规则

## 代码修复
- 修复 iopaint/model/ldm.py 中的 torch.cuda.amp.autocast() 弃用警告

## 文档更新
- README.md - 添加模型推荐和使用指南
- 完整的项目源码(iopaint/)
- Web 前端源码(web_app/)

🤖 Generated with Claude Code
2025-11-28 17:10:24 +00:00

72 lines
2.3 KiB
Python

import os
import io
from PIL import Image
from iopaint.helper import pil_to_bytes
TESTS_DIR = os.path.dirname(os.path.abspath(__file__))
def test_jpeg_quality():
# Test JPEG quality settings
img_path = os.path.join(TESTS_DIR, "bunny.jpeg")
pil_img = Image.open(img_path)
# Test different quality settings
high_quality = pil_to_bytes(pil_img, "jpg", quality=95)
low_quality = pil_to_bytes(pil_img, "jpg", quality=50)
# Print file sizes in KB
print(f"High quality JPEG size: {len(high_quality) / 1024:.2f} KB")
print(f"Low quality JPEG size: {len(low_quality) / 1024:.2f} KB")
# Higher quality should result in larger file size
assert len(high_quality) > len(low_quality)
# Verify the output can be opened as an image
Image.open(io.BytesIO(high_quality))
Image.open(io.BytesIO(low_quality))
def test_png_parameters():
# Test PNG with parameters
img_path = os.path.join(TESTS_DIR, "cat.png")
pil_img = Image.open(img_path)
# Test PNG with parameters
params = {"parameters": "test_param=value"}
png_with_params = pil_to_bytes(pil_img, "png", infos=params)
# Test PNG without parameters
png_without_params = pil_to_bytes(pil_img, "png")
# Print file sizes in KB
print(f"PNG with parameters size: {len(png_with_params) / 1024:.2f} KB")
print(f"PNG without parameters size: {len(png_without_params) / 1024:.2f} KB")
# Verify both outputs can be opened as images
Image.open(io.BytesIO(png_with_params))
Image.open(io.BytesIO(png_without_params))
def test_format_conversion():
# Test format conversion
jpeg_path = os.path.join(TESTS_DIR, "bunny.jpeg")
png_path = os.path.join(TESTS_DIR, "cat.png")
# Convert JPEG to PNG
jpeg_img = Image.open(jpeg_path)
jpeg_to_png = pil_to_bytes(jpeg_img, "png")
converted_png = Image.open(io.BytesIO(jpeg_to_png))
print(f"JPEG to PNG size: {len(jpeg_to_png) / 1024:.2f} KB")
assert converted_png.format.lower() == "png"
# Convert PNG to JPEG
png_img = Image.open(png_path)
# Convert RGBA to RGB if necessary
if png_img.mode == "RGBA":
png_img = png_img.convert("RGB")
png_to_jpeg = pil_to_bytes(png_img, "jpg")
print(f"PNG to JPEG size: {len(png_to_jpeg) / 1024:.2f} KB")
converted_jpeg = Image.open(io.BytesIO(png_to_jpeg))
assert converted_jpeg.format.lower() == "jpeg"