WxPython 图片操作:缩放、旋转、虚线框、圆圈
import wx
class MyFrame(wx.Frame):
def __init__(self, parent, title):
super(MyFrame, self).__init__(parent, title=title, size=(600, 400))
self.InitUI()
def InitUI(self):
# 加载背景图片
self.bg = wx.Image('bg.jpg', wx.BITMAP_TYPE_ANY).ConvertToBitmap()
wx.StaticBitmap(self, -1, self.bg, (0, 0))
# 加载小图片 b
self.b = wx.Image('b.jpg', wx.BITMAP_TYPE_ANY).ConvertToBitmap()
self.bitmap = wx.StaticBitmap(self, -1, self.b, (100, 100))
# 绑定事件
self.bitmap.Bind(wx.EVT_LEFT_DOWN, self.OnLeftDown)
self.bitmap.Bind(wx.EVT_LEFT_UP, self.OnLeftUp)
self.bitmap.Bind(wx.EVT_MOTION, self.OnMotion)
self.bitmap.Bind(wx.EVT_RIGHT_DOWN, self.OnRightDown)
self.Show(True)
def OnLeftDown(self, e):
self.startPos = e.GetPosition()
self.rect = self.bitmap.GetScreenRect()
self.delta = wx.Point(self.rect.x - self.startPos.x, self.rect.y - self.startPos.y)
# 画虚线框
self.dc = wx.ClientDC(self)
self.dc.SetPen(wx.Pen('white', 1, wx.DOT))
self.dc.SetBrush(wx.Brush(wx.Colour(0, 0, 0, 0), wx.TRANSPARENT))
self.dc.DrawRectangle(self.rect)
# 画四个圆
self.dc.SetPen(wx.Pen('red', 1))
self.dc.SetBrush(wx.Brush(wx.Colour(0, 0, 0, 0), wx.TRANSPARENT))
self.dc.DrawCircle(self.rect.x, self.rect.y, 5)
self.dc.DrawCircle(self.rect.x + self.rect.width, self.rect.y, 5)
self.dc.DrawCircle(self.rect.x, self.rect.y + self.rect.height, 5)
self.dc.DrawCircle(self.rect.x + self.rect.width, self.rect.y + self.rect.height, 5)
def OnLeftUp(self, e):
# 清除虚线框和圆
if self.dc:
self.dc.Clear()
self.dc = None
def OnMotion(self, e):
if e.Dragging() and e.LeftIsDown():
# 拖动虚线框,缩放 b
pos = e.GetPosition()
dx = pos.x - self.startPos.x
dy = pos.y - self.startPos.y
self.newRect = wx.Rect(self.rect.x + dx, self.rect.y + dy, self.rect.width, self.rect.height)
self.bitmap.SetBitmap(self.b.GetSubBitmap(self.newRect))
if e.Dragging() and e.RightIsDown():
# 拖动四个圆,旋转 b
pos = e.GetPosition()
dx = pos.x - self.startPos.x
dy = pos.y - self.startPos.y
cx = self.rect.x + self.rect.width / 2
cy = self.rect.y + self.rect.height / 2
theta = (pos.x - cx) * (self.startPos.y - cy) - (pos.y - cy) * (self.startPos.x - cx)
theta = theta / ((pos.x - cx) * (self.startPos.x - cx) + (pos.y - cy) * (self.startPos.y - cy))
theta = theta * 180 / 3.1415926
self.newBitmap = self.b.Rotate(theta, (self.rect.x + self.rect.width / 2, self.rect.y + self.rect.height / 2))
self.bitmap.SetBitmap(self.newBitmap.ConvertToBitmap())
def OnRightDown(self, e):
# 点击背景,清除虚线框和圆
if self.dc:
self.dc.Clear()
self.dc = None
if __name__ == '__main__':
app = wx.App()
MyFrame(None, '图片处理')
app.MainLoop()
代码功能:
- 加载背景图片和一个小图片 b。
- 点击图片 b,b 周围会出现虚线框和四个角的圆圈。
- 拖动虚线框可以缩放图片 b。
- 拖动圆圈可以旋转图片 b。
- 点击背景图片可以清除虚线框和圆圈。
代码说明:
- 使用
wx.StaticBitmap显示图片。 - 使用
wx.EVT_LEFT_DOWN、wx.EVT_LEFT_UP、wx.EVT_MOTION和wx.EVT_RIGHT_DOWN绑定事件。 - 使用
wx.ClientDC画虚线框和圆圈。 - 使用
wx.Image.GetSubBitmap和wx.Image.Rotate分别实现缩放和旋转功能。 - 使用
dc.Clear()清除虚线框和圆圈。
使用方法:
- 确保
bg.jpg和b.jpg文件位于代码同目录下。 - 运行代码,即可看到图片处理窗口。
注意:
- 代码中的图片路径需要根据实际情况调整。
- 该代码仅供参考,实际使用中可能需要根据具体需求进行修改。
原文地址: https://www.cveoy.top/t/topic/ohi7 著作权归作者所有。请勿转载和采集!