gpt4 book ai didi

python生成九宫格图片

转载 作者:qq735679552 更新时间:2022-09-27 22:32:09 25 4
gpt4 key购买 nike

CFSDN坚持开源创造价值,我们致力于搭建一个资源共享平台,让每一个IT人在这里找到属于你的精彩世界.

这篇CFSDN的博客文章python生成九宫格图片由作者收集整理,如果你对这篇文章有兴趣,记得点赞哟.

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

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

效果大致是:

          。

库:pillow 。

源码:

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
# 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生成九宫格图片的文章就讲到这里了,如果你想了解更多关于python生成九宫格图片的内容请搜索CFSDN的文章或继续浏览相关文章,希望大家以后支持我的博客! 。

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com