发布时间2025-05-01 19:11
针对海外代购商品图片的缩放和旋转需求,以下是结合图像处理工具及编程技术的综合解决方案,涵盖手动操作与自动化批量处理两种方式:
python
from PIL import Image
缩放
img = Image.open("product.jpg")
resized_img = img.resize((200, 200)) 指定目标尺寸
resized_img.save("resized_product.jpg")
旋转(自动扩展画布)
rotated_img = img.rotate(45, expand=True) expand参数避免裁剪
rotated_img.save("rotated_product.jpg")
python
rotated_img = img.rotate(30, center=(0, 0)) 需手动计算扩展后的画布
python
import cv2
缩放(保持宽高比)
img = cv2.imread("product.jpg")
scale_percent = 50 缩小50%
width = int(img.shape[1] scale_percent/100)
height = int(img.shape[0] scale_percent/100)
resized_img = cv2.resize(img, (width, height))
旋转(计算旋转矩阵)
(h, w) = img.shape[:2]
center = (w//2, h//2)
M = cv2.getRotationMatrix2D(center, 45, 1.0) 45度,缩放因子1.0
rotated_img = cv2.warpAffine(img, M, (w, h))
cv2.imwrite("rotated_opencv.jpg", rotated_img)
1. 保持比例:缩放时锁定宽高比,避免商品图片拉伸变形(如电商平台主图要求)。
2. 文件格式与压缩:保存为JPEG时调整质量参数(如 `quality=85`),平衡清晰度与文件大小。
3. 背景处理:旋转后若需透明背景,可导出为PNG格式,或在代码中设置 `fill_background` 参数。
4. 元数据保留:处理时注意保留EXIF信息(如商品拍摄参数),避免丢失关键数据。
通过以上方法,可灵活应对海外代购商品图片的缩放、旋转及批量处理需求,兼顾效率与专业性。具体实现时需根据实际场景选择工具组合。
更多代购