代码拉取完成,页面将自动刷新
"""
几何变换
实现图像的旋转、对称翻转、剪切等功能。
(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>")
此处可能存在不合适展示的内容,页面不予展示。您可通过相关编辑功能自查并修改。
如您确认内容无涉及 不当用语 / 纯广告导流 / 暴力 / 低俗色情 / 侵权 / 盗版 / 虚假 / 无价值内容或违法国家有关法律法规的内容,可点击提交进行申诉,我们将尽快为您处理。