├── README.md ├── bigdataqianyan.png ├── cut_image.py ├── dcapi.jpeg ├── doc ├── WechatIMG198.png ├── WechatIMG199.png └── 二维码.png ├── python.jpeg └── result ├── 1.png ├── 2.png ├── 3.png ├── 4.png ├── 5.png ├── 6.png ├── 7.png ├── 8.png ├── 9.png ├── python1.png ├── python2.png ├── python3.png ├── python4.png ├── python5.png ├── python6.png ├── python7.png ├── python8.png └── python9.png /README.md: -------------------------------------------------------------------------------- 1 | 2 | # cut_image 3 | ### 使用python PIL 库将一张照片切分为9张图 4 | 5 | ## 举例 6 | #### 例子1:不规则图形,先填充白边再切分 7 | ##### 原图: 8 | ![python](https://github.com/yangxuan0928/cut_image/blob/master/python.jpeg) 9 | 10 | ##### 进行切图后: 11 | 12 | 13 | 14 | 15 | 16 | 17 | 18 | 19 | 20 | 21 | 22 | 23 | 24 | 25 | 26 |
27 | 28 | #### 例子2:其他照片 29 | ##### 原图: 30 | ![python](https://github.com/yangxuan0928/cut_image/blob/master/dcapi.jpeg) 31 | 32 | ##### 进行切图后: 33 | 34 | 35 | 36 | 37 | 38 | 39 | 40 | 41 | 42 | 43 | 44 | 45 | 46 | 47 | 48 |
49 | 50 | ## 依赖 51 | 52 | #### Python 2.7 53 | PIL: pip install pillow 54 | 55 | #### Python 3 56 | PIL: pip3 install pillow 57 | 58 | ## 运行 59 | 60 | python3 cut_pic.py 61 | 62 | ## 用处 63 | 可以发一个高逼格的朋友圈 64 | 65 | 66 | 67 | ## 推荐 68 | 查看文档及视频教程,请关注微信公众号:**大数据前沿** 消息窗口回复:**微信切图** 69 | 70 | 扫码下方二维码关注,更多好玩的项目及干货等着你。 71 | 72 | 73 | 74 | -------------------------------------------------------------------------------- /bigdataqianyan.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/yangxuan0928/cut_image/3d45a4e060834f4308d38e0fc80057a4936cf920/bigdataqianyan.png -------------------------------------------------------------------------------- /cut_image.py: -------------------------------------------------------------------------------- 1 | # -*- coding: utf-8 -*- 2 | ''' 3 | 将一张图片填充为正方形后切为9张图 4 | Author:微信公众号:大数据前沿 5 | 教程与文档:关注微信公众号 大数据前沿 回复 微信切图 获取。 6 | ''' 7 | from PIL import Image 8 | import sys 9 | 10 | #将图片填充为正方形 11 | def fill_image(image): 12 | width, height = image.size 13 | #选取长和宽中较大值作为新图片的 14 | new_image_length = width if width > height else height 15 | #生成新图片[白底] 16 | new_image = Image.new(image.mode, (new_image_length, new_image_length), color='white') 17 | #将之前的图粘贴在新图上,居中 18 | if width > height:#原图宽大于高,则填充图片的竖直维度 19 | new_image.paste(image, (0, int((new_image_length - height) / 2)))#(x,y)二元组表示粘贴上图相对下图的起始位置 20 | else: 21 | new_image.paste(image, (int((new_image_length - width) / 2),0)) 22 | return new_image 23 | 24 | #切图 25 | def cut_image(image): 26 | width, height = image.size 27 | item_width = int(width / 3) 28 | box_list = [] 29 | # (left, upper, right, lower) 30 | for i in range(0,3): 31 | for j in range(0,3): 32 | #print((i*item_width,j*item_width,(i+1)*item_width,(j+1)*item_width)) 33 | box = (j*item_width,i*item_width,(j+1)*item_width,(i+1)*item_width) 34 | box_list.append(box) 35 | 36 | image_list = [image.crop(box) for box in box_list] 37 | 38 | return image_list 39 | 40 | #保存 41 | def save_images(image_list): 42 | index = 1 43 | for image in image_list: 44 | image.save('./result/python'+str(index) + '.png', 'PNG') 45 | index += 1 46 | 47 | 48 | 49 | if __name__ == '__main__': 50 | file_path = "python.jpeg" 51 | image = Image.open(file_path) 52 | #image.show() 53 | image = fill_image(image) 54 | image_list = cut_image(image) 55 | save_images(image_list) 56 | 57 | 58 | -------------------------------------------------------------------------------- /dcapi.jpeg: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/yangxuan0928/cut_image/3d45a4e060834f4308d38e0fc80057a4936cf920/dcapi.jpeg -------------------------------------------------------------------------------- /doc/WechatIMG198.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/yangxuan0928/cut_image/3d45a4e060834f4308d38e0fc80057a4936cf920/doc/WechatIMG198.png -------------------------------------------------------------------------------- /doc/WechatIMG199.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/yangxuan0928/cut_image/3d45a4e060834f4308d38e0fc80057a4936cf920/doc/WechatIMG199.png -------------------------------------------------------------------------------- /doc/二维码.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/yangxuan0928/cut_image/3d45a4e060834f4308d38e0fc80057a4936cf920/doc/二维码.png -------------------------------------------------------------------------------- /python.jpeg: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/yangxuan0928/cut_image/3d45a4e060834f4308d38e0fc80057a4936cf920/python.jpeg -------------------------------------------------------------------------------- /result/1.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/yangxuan0928/cut_image/3d45a4e060834f4308d38e0fc80057a4936cf920/result/1.png -------------------------------------------------------------------------------- /result/2.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/yangxuan0928/cut_image/3d45a4e060834f4308d38e0fc80057a4936cf920/result/2.png -------------------------------------------------------------------------------- /result/3.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/yangxuan0928/cut_image/3d45a4e060834f4308d38e0fc80057a4936cf920/result/3.png -------------------------------------------------------------------------------- /result/4.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/yangxuan0928/cut_image/3d45a4e060834f4308d38e0fc80057a4936cf920/result/4.png -------------------------------------------------------------------------------- /result/5.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/yangxuan0928/cut_image/3d45a4e060834f4308d38e0fc80057a4936cf920/result/5.png -------------------------------------------------------------------------------- /result/6.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/yangxuan0928/cut_image/3d45a4e060834f4308d38e0fc80057a4936cf920/result/6.png -------------------------------------------------------------------------------- /result/7.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/yangxuan0928/cut_image/3d45a4e060834f4308d38e0fc80057a4936cf920/result/7.png -------------------------------------------------------------------------------- /result/8.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/yangxuan0928/cut_image/3d45a4e060834f4308d38e0fc80057a4936cf920/result/8.png -------------------------------------------------------------------------------- /result/9.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/yangxuan0928/cut_image/3d45a4e060834f4308d38e0fc80057a4936cf920/result/9.png -------------------------------------------------------------------------------- /result/python1.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/yangxuan0928/cut_image/3d45a4e060834f4308d38e0fc80057a4936cf920/result/python1.png -------------------------------------------------------------------------------- /result/python2.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/yangxuan0928/cut_image/3d45a4e060834f4308d38e0fc80057a4936cf920/result/python2.png -------------------------------------------------------------------------------- /result/python3.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/yangxuan0928/cut_image/3d45a4e060834f4308d38e0fc80057a4936cf920/result/python3.png -------------------------------------------------------------------------------- /result/python4.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/yangxuan0928/cut_image/3d45a4e060834f4308d38e0fc80057a4936cf920/result/python4.png -------------------------------------------------------------------------------- /result/python5.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/yangxuan0928/cut_image/3d45a4e060834f4308d38e0fc80057a4936cf920/result/python5.png -------------------------------------------------------------------------------- /result/python6.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/yangxuan0928/cut_image/3d45a4e060834f4308d38e0fc80057a4936cf920/result/python6.png -------------------------------------------------------------------------------- /result/python7.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/yangxuan0928/cut_image/3d45a4e060834f4308d38e0fc80057a4936cf920/result/python7.png -------------------------------------------------------------------------------- /result/python8.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/yangxuan0928/cut_image/3d45a4e060834f4308d38e0fc80057a4936cf920/result/python8.png -------------------------------------------------------------------------------- /result/python9.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/yangxuan0928/cut_image/3d45a4e060834f4308d38e0fc80057a4936cf920/result/python9.png --------------------------------------------------------------------------------