3 Star 0 Fork 0

lzq1357 / Modifying pictures

加入 Gitee
与超过 1200万 开发者一起发现、参与优秀开源项目,私有仓库也完全免费 :)
免费加入
克隆/下载
GeometryFrame.py 4.53 KB
一键复制 编辑 原始数据 按行查看 历史
lzq1357 提交于 2021-07-26 23:15 . UI优化补充
"""
几何变换
实现图像的旋转、对称翻转、剪切等功能。
(1)旋转:顺逆90°旋转。
(2)翻转:上下翻转、左右翻转。
(3)剪切:使用鼠标选区取域,并剪切成新的图像。
# ylyy #
"""
from tkinter import *
from PIL import Image, ImageDraw
import UI
from VariableFrame import VariableFrame
#
class GeometryFrame(VariableFrame):
x = 0
y = 0
def __init__(self, canv, master=None):
VariableFrame.__init__(self, canv, master)
bcolor = UI.bg
fcolor = UI.fg
w = UI.rightWidth-12
h = 25
f = UI.font
fy = 5
dy = h+8
fx = 0
withBtn = Button(self, text="顺时针旋转",
bg=bcolor, fg=fcolor,
bitmap=UI.viewBmp, compound='left',
width=w, height=h,
font=f,
command=lambda:self.rotate(-90))
withBtn.place(x=fx,y=fy)
inverseBtn = Button(self, text="逆时针旋转",
bg=bcolor, fg=fcolor,
bitmap=UI.viewBmp, compound='left',
width=w, height=h,
font=f,
command=lambda:self.rotate(90))
inverseBtn.place(x=fx,y=fy+dy)
out_lrBtn = Button(self, text="左右翻转",
bg=bcolor, fg=fcolor,
bitmap=UI.viewBmp, compound='left',
width=w, height=h,
font=f,
command=lambda:self.out_lr())
out_lrBtn.place(x=fx,y=fy+2*dy)
out_tbBtn = Button(self, text="上下翻转",
bg=bcolor, fg=fcolor,
bitmap=UI.viewBmp, compound='left',
width=w, height=h,
font=f,
command=lambda: self.out_tb())
out_tbBtn.place(x=fx, y=fy+3*dy)
img_cutBtn = Button(self, text="剪切",
bg=bcolor, fg=fcolor,
bitmap=UI.viewBmp, compound='left',
width=w, height=h,
font=f,
command=lambda: self.cut())
img_cutBtn.place(x=fx, y=fy+4*dy)
#旋转图片
def rotate(self, angle):
self.clearEvent()
img_rotate = self.canv.img.rotate(angle, expand=1)
self.canv.refresh(img_rotate)
def out_lr(self):
#左右翻转图片
self.clearEvent()
out_lr = self.canv.img.transpose(Image.FLIP_LEFT_RIGHT) # 左右翻转
self.canv.refresh(out_lr)
def out_tb(self):
#上下翻转图片
self.clearEvent()
out_tb = self.canv.img.transpose(Image.FLIP_TOP_BOTTOM)
self.canv.refresh(out_tb)
def cut(self):
# 剪切图片(鼠标交互已实现)
self.canv.bind("<ButtonPress-1>", self.press)
self.canv.bind("<B1-Motion>", self.move)
self.canv.bind("<ButtonRelease-1>", self.release)
def press(self,event):
ex,ey = self.canv.realCoord(event.x, event.y)
self.x = ex
self.y = ey
self.canv.create_rectangle(self.x, self.y, self.x+2, self.y+2 ,width=3, outline="#ee88aa", tags=("rect"))
def move(self,event):
ex,ey = self.canv.realCoord(event.x, event.y)
self.canv.delete("rect")
self.canv.create_rectangle(self.x, self.y, ex, ey, width=3, outline="#ee88aa", tags=("rect"))
def release(self, event): # 剪切
xx, yy = self.canv.getCoord()
self.x = int((self.x - xx)/self.canv.scale)
self.y = int((self.y - yy)/self.canv.scale)
ex,ey = self.canv.realCoord(event.x, event.y)
x0 = int((ex - xx)/self.canv.scale)
y0 = int((ey - yy)/self.canv.scale)
if self.x > x0:
temp = x0
x0 = self.x
self.x = temp
if self.y > y0:
temp = y0
y0 = self.y
self.y = temp
w,h = self.canv.img.size
if self.x>=0 and self.y>=0 and x0<=w and y0<=h and x0-self.x>1 and y0-self.y>1:
img_cropped = self.canv.img.crop((self.x, self.y, x0, y0))
self.canv.refresh(img_cropped)
self.canv.delete("rect")
def clearEvent(self):
self.canv.unbind("<ButtonPress-1>")
self.canv.unbind("<B1-Motion>")
self.canv.unbind("<ButtonRelease-1>")
Python
1
https://gitee.com/lzq1357/Modifying-pictures.git
git@gitee.com:lzq1357/Modifying-pictures.git
lzq1357
Modifying-pictures
Modifying pictures
master

搜索帮助