PS注意:不少人学Python过程当中会遇到各类烦恼问题,没有人帮答疑容易放弃。为此小编建了个Python全栈免费答疑.裙 :七衣衣九七七巴而五(数字的谐音)转换下能够找到了,不懂的问题有老司机解决里面还有最新Python教程项目可拿,,一块儿相互监督共同进步!python
原图以下:网络
Imageapp
咱们想要利用这张图制做高逼格的九宫格朋友圈分享。学习
达到相似于这样的效果:3d
实现原理很是简单,那就是利用PIL库对原图不断画小区域而后切下来存储成新的小图片。code
假设每个格子的宽和高分别是w、h,那么第row行(从0开始计数),第col列(从0开始计数)的格子左上角坐标和右下角坐标分别是(col * w, row * h),(col * w + w, r * h + h)。orm
code snippet:教程
#! /usr/local/bin/python3图片
# -*- coding: utf-8 -*-ip
fromPILimportImage
defcut_image(image):
width, height = image.size
item_width = width /3.0
item_height = height /3.0
box_list = []
forrowinrange(0,3):
forcolinrange(0,3):
box = (col * item_width, row * item_height,( col +1) * item_width,( row +1) * item_height)
box_list.append( box )
image_list = [image.crop(box)forboxinbox_list]
returnimage_list
defsave_images(image_list):
dirName ='output'
ifFalse== os.path.exists( dirName ):
os.makedirs( dirName )
index =1
forimageinimage_list:
image.save('./output/python'+str(index) +'.png','PNG')
index +=1
if__name__ =='__main__':
image = Image.open("use.png")
image_list = cut_image(image)
save_images(image_list)
为了能在朋友圈中预览时看到全部图片的完整样子,建议保证本身的原始图片是正方形的,而后再运行这个脚本,在output中获得九张图片。最后,嗯,就能够去秀了!
注意:不少人学Python过程当中会遇到各类烦恼问题,没有人帮答疑容易放弃。为此小编建了个Python全栈免费答疑.裙 :七衣衣九七七巴而五(数字的谐音)转换下能够找到了,不懂的问题有老司机解决里面还有最新Python教程项目可拿,,一块儿相互监督共同进步!本文的文字及图片来源于网络加上本身的想法,仅供学习、交流使用,不具备任何商业用途,版权归原做者全部,若有问题请及时联系咱们以做处理。