用户提问: ai批量旋转
Ai回答: 在批量旋转图片或处理其他AI相关任务时,可以使用以下方法,具体取决于你的需求和技术栈:
1、批量旋转图片
使用Python + PIL/Pillow
python
from PIL import Image
import os
input_folder = input_images
output_folder = rotated_images
rotation_angle = 90 旋转角度(顺时针)
os.makedirs(output_folder, exist_ok=True)
for filename in os.listdir(input_folder):
if filename.endswith((.jpg, .png, .jpeg)):
img_path = os.path.join(input_folder, filename)
img = Image.open(img_path)
rotated_img = img.rotate(rotation_angle, expand=True) expand防止裁剪
rotated_img.save(os.path.join(output_folder, filename))
说明:
支持常见图片格式(JPG/PNG等)。
`expand=True` 确保旋转后图片不被裁剪。
使用OpenCV
python
import cv2
import os
input_folder = input_images
output_folder = rotated_images
angle = 90 旋转角度
os.makedirs(output_folder, exist_ok=True)
for filename in os.listdir(input_folder):
if filename.endswith((.jpg, .png)):
img_path = os.path.join(input_folder, filename)
img = cv2.imread(img_path)
height, width = img.shape[:2]
rotation_matrix = cv2.getRotationMatrix2D((width/2, height/2), angle, 1)
rotated_img = cv2.warpAffine(img, rotation_matrix, (width, height))
cv2.imwrite(os.path.join(output_folder, filename), rotated_img)
2、批量旋转PDF页面
使用PyMuPDF
python
import fitz PyMuPDF
input_pdf = input.pdf
output_pdf = rotated.pdf
rotation_angle = 90 必须是90的倍数
doc = fitz.open(input_pdf)
for page in doc:
page.set_rotation(rotation_angle)
doc.save(output_pdf)
3、其他AI批量处理场景
批量旋转目标检测的标注框(如YOLO格式)
python
import numpy as np
def rotate_bbox(bbox, img_width, img_height, angle):
x_center, y_center, w, h = bbox
if angle == 90:
new_x = y_center
new_y = 1 x_center
new_w, new_h = h, w
elif angle == 180:
new_x = 1 x_center
new_y = 1 y_center
else: 270度
new_x = 1 y_center
new_y = x_center
new_w, new_h = h, w
return [new_x, new_y, new_w, new_h]
4、使用命令行工具
ImageMagick(适用于图片):
bash
convert input.jpg -rotate 90 output.jpg
批量处理:
mogrify -rotate 90 -path rotated_images *.jpg
FFmpeg(适用于视频帧旋转):
bash
ffmpeg -i input.mp4 -vf transpose=1 rotated.mp4
注意事项
1、备份数据:批量操作前建议备份原文件。
2、角度方向:顺时针旋转通常为正角度,逆时针为负(根据库的约定)。
3、元数据保留:部分工具会丢失EXIF信息,需额外处理。
如果需要更复杂的AI处理(如基于内容的自动旋转校正),可以结合深度学习模型(如`AutoRotate`模型)。
0
IP地址: 87.198.221.2
搜索次数: 90
提问时间: 2025-04-29 00:28:46
热门提问:
ai男友推荐
ai健康问答
山河智能
画图用哪个ai
ai绘画 同人