3 Star 0 Fork 0

lzq1357 / Modifying pictures

加入 Gitee
与超过 1200万 开发者一起发现、参与优秀开源项目,私有仓库也完全免费 :)
免费加入
克隆/下载
ToolFrame.py 5.18 KB
一键复制 编辑 原始数据 按行查看 历史
lzq1357 提交于 2021-08-14 21:34 . 修复显示尺寸bug
"""
基本功能
(1)打开/新建:打开已存在的图片文件 / 新建图像。
(2)保存/另存为:将编辑后的图片保存至原文件 或 保存至新建文件。
(3)撤销/恢复:撤销一个操作 / 恢复撤销的操作。
(4)放大/缩小:将图像放大或缩小展示,方便编辑。
# lzy #
"""
import os
from tkinter import *
from tkinter import filedialog
from PIL import Image
import UI
class ToolFrame(Frame):
canv=None
path = "" #"img\tdcq.jpg"
sizeScale = None
types = [('image files', '*.jpg *.jpeg *.png *.gif *.webp'), ('all files', '*.*')]
def __init__(self, canv, master=None):
Frame.__init__(self, master, relief=RAISED, borderwidth=2, bg=UI.bg, width=1145, height=36)
self.canv=canv
self.placeWidget()
def placeWidget(self):
"""
放置控件
"""
bcolor = UI.bg
fcolor = UI.fg
w = 8
h = 1
pw=80
ph = UI.topHeight-11
f = UI.font
fx = 10 #first x
dx = pw+8 #间距 x
btn = Button(self, text="选择图片",
bg=bcolor, fg=fcolor,
bitmap=UI.viewBmp, compound='left',
width=pw, height=ph,
font=f,
command=lambda:self.choose())
btn.place(x=fx,y=0)
btn1 = Button(self, text="保存",
bg=bcolor, fg=fcolor,
bitmap=UI.viewBmp, compound='left',
width=pw, height=ph,
font=f,
command=lambda:self.save())
btn1.place(x=fx+dx,y=0)
btn1 = Button(self, text="另存为",
bg=bcolor, fg=fcolor,
bitmap=UI.viewBmp, compound='left',
width=pw, height=ph,
font=f,
command=lambda:self.saveas())
btn1.place(x=fx+2*dx,y=0)
btn2 = Button(self, text="撤消",
bg=bcolor, fg=fcolor,
bitmap=UI.viewBmp, compound='left',
width=pw, height=ph,
font=f,
command=lambda:self.canv.undo())
btn2.place(x=fx+3*dx,y=0)
btn3 = Button(self, text="恢复",
bg=bcolor, fg=fcolor,
bitmap=UI.viewBmp, compound='left',
width=pw, height=ph,
font=f,
command=lambda:self.canv.redo())
btn3.place(x=fx+4*dx,y=0)
sizeLabel = Button(self, text="显示尺寸%",
bg=bcolor, fg=fcolor,
bitmap=UI.viewBmp, compound='left',
width=pw, height=ph,
font=f)
sizeLabel.place(x=fx+5*dx,y=0)
self.sizeScale = Scale(self,
from_=10, # 设置最小值
to=500, # 设置最大值
resolution=10, # 设置步距值
orient=HORIZONTAL, # 设置水平方向
borderwidth=0,
bg=bcolor, fg=fcolor,
command=self.setShowSize)
self.sizeScale.set(100)
self.sizeScale.place(x=fx+6*dx, y=-9)
btn4 = Button(self, text="关闭",
bg=bcolor, fg=fcolor,
bitmap=UI.viewBmp, compound='left',
width=pw, height=ph,
font=f,
command=lambda:self.canv.close())
btn4.place(x=fx+8*dx,y=0)
def choose(self):
"""
选择图片
"""
types = [('image files', '*.jpg *.jpeg *.png *.gif *.webp'), ('all files', '*.*')]
file_path = filedialog.askopenfilename(filetypes=types)
if not os.path.isfile(file_path):
return
image = Image.open(file_path)
self.path = file_path
self.canv.restart()
w = image.width
h = image.height
ww = (self.canv.width-10)/w
hh = (self.canv.height-10)/h
self.sizeScale.set(100)
if ww<1 or hh<1:
if ww<hh:
min=ww
else:
min=hh
self.sizeScale.set(int(min*100))
self.canv.refresh(image)
def save(self):
"""
保存
"""
if self.canv.img is not None:
self.canv.img.save(self.path)
def saveas(self):
"""
另存为
"""
types = [('image files', '*.jpg *.jpeg *.png *.gif *.webp *.ico')]
if self.canv.img is not None:
img1=self.canv.img
fname = filedialog.asksaveasfilename(filetypes=types)
ext = fname.split('.').pop().lower()
if ext in ('jpg', 'jpeg', 'gif', 'webp', 'png', 'ico'):
img1.save(str(fname))
else:
img1.save(str(fname)+'.png')
def setShowSize(self, scaleVar):
"""
改变图片显示尺寸
"""
self.canv.scale = int(scaleVar)/100
self.canv.showImage()
#
Python
1
https://gitee.com/lzq1357/Modifying-pictures.git
git@gitee.com:lzq1357/Modifying-pictures.git
lzq1357
Modifying-pictures
Modifying pictures
master

搜索帮助