You should often see people posting pictures of Jiugongge in your circle of friends. The essence is to cut a picture into nine parts, and then post these nine pictures together on WeChat.
FIG Speaking cut, Python can be achieved, is mainly used Python library Pillow, installation pip install pillow
to the main cutting step of FIG follows:
-
Open the picture to be processed
-
Determine whether the opened picture is square
-
If it’s a square, divide it into nine. If it’s not a square, fill it with white to form a square, and then divide it into nine.
-
Save the processed picture
The main implementation code is as follows:
# 填充新的 image
def fill_image(image):
width, height = image.size
_length = width
if height > width:
_length = height
new_image = Image.new(image.mode, (_length, _length), color='white')
if width > height:
new_image.paste(image, (0, int((_length - height) / 2)))
else:
new_image.paste(image, (int((_length - width) / 2), 0))
return new_image
# 裁剪 image
def cut_image(image):
width, height = image.size
_width = int(width / 3)
box_list = []
for i in range(0, 3):
for j in range(0, 3):
box = (j * _width, i * _width, (j + 1) * _width, (i + 1) * _width)
box_list.append(box)
image_list = [image.crop(box) for box in box_list]
return image_list
# 将 image 列表的里面的图片保存
def save_images(image_list, res_dir):
index = 1
if not os.path.exists(res_dir):
os.mkdir(res_dir)
for image in image_list:
new_name = os.path.join(res_dir, str(index) + '.png')
image.save(new_name, 'PNG')
index += 1
Original image:
Effect picture:
Welcome to Wechat to search for Python minor two , read and get the source code as soon as possible , and reply to the keyword 1024 to receive free personally organized various programming language learning materials.