python生成九宫格图片

作者:zenobia119 时间:2022-09-09 04:46:34 

本文实例为大家分享了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,后面再研究研究。

来源:https://blog.csdn.net/zenobia119/article/details/81840924

标签:python,九宫格
0
投稿

猜你喜欢

  • 小看了setTimeout()

    2009-12-04 12:44:00
  • python脚本实现验证码识别

    2022-02-03 09:41:30
  • Python 使用 PyQt5 开发的关机小工具分享

    2023-09-15 05:06:02
  • 解析xml字符串的函数

    2008-06-10 12:37:00
  • ASPJPEG组件使用详解(缩略图+水印)

    2007-09-19 17:31:00
  • linux服务器下PHPCMS v9 安全配置详解

    2023-11-17 11:51:52
  • php中session_id()函数详细介绍,会话id生成过程及session id长度

    2023-11-24 08:01:00
  • Python关于print的操作(倒计时、转圈显示、进度条)

    2022-08-19 07:26:58
  • 一文带你学会使用PHP接口

    2023-06-07 18:40:13
  • PDO::getAvailableDrivers讲解

    2023-06-08 22:24:20
  • 对python .txt文件读取及数据处理方法总结

    2022-08-04 02:36:07
  • 如何实现python爬虫爬取视频时实现实时进度条显示

    2022-07-01 04:17:12
  • 删除多余的属性 xmlns=""

    2010-08-24 18:41:00
  • 详解Python中表达式i += x与i = i + x是否等价

    2023-07-29 06:51:12
  • 网页代码中键盘操作相关标签教程

    2010-03-18 15:56:00
  • python处理csv数据的方法

    2023-03-08 19:05:31
  • Python使用pickle进行序列化和反序列化的示例代码

    2022-11-17 10:46:22
  • Python中Jieba进行词频统计与关键词提取

    2022-02-03 23:08:50
  • 自适应网页设计(Responsive Web Design)

    2012-05-02 10:49:07
  • Python 基于Twisted框架的文件夹网络传输源码

    2021-03-19 03:01:00
  • asp之家 网络编程 m.aspxhome.com