赤壁网站建设,网站发布到ftp,四川省工程建设信息网站,网站权重划分大家都知道在微信朋友圈或者微博以及QQ动态中#xff0c;有很多“强迫症患者”发图片都爱发9张#xff0c;而有些图是一张图片分成的九宫图#xff0c;对于这种操作#xff0c;大家知道是怎么做到的吗#xff1f;
本文就是用Python做的一个九宫格图片生成器#xff0c;是…大家都知道在微信朋友圈或者微博以及QQ动态中有很多“强迫症患者”发图片都爱发9张而有些图是一张图片分成的九宫图对于这种操作大家知道是怎么做到的吗
本文就是用Python做的一个九宫格图片生成器是一个打包好的exe文件用户无需部署安装Python的开发环境在本地就可以运行此程序以此快速生成九宫格图片。
下面是程序的所有代码这是一个Python GUI程序代码不多也很容易理解
# -*- coding: UTF-8 -*-
# 将一张图片分成九张九宫格
import tkinter as tk
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), colorwhite) #注意这个函数
#将之前的图粘贴在新图上居中
if width height:#原图宽大于高则填充图片的竖直维度 #(x,y)二元组表示粘贴上图相对下图的起始位置,是个坐标点。
new_image.paste(image, (0, int((new_image_length - height) / 2)))
else:
new_image.paste(image, (int((new_image_length - width) / 2),0))
return new_image
# 分割图片
def cut_image(image):
width, height image.size
item_width int(width / 3) #因为朋友圈一行放3张图。
box_list []
# (left, upper, right, lower)
for i in range(0,3):
for j in range(0,3):
#print((i*item_width,j*item_width,(i1)*item_width,(j1)*item_width))
box (j*item_width,i*item_width,(j1)*item_width,(i1)*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
# 点击按钮实现图片分割
def cTofClicked():
file_pathstr(entryCd.get()) # 获取要进行分割的图片路径
image Image.open(file_path)
#image.show()
image fill_image(image)
image_list cut_image(image)
save_images(image_list)
labelcTof.config(text九宫格图片已生请在程序所在目录查看)
# 窗体
toptk.Tk()
top.title(九宫格图片生成器)
labelcToftk.Label(top,text请输入要进行转换的图片路径,height4,\
width40,fgblue)
labelcTof.pack()
entryCdtk.Entry(top,text0) # 文本框获取图片路径
entryCd.pack()
label_tiptk.Label(top,text请检查图片路径是否输入正确,height2,\
width40,fggray)
label_tip.pack()
btnCaltk.Button(top,text点击生成九宫格图片,fgred,bgyellow,commandcTofClicked) # 点击回调函数
btnCal.pack()
top.mainloop() # 执行主循环
以上就是本文的全部内容希望对大家的学习有所帮助也希望大家多多支持脚本之家。