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的Flask框架来构建一个简单的数字商品支付解决方案
Mar 31 Python
python中的全局变量用法分析
Jun 09 Python
对python 矩阵转置transpose的实例讲解
Apr 17 Python
浅谈Pandas 排序之后索引的问题
Jun 07 Python
详解PyCharm配置Anaconda的艰难心路历程
Aug 13 Python
Django框架首页和登录页分离操作示例
May 28 Python
python3.4+pycharm 环境安装及使用方法
Jun 13 Python
pandas DataFrame 交集并集补集的实现
Jun 24 Python
Pandas时间序列:时期(period)及其算术运算详解
Feb 25 Python
tensorflow使用CNN分析mnist手写体数字数据集
Jun 17 Python
Python实现Canny及Hough算法代码实例解析
Aug 06 Python
matplotlib实现数据实时刷新的示例代码
Jan 05 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
Linux下实现PHP多进程的方法分享
2012/08/16 PHP
微信支付PHP SDK ―― 公众号支付代码详解
2016/09/13 PHP
Laravel 5使用Laravel Excel实现Excel/CSV文件导入导出的功能详解
2017/10/11 PHP
javascript面向对象之Javascript 继承
2010/05/04 Javascript
JS定义回车事件(实现代码)
2013/07/08 Javascript
js中数组(Array)的排序(sort)注意事项说明
2014/01/24 Javascript
jquery解析xml字符串简单示例
2014/04/11 Javascript
jQuery源码解读之removeClass()方法分析
2015/02/20 Javascript
JS动态修改iframe高度和宽度的方法
2015/04/01 Javascript
JavaScript判断undefined类型的正确方法
2015/06/30 Javascript
基于jquery实现的树形菜单效果代码
2015/09/06 Javascript
Javascript实现通过选择周数显示开始日和结束日的实现代码
2016/05/30 Javascript
js获取当前页的URL与window.location.href简单方法
2017/02/13 Javascript
深入理解Angular中的依赖注入
2017/06/26 Javascript
详解webpack + vue + node 打造单页面(入门篇)
2017/09/23 Javascript
JS实现区分中英文并统计字符个数的方法示例
2018/06/09 Javascript
layui给下拉框、按钮状态、时间赋初始值的方法
2019/09/10 Javascript
基于Vue的侧边目录组件的实现
2020/02/05 Javascript
Bootstrap实现前端登录页面带验证码功能完整示例
2020/03/26 Javascript
python 获取网页编码方式实现代码
2017/03/11 Python
python SSH模块登录,远程机执行shell命令实例解析
2018/01/12 Python
对python 各种删除文件失败的处理方式分享
2018/04/24 Python
Python爬虫抓取代理IP并检验可用性的实例
2018/05/07 Python
python 查找文件名包含指定字符串的方法
2018/06/05 Python
python 读取修改pcap包的例子
2019/07/23 Python
Python使用tkinter模块实现推箱子游戏
2019/10/08 Python
wxPython色环电阻计算器
2019/11/18 Python
python 判断txt每行内容中是否包含子串并重新写入保存的实例
2020/03/12 Python
python编程的核心知识点总结
2021/02/08 Python
HTML5所有标签汇总及标签意义解释
2015/03/12 HTML / CSS
AmazeUi Tree(树形结构) 应用小结
2020/08/17 HTML / CSS
硕士学位论文评语
2014/12/31 职场文书
关于社会实践的心得体会(2016最新版)
2016/01/25 职场文书
个人落户申请书怎么写?
2019/06/28 职场文书
MySQL基础(一)
2021/04/05 MySQL
mysql 直接拷贝data 目录下文件还原数据的实现
2021/07/25 MySQL