python图像裁剪
【代码】python图像裁剪。
·
from PIL import Image
def process(input_path,output_path,size=(480,640),dpi=(300,300)):
ratio=size[0]/size[1]
img0=Image.open(input_path)
ratio_0=img0.size[0]/img0.size[1]
if ratio<ratio_0: #如果宽度相对较大
temp_size=(size[1]*ratio_0,size[1])
elif ratio>ratio_0: #如果长度相对较大
temp_size=(size[0],size[0]*ratio_0)
else: #如果长宽比刚好相等
img2=img0.resize(size=size)
img2.save(output_path,dpi=dpi)
return #保存并退出
temp_size=[round(c) for c in temp_size]
img2=img0.resize(temp_size)
left=round( (temp_size[0]-size[0])/2)
top=round( (temp_size[1]-size[1])/2)
right=left+size[0]
bottom=top+size[1]
#裁剪
img2=img2.crop((left,top,right,bottom))
img2.save(output_path,dpi=dpi)
魔乐社区(Modelers.cn) 是一个中立、公益的人工智能社区,提供人工智能工具、模型、数据的托管、展示与应用协同服务,为人工智能开发及爱好者搭建开放的学习交流平台。社区通过理事会方式运作,由全产业链共同建设、共同运营、共同享有,推动国产AI生态繁荣发展。
更多推荐


所有评论(0)