python生成九宫格图片
2018-12-03 09:26
836 查看
本文实例为大家分享了Python九宫格图片生成的具体代码,供大家参考,具体内容如下
利用Image类将一张图片分割成9张,发朋友圈利器,打包成EXE后,长期使用。
效果大致是:
库:pillow
源码:
# pengyouquanPicture.py # 朋友圈九宫格图片制作 from PIL import Image import sys # 先将input image 填充为正方形 def fill_image(image): width, height = image.size #选取原图片长、宽中较大值作为新图片的九宫格半径 new_image_length = width if width > height else height #生产新图片【白底】 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-heigth) / 2))) else: new_image.paste(image,(int((new_image_length-width) / 2), 0)) return new_image # 将图片切割成九宫格 def cut_image(image): width, height = image.size #一行放3张图 item_width = int(width / 3) box_list = [] for i in range(0,3): for j in range(0,3): box = (j*item_width,i*item_width,(j+1)*item_width,(i+1)*item_width) box_list.append(box) 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(str(index) + '.png', 'PNG') index += 1 if __name__ == '__main__': file_path = "1.jpg" image = Image.open(file_path) #image.show() image = fill_image(image) image_list = cut_image(image) save_images(image_list)
打包EXE:
pyinstaller.exe -F pengyouquanPicture.py -i "b8.ico"
把EXE文件和要分割的图片放在一个路径下,人后图片重命名为1.jpg ,直接执行exe 就可以得到9张照片啦。
PS:怎么打包成APP,后面再研究研究。
以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持脚本之家。
您可能感兴趣的文章:
相关文章推荐
- Python 生成4位验证码图片
- 使用flash cs5生成九宫格图片
- 如何用python生成带图片名称和标签的.txt文件(代码)
- python3用pillow生成验证码,tornado中输出图片
- 原创:用python把链接指向的网页直接生成图片的http服务及网站(含源码及思想)
- python 根据数组生成图片
- Python matplotlib生成图片背景透明
- caffe下python接口的配置以及图片的生成
- 用python从excel表格中读取数据生成可以放在科技论文中的图片
- 原创:用python把链接指向的网页直接生成图片的http服务及网站(含源码及思想)
- Python matplotlib生成图片背景透明
- Python中判断图片格式并进行转换,将转换的图像保存到生成的文件夹中
- Pyhton每日练习 08 使用 Python 生成类似于下图中的字母+数字验证码图片
- Python3 Pillow生成简单验证码图片
- 【caffe-Windows】微软官方caffe之 Python接口配置及图片生成实例
- Python实现中文词云(wordcloud),根据背景图片生成词云
- Python图片处理PIL/pillow/生成验证码/出现KeyError: 和The _imagingft C module is not installed
- Python生成数字图片代码分享
- Python生成汉字字库文字,以及转换为文字图片
- python爬虫爬取指定用户微博图片及内容,并进行微博分类及使用习惯分析,生成可视化图表