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 相关文章推荐
推荐11个实用Python库
Jan 23 Python
Python中函数的参数定义和可变参数用法实例分析
Jun 04 Python
python利用paramiko连接远程服务器执行命令的方法
Oct 16 Python
python爬虫之BeautifulSoup 使用select方法详解
Oct 23 Python
python如何让类支持比较运算
Mar 20 Python
全面了解django的缓存机制及使用方法
Jul 22 Python
Python3读写Excel文件(使用xlrd,xlsxwriter,openpyxl3种方式读写实例与优劣)
Feb 13 Python
python3中calendar返回某一时间点实例讲解
Nov 18 Python
python数据库批量插入数据的实现(executemany的使用)
Apr 30 Python
用python实现监控视频人数统计
May 21 Python
Pandas搭配lambda组合使用详解
Jan 22 Python
深入理解pytorch库的dockerfile
Jun 10 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
《星际争霸2》终章已出 RTS时代宣告终结
2017/02/07 星际争霸
如何在PHP中使用Oracle数据库(3)
2006/10/09 PHP
实用PHP会员权限控制实现原理分析
2011/05/29 PHP
PHP伪静态Rewrite设置之APACHE篇
2014/07/30 PHP
PHP利用超级全局变量$_GET来接收表单数据的实例
2016/11/05 PHP
php魔法函数与魔法常量使用介绍
2017/07/23 PHP
ExtJs 3.1 XmlTreeLoader Example Error
2010/02/09 Javascript
Html中JS脚本执行顺序简单举例说明
2010/06/19 Javascript
js实现iGoogleDivDrag模块拖动层拖动特效的方法
2015/03/04 Javascript
javascript数组去重方法分析
2016/12/15 Javascript
令按钮悬浮在(手机)页面底部的实现方法
2017/05/02 Javascript
封装运动框架实战左右与上下滑动的焦点轮播图(实例)
2017/10/17 Javascript
webpack 模块热替换原理
2018/04/09 Javascript
vue interceptor 使用教程实例详解
2018/09/13 Javascript
javascript实现导航栏分页效果
2019/06/27 Javascript
jQuery实现轮播图源码
2019/10/23 jQuery
vue中的 $slot 获取插槽的节点实例
2019/11/12 Javascript
vue-video-player 解决微信自动全屏播放问题(横竖屏导致样式错乱问题)
2020/02/25 Javascript
JavaScript基于用户照片姓名生成海报
2020/05/29 Javascript
jQuery实现动态加载瀑布流
2020/09/01 jQuery
编写Python脚本批量下载DesktopNexus壁纸的教程
2015/05/06 Python
Python pickle模块用法实例分析
2015/05/27 Python
CentOS中使用virtualenv搭建python3环境
2015/06/08 Python
Python 读取指定文件夹下的所有图像方法
2018/04/27 Python
python smtplib发送带附件邮件小程序
2018/05/22 Python
Python数据可视化之画图
2019/01/15 Python
对python For 循环的三种遍历方式解析
2019/02/01 Python
Python 实现微信防撤回功能
2019/04/29 Python
python模拟斗地主发牌
2020/04/22 Python
中国专业的综合网上购物商城:京东
2016/08/02 全球购物
新闻学毕业生自荐信
2013/11/15 职场文书
2014年财务人员工作总结
2014/11/11 职场文书
导游词怎么写
2015/02/04 职场文书
班干部学习委员竞选稿
2015/11/20 职场文书
关于python pygame游戏进行声音添加的技巧
2021/10/24 Python
SQL CASE 表达式的具体使用
2022/03/21 SQL Server