1. 程式人生 > >python——將一張影象分割為九張

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)
結果: