python生成九宫格图片


Posted in Python onNovember 19, 2018

本文实例为大家分享了Python九宫格图片生成的具体代码,供大家参考,具体内容如下

利用Image类将一张图片分割成9张,发朋友圈利器,打包成EXE后,长期使用。

效果大致是:

python生成九宫格图片python生成九宫格图片

库: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"

python生成九宫格图片

把EXE文件和要分割的图片放在一个路径下,人后图片重命名为1.jpg ,直接执行exe 就可以得到9张照片啦。

PS:怎么打包成APP,后面再研究研究。

以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持三水点靠木。

Python 相关文章推荐
python高并发异步服务器核心库forkcore使用方法
Nov 26 Python
Python脚本简单实现打开默认浏览器登录人人和打开QQ的方法
Apr 12 Python
Python 专题五 列表基础知识(二维list排序、获取下标和处理txt文本实例)
Mar 20 Python
运用Python的webbrowser实现定时打开特定网页
Feb 21 Python
Python人工智能之路 jieba gensim 最好别分家之最简单的相似度实现
Aug 13 Python
python2使用bs4爬取腾讯社招过程解析
Aug 14 Python
python 扩展print打印文件路径和当前时间信息的实例代码
Oct 11 Python
利用Tensorboard绘制网络识别准确率和loss曲线实例
Feb 15 Python
15行Python代码实现免费发送手机短信推送消息功能
Feb 27 Python
通过实例了解python__slots__使用方法
Sep 14 Python
python绘制趋势图的示例
Sep 17 Python
Pytorch中expand()的使用(扩展某个维度)
Jul 15 Python
python实现简易动态时钟
Nov 19 #Python
python使用Turtle库绘制动态钟表
Nov 19 #Python
python+PyQT实现系统桌面时钟
Jun 16 #Python
Windows 8.1 64bit下搭建 Scrapy 0.22 环境
Nov 18 #Python
Window环境下Scrapy开发环境搭建
Nov 18 #Python
Python中安装easy_install的方法
Nov 18 #Python
win7 x64系统中安装Scrapy的方法
Nov 18 #Python
You might like
对Session和Cookie的区分与解释
2007/03/16 PHP
discuz Passport 通行证 整合笔记
2008/06/30 PHP
php中利用str_pad函数生成数字递增形式的产品编号
2013/09/30 PHP
php使用curl模拟登录后采集页面的例子
2013/11/04 PHP
PHP连接SQLServer2005方法及代码
2013/12/26 PHP
Yii实现显示静态页的方法
2016/04/25 PHP
header与缓冲区之间的深层次分析
2016/07/30 PHP
Yii2框架实现登陆添加验证码功能示例
2018/07/12 PHP
Jquery下:nth-child(an+b)的使用注意
2011/05/28 Javascript
JavaScript中json对象和string对象之间相互转化
2012/12/26 Javascript
IE7浏览器窗口大小改变事件执行多次bug及IE6/IE7/IE8下resize问题
2015/08/21 Javascript
JavaScript函数柯里化详解
2016/04/29 Javascript
微信小程序加载更多 点击查看更多
2016/11/29 Javascript
js获取浏览器的各种属性
2017/04/27 Javascript
Node.js中DNS模块学习总结
2018/02/28 Javascript
浅谈JavaScript 代码整洁之道
2018/10/23 Javascript
react native 原生模块桥接的简单说明小结
2019/02/26 Javascript
[01:04:31]DOTA2-DPC中国联赛定级赛 iG vs Magma BO3第二场 1月8日
2021/03/11 DOTA
用python写一个windows下的定时关机脚本(推荐)
2017/03/21 Python
基于pandas将类别属性转化为数值属性的方法
2018/07/25 Python
Python从ZabbixAPI获取信息及实现Zabbix-API 监控的方法
2018/09/17 Python
python实现邮件发送功能
2019/08/10 Python
tensorflow之tf.record实现存浮点数数组
2020/02/17 Python
Django实现将一个字典传到前端显示出来
2020/04/03 Python
Python利用myqr库创建自己的二维码
2020/11/24 Python
高一历史教学反思
2014/01/13 职场文书
经销商培训邀请函
2014/01/21 职场文书
2014全国两会学习心得体会1000字
2014/03/10 职场文书
班子群众路线教育实践个人对照检查材料思想汇报
2014/09/30 职场文书
党支部先进事迹材料
2014/12/24 职场文书
营销计划书
2015/01/17 职场文书
退休欢送会致辞
2015/07/31 职场文书
多表查询、事务、DCL
2021/04/05 MySQL
go语言中json数据的读取和写出操作
2021/04/28 Golang
MySql新手入门的基本操作汇总
2021/05/13 MySQL
JavaWeb Servlet实现网页登录功能
2021/07/04 Java/Android