import tkinter as tk
from PIL import Image,ImageTk
import sys
import tkinter.filedialog
#先将图片填充为正方形
def fill_image(image):
width, height = image.size
#比较图片的宽和高,选取值较大的作为新图的宽
newImage_width = width if width > height else height
#生成正方形图,空白处用白色填充
newImage = Image.new(image.mode, (newImage_width, newImage_width), color='white')
#如果原图宽大于高,则填充图片的竖直维度
if width > height:
newImage.paste(image, (0, int((newImage_width - height) / 2)))
else:
newImage.paste(image, (int((newImage_width - width) / 2),0))
return newImage
# 切图(切成9张图)
def cut_image(image):
width, height = image.size
colWidth = int(width / 3) #一行3张
image_grid = []
for i in range(0,3):
for j in range(0,3):
row = (j*colWidth,i*colWidth,(j+1)*colWidth,(i+1)*colWidth)
image_grid.append(row)
image_list = [image.crop(row) for row in image_grid]
return image_list
#保存图片
def save_images(image_list):
index = 1
for image in image_list:
image.save(str(index) + '.png', 'PNG')
index+=1
# 单击选择图片按钮,选择并显示图片
def select_button():
global a
a=tk.filedialog.askopenfilename()
#显示图片
img = Image.open(a)
out = img.resize((310,280)) #设置图片的大小
#out.save("new.png") #保存图片
render = ImageTk.PhotoImage(out)
img = tkinter.Label(image=render)
img.image = render
img.place(x=30, y=60)
txt.set(a) #显示图片文件路径
# 单击切分图片按钮,实现图片分割
def cut_button():
file_path=a # 获取图片路径
image = Image.open(file_path)
image_new = fill_image(image)
image_list = cut_image(image_new)
save_images(image_list)
label1.config(text='切图成功!请在程序所在目录查看!')
# 设置窗口
main=tk.Tk()
#设置窗口的大小
main.geometry('400x400')
main.title('明日九宫格切图器') #设置标题栏
main.iconbitmap('mr.ico') #窗口图标
label1=tk.Label(main,text='显示要切分图片的文件路径:',fg='blue')
label1.pack()
txt = tkinter.StringVar()
txt_entry = tkinter.Entry(main, width=55, textvariable=txt)
txt_entry.pack()
button1=tk.Button(main,width=10, height=1,text='选择图片',fg='red',bg='yellow',command=select_button).place(x=30, y=360)
button2=tk.Button(main,width=10, height=1,text='切分图片',fg='red',bg='green',command=cut_button).place(x=120, y=360)
main.mainloop() # 执行主循环
没有合适的资源?快使用搜索试试~ 我知道了~
用Python制作九宫格切图器.zip
![preview](https://csdnimg.cn/release/downloadcmsfe/public/img/white-bg.ca8570fa.png)
共12个文件
png:9个
py:1个
jpg:1个
![preview-icon](https://csdnimg.cn/release/downloadcmsfe/public/img/scale.ab9e0183.png)
1.该资源内容由用户上传,如若侵权请联系客服进行举报
2.虚拟产品一经售出概不退款(资源遇到问题,请及时私信上传者)
2.虚拟产品一经售出概不退款(资源遇到问题,请及时私信上传者)
版权申诉
0 下载量 131 浏览量
2024-02-22
17:22:04
上传
评论
收藏 275KB ZIP 举报
温馨提示
用Python制作九宫格切图器.zip
资源推荐
资源详情
资源评论
![zip](https://img-home.csdnimg.cn/images/20210720083736.png)
![zip](https://img-home.csdnimg.cn/images/20210720083736.png)
![docx](https://img-home.csdnimg.cn/images/20210720083331.png)
![zip](https://img-home.csdnimg.cn/images/20210720083736.png)
![7z](https://img-home.csdnimg.cn/images/20210720083312.png)
![zip](https://img-home.csdnimg.cn/images/20210720083736.png)
![zip](https://img-home.csdnimg.cn/images/20210720083736.png)
![zip](https://img-home.csdnimg.cn/images/20210720083736.png)
![rar](https://img-home.csdnimg.cn/images/20210720083606.png)
![zip](https://img-home.csdnimg.cn/images/20210720083736.png)
![pdf](https://img-home.csdnimg.cn/images/20210720083512.png)
![zip](https://img-home.csdnimg.cn/images/20210720083736.png)
![zip](https://img-home.csdnimg.cn/images/20210720083736.png)
![rar](https://img-home.csdnimg.cn/images/20210720083606.png)
![pdf](https://img-home.csdnimg.cn/images/20210720083512.png)
收起资源包目录
![package](https://csdnimg.cn/release/downloadcmsfe/public/img/package.f3fc750b.png)
![folder](https://csdnimg.cn/release/downloadcmsfe/public/img/folder.005fa2e5.png)
![file-type](https://csdnimg.cn/release/download/static_files/pc/images/minetype/PNG.png)
![file-type](https://csdnimg.cn/release/download/static_files/pc/images/minetype/PNG.png)
![file-type](https://csdnimg.cn/release/download/static_files/pc/images/minetype/JPG.png)
![file-type](https://csdnimg.cn/release/download/static_files/pc/images/minetype/UNKNOWN.png)
![file-type](https://csdnimg.cn/release/download/static_files/pc/images/minetype/PNG.png)
![file-type](https://csdnimg.cn/release/download/static_files/pc/images/minetype/PNG.png)
![file-type](https://csdnimg.cn/release/download/static_files/pc/images/minetype/PNG.png)
![file-type](https://csdnimg.cn/release/download/static_files/pc/images/minetype/PNG.png)
![file-type](https://csdnimg.cn/release/download/static_files/pc/images/minetype/PNG.png)
![file-type](https://csdnimg.cn/release/download/static_files/pc/images/minetype/UNKNOWN.png)
![file-type](https://csdnimg.cn/release/download/static_files/pc/images/minetype/PNG.png)
![file-type](https://csdnimg.cn/release/download/static_files/pc/images/minetype/PNG.png)
共 12 条
- 1
资源评论
![avatar-default](https://csdnimg.cn/release/downloadcmsfe/public/img/lazyLogo2.1882d7f4.png)
![avatar](https://profile-avatar.csdnimg.cn/7be31534d5d34df890f6ea820718b8ed_liaozp88.jpg!1)
徐浪老师
- 粉丝: 6531
- 资源: 6880
![benefits](https://csdnimg.cn/release/downloadcmsfe/public/img/vip-rights-1.c8e153b4.png)
下载权益
![privilege](https://csdnimg.cn/release/downloadcmsfe/public/img/vip-rights-2.ec46750a.png)
C知道特权
![article](https://csdnimg.cn/release/downloadcmsfe/public/img/vip-rights-3.fc5e5fb6.png)
VIP文章
![course-privilege](https://csdnimg.cn/release/downloadcmsfe/public/img/vip-rights-4.320a6894.png)
课程特权
![rights](https://csdnimg.cn/release/downloadcmsfe/public/img/vip-rights-icon.fe0226a8.png)
开通VIP
上传资源 快速赚钱
我的内容管理 展开
我的资源 快来上传第一个资源
我的收益
登录查看自己的收益我的积分 登录查看自己的积分
我的C币 登录后查看C币余额
我的收藏
我的下载
下载帮助
![voice](https://csdnimg.cn/release/downloadcmsfe/public/img/voice.245cc511.png)
![center-task](https://csdnimg.cn/release/downloadcmsfe/public/img/center-task.c2eda91a.png)
安全验证
文档复制为VIP权益,开通VIP直接复制
![dialog-icon](https://csdnimg.cn/release/downloadcmsfe/public/img/green-success.6a4acb44.png)