import os
import time
import tkinter as tk
from tkinter import messagebox
from picture import crop_images, resize_images, rename_images, add_text_to_images
def cut_picture(): # 剪裁
input_path = filename_entry.get()
folder_path = input_path.replace('\\', '/') # 替换字符串中\,变为python可读的地址
region = region_entry.get()
values = region.split() # 或者使用 values = input_str.split("|")
left, upper, right, lower = map(int, values)
try:
crop_images(folder_path, left, upper, right, lower)
messagebox.showinfo("执行成功")
except:
messagebox.showinfo("执行失败")
def adjust_size(): # 调整大小
input_path = filename_entry.get()
folder_path = input_path.replace('\\', '/') # 替换字符串中\,变为python可读的地址
size = size_entry.get()
values = size.split() # 或者使用 values = input_str.split("|")
width, height = map(int, values)
try:
resize_images(folder_path, width, height)
messagebox.showinfo("执行成功")
except:
messagebox.showinfo("执行失败")
def rename_picture(): # 修改文件名
input_path = filename_entry.get()
folder_path = input_path.replace('\\', '/') # 替换字符串中\,变为python可读的地址
image_name = image_name_entry.get()
try:
rename_images(folder_path, image_name)
messagebox.showinfo("执行成功")
except:
messagebox.showinfo("执行失败")
def add_text(): # 添加文字
input_path = filename_entry.get()
folder_path = input_path.replace('\\', '/') # 替换字符串中\,变为python可读的地址
text = text_entry.get()
input_position = position_entry.get()
values = input_position.split()
position = tuple(map(int, values))
try:
add_text_to_images(folder_path, text, position)
messagebox.showinfo("执行成功")
except:
messagebox.showinfo("执行失败")
def execute_function():
# 根据单选按钮的选择执行相应函数
selection = function_selection.get()
if selection == 1:
cut_picture()
elif selection == 2:
adjust_size()
elif selection == 3:
rename_picture()
elif selection == 4:
add_text()
# 创建界面窗口
window = tk.Tk()
# 单选按钮
function_selection = tk.IntVar()
cut_picture_radio = tk.Radiobutton(window, text="剪裁", variable=function_selection, value=1)
cut_picture_radio.pack()
adjust_size_radio = tk.Radiobutton(window, text="调整大小", variable=function_selection,
value=2)
adjust_size_radio.pack()
rename_picture_radio = tk.Radiobutton(window, text="修改文件名", variable=function_selection,
value=3)
rename_picture_radio.pack()
add_text_radio = tk.Radiobutton(window, text="添加文字", variable=function_selection,
value=4)
add_text_radio.pack()
# 标签和文本框
filename_label = tk.Label(window, text="文件夹地址:")
filename_label.pack()
filename_entry = tk.Entry(window)
filename_entry.pack()
region_label = tk.Label(window, text="剪裁区域:")
region_label.pack()
region_entry = tk.Entry(window)
region_entry.pack()
size_label = tk.Label(window, text="长宽尺寸:")
size_label.pack()
size_entry = tk.Entry(window)
size_entry.pack()
image_name_label = tk.Label(window, text="新名称:")
image_name_label.pack()
image_name_entry = tk.Entry(window)
image_name_entry.pack()
text_label = tk.Label(window, text="添加文字内容:")
text_label.pack()
text_entry = tk.Entry(window)
text_entry.pack()
position_label = tk.Label(window, text="添加文字位置:")
position_label.pack()
position_entry = tk.Entry(window)
position_entry.pack()
# 执行按钮
execute_button = tk.Button(window, text="执行", command=execute_function)
execute_button.pack()
window.mainloop()
没有合适的资源?快使用搜索试试~ 我知道了~
温馨提示
分为函数和界面代码,代码整体框架: # 1.编写在图片上添加文字功能函数 # 2.编写重命名图片功能函数 # 3.编写修改图片大小功能函数 # 4.编写图片剪裁功能函数 # 5.创建界面窗口 # 5.2创建界面控件:4个单选按钮,4个标签和多个单行编辑框,1个执行按钮 # 5.3单选按钮和执行链接对应函数 备注: 由于python读取地址格式的问题,本代码额外增加了转换,直接复制粘贴就可以。 可在b站看使用教程效果,链接如下: 【【09】python练习|图片的批量裁剪、添加文字、修改尺寸和文件名】 https://www.bilibili.com/video/BV1TH4y197P1/?share_source=copy_web&vd_source=3a5925b532459caa93ff86011cae74a5
资源推荐
资源详情
资源评论
收起资源包目录
picture_screen.zip (3个子文件)
picture_screen.exe 21.74MB
picture.py 2KB
picture_screen.py 4KB
共 3 条
- 1
资源评论
parry学习记录
- 粉丝: 0
- 资源: 9
上传资源 快速赚钱
- 我的内容管理 展开
- 我的资源 快来上传第一个资源
- 我的收益 登录查看自己的收益
- 我的积分 登录查看自己的积分
- 我的C币 登录后查看C币余额
- 我的收藏
- 我的下载
- 下载帮助
安全验证
文档复制为VIP权益,开通VIP直接复制
信息提交成功