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循环监控远程端口的方法
Mar 14 Python
Python中字典的基本知识初步介绍
May 21 Python
使用pdb模块调试Python程序实例
Jun 02 Python
Python生成随机验证码的两种方法
Dec 22 Python
python中异常捕获方法详解
Mar 03 Python
python爬虫基本知识
Mar 05 Python
python opencv设置摄像头分辨率以及各个参数的方法
Apr 02 Python
Python数据分析库pandas基本操作方法
Apr 08 Python
python基于socket进行端口转发实现后门隐藏的示例
Jul 25 Python
python将字符串转变成dict格式的实现
Nov 18 Python
python爬虫破解字体加密案例详解
Mar 02 Python
Python list列表删除元素的4种方法
Nov 01 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
PHP 高手之路(二)
2006/10/09 PHP
php Calender(日历)代码分享
2014/01/03 PHP
ThinkPHP让分页保持搜索状态的方法
2014/07/02 PHP
smarty内置函数section的用法
2015/01/22 PHP
TNC vs BOOM BO3 第三场2.13
2021/03/10 DOTA
ie和firefox中img对象区别的困惑
2006/12/27 Javascript
js 在定义的时候立即执行的函数表达式(function)写法
2013/01/16 Javascript
jquery sortable的拖动方法示例详解
2014/01/16 Javascript
jquery $.trim()方法使用介绍
2014/05/21 Javascript
基于JavaScript代码实现微信扫一扫下载APP
2015/12/30 Javascript
jQuery基于$.ajax设置移动端click超时处理方法
2016/05/14 Javascript
Javascript 5种方法实现过滤删除前后所有空格
2016/06/22 Javascript
JS实现根据密码长度显示安全条功能
2017/03/08 Javascript
javascript修改浏览器title方法 JS动态修改浏览器标题
2017/11/30 Javascript
浅谈React前后端同构防止重复渲染
2018/01/05 Javascript
js中DOM事件绑定分析
2018/03/18 Javascript
vue检测对象和数组的变化分析
2018/06/30 Javascript
Vue的transition-group与Virtual Dom Diff算法的使用
2019/12/09 Javascript
快速解决element的autofocus失效问题
2020/09/08 Javascript
python中的yield使用方法
2014/02/11 Python
详解python中 os._exit() 和 sys.exit(), exit(0)和exit(1) 的用法和区别
2017/06/23 Python
Android基于TCP和URL协议的网络编程示例【附demo源码下载】
2018/01/23 Python
Python判断一个三位数是否为水仙花数的示例
2018/11/13 Python
Python数据结构之栈、队列及二叉树定义与用法浅析
2018/12/27 Python
python 去除二维数组/二维列表中的重复行方法
2019/01/23 Python
python实现名片管理系统项目
2019/04/26 Python
python wxpython 实现界面跳转功能
2019/12/17 Python
美国潜水装备、水肺潜水和浮潜设备商店:Leisure Pro
2018/08/08 全球购物
无故旷工检讨书
2014/01/26 职场文书
教师一岗双责责任书
2014/04/16 职场文书
三月学雷锋活动总结
2014/06/26 职场文书
会计工作总结范文2014
2014/12/23 职场文书
关于五一放假的通知
2015/08/18 职场文书
SpringBoot读取Resource下文件的4种方法
2021/07/02 Java/Android
MySQL空间数据存储及函数
2021/09/25 MySQL
向Spring IOC 容器动态注册bean实现方式
2022/07/15 Java/Android