参考:
用python发一张高逼格朋友圈
附:python3.6.4+sublime Text安装及配置
点击打开链接
这个编译器真是相见恨晚啊啊啊啊啊
from PIL import Image
import sys
def fill_image(image):
width,height=image.size
print(width,height)
new_image_length=width if width>height else height
print(new_image_length)
#new_image = Image.new(image.mode, (new_image_length, new_image_length), color='white')
new_image=Image.new(image.mode,(new_image_length,new_image_length),color='white')
if width>height:
new_image.paste(image,(0,int((new_image_length-height)/2)))
else:
new_image.paste(image,(int((new_image_length-width)/2),0))
return new_image
def cut_image(image):
width,height=image.size
item_width=int(width/3)
box_list=[]
count=0
for j in range(0,3):
for i in range(0,3):
count+=1
box=(i*item_width,j*item_width,(i+1)*item_width,(j+1)*item_width)
box_list.append(box)
print(count)
image_list=[image.crop(box) for box in box_list]
return image_list
def save_images(image_list):
index=1
for image in image_list:
image.save('result/'+str(index)+'.png')
index+=1
if __name__ == '__main__':
file_path="001.jpg"
#打开图像
image=Image.open(file_path)
#将图像转为正方形,不够的地方补充为白色底色
image=fill_image(image)
#分为图像
image_list=cut_image(image)
#保存图像
save_images(image_list)
结果:
本文介绍如何使用Python进行图像处理,通过代码演示将一张图片分割成9个等份的小图,适合用于创建九宫格效果或进一步的图像分析。

1689

被折叠的 条评论
为什么被折叠?



