发现Stable Diffusion Webui,Lora-scripts之类的还是不够智能,预处理阶段有这样的需求。目标很简单,把一个超大的图缩小成正方向(如1024*1024),并保留短的一边,长的一边剪裁,并且剪裁过程居中。
问了一下文心一言,基本在它的代码上改了一下:
首先:
pip install Pillow
其次代码如下:
from PIL import Image
import os
size=(1024,1024)
Image.MAX_IMAGE_PIXELS = None
old_path='test'
new_path='new'
counter=0
for filename in os.listdir(old_path):
if filename.endswith('.jpg'):
with Image.open(os.path.join(old_path, filename)) as img:
img_width, img_height=img.size
ratio_w=size[0]/img_width
ratio_h=size[1]/img_height
ratio=max(ratio_w,ratio_h)
# Calculate the new image size and position
new_width = int(img_width * ratio)
new_height = int(img_heigh