python tkinter画布设置按钮对图片放大缩小_使用Tkinter画布小部件添加放大和缩小功能?...

据我所知,内置的Tkinter画布类比例将不会自动缩放图像。如果无法使用自定义小部件,则可以在调用scale函数时缩放原始图像并将其替换到画布上。

下面的代码片段可以合并到原始类中。它执行以下操作:缓存Image.open()的结果。

添加一个redraw()函数来计算缩放的图像并将其添加到画布中,还删除以前绘制的图像(如果有的话)。

使用鼠标坐标作为图像放置的一部分。我只需将x and y传递给create_image函数,以显示当鼠标移动时图像位置如何移动。您可以用自己的中心/偏移计算来替换。

这使用了LinuxMouseWheels按钮4和5(您需要将其推广到Windows等)。

(已更新)代码:class GUI:

def __init__(self, root):

# ... omitted rest of initialization code

self.canvas.config(scrollregion=self.canvas.bbox(ALL))

self.scale = 1.0

self.orig_img = Image.open(File)

self.img = None

self.img_id = None

# draw the initial image at 1x scale

self.redraw()

# ... rest of init, bind buttons, pack frame

def zoom(self,event):

if event.num == 4:

self.scale *= 2

elif event.num == 5:

self.scale *= 0.5

self.redraw(event.x, event.y)

def redraw(self, x=0, y=0):

if self.img_id:

self.canvas.delete(self.img_id)

iw, ih = self.orig_img.size

size = int(iw * self.scale), int(ih * self.scale)

self.img = ImageTk.PhotoImage(self.orig_img.resize(size))

self.img_id = self.canvas.create_image(x, y, image=self.img)

# tell the canvas to scale up/down the vector objects as well

self.canvas.scale(ALL, x, y, self.scale, self.scale)

更新我做了一些不同规模的测试,发现resize/create_image使用了相当多的内存。我在一个32GB内存的MacPro上用540x375JPEG进行了测试。以下是用于不同比例因子的内存:1x (500, 375) 14 M

2x (1000, 750) 19 M

4x (2000, 1500) 42 M

8x (4000, 3000) 181 M

16x (8000, 6000) 640 M

32x (16000, 12000) 1606 M

64x (32000, 24000) ...

reached around ~7400 M and ran out of memory, EXC_BAD_ACCESS in _memcpy

鉴于上述情况,一个更有效的解决方案可能是确定显示图像的视窗大小,计算鼠标坐标中心周围的裁剪矩形,使用矩形裁剪图像,然后仅缩放裁剪部分。这应该使用常量内存来存储临时映像。否则,您可能需要使用第三方Tkinter控件来执行此裁剪/窗口缩放。

更新2工作但过于简单的裁剪逻辑,只是让您开始:def redraw(self, x=0, y=0):

if self.img_id: self.canvas.delete(self.img_id)

iw, ih = self.orig_img.size

# calculate crop rect

cw, ch = iw / self.scale, ih / self.scale

if cw > iw or ch > ih:

cw = iw

ch = ih

# crop it

_x = int(iw/2 - cw/2)

_y = int(ih/2 - ch/2)

tmp = self.orig_img.crop((_x, _y, _x + int(cw), _y + int(ch)))

size = int(cw * self.scale), int(ch * self.scale)

# draw

self.img = ImageTk.PhotoImage(tmp.resize(size))

self.img_id = self.canvas.create_image(x, y, image=self.img)

gc.collect()

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
要在Tkinter放大缩小图片,可以使用Pillow(Python Imaging Library)库来处理图片。以下是一个简单的例子,展示了如何在Tkinter放大缩小图片: ```python from PIL import Image, ImageTk import tkinter as tk class App: def __init__(self, root): self.root = root self.canvas = tk.Canvas(root) self.canvas.pack() # 打开图片 self.image = Image.open("example.jpg") self.photo = ImageTk.PhotoImage(self.image) # 显示图片 self.image_item = self.canvas.create_image(0, 0, image=self.photo, anchor="nw") # 添加放大缩小 self.zoom_in_button = tk.Button(root, text="放大", command=self.zoom_in) self.zoom_in_button.pack(side="left") self.zoom_out_button = tk.Button(root, text="缩小", command=self.zoom_out) self.zoom_out_button.pack(side="left") # 初始缩放比例为1 self.scale = 1.0 def zoom_in(self): self.scale *= 1.2 self.resize() def zoom_out(self): self.scale /= 1.2 self.resize() def resize(self): # 根据缩放比例计算新的图片尺寸 width = int(self.image.width * self.scale) height = int(self.image.height * self.scale) # 使用Pillow库的resize方法缩放图片 resized_image = self.image.resize((width, height)) self.photo = ImageTk.PhotoImage(resized_image) # 更新Canvas中的图片 self.canvas.itemconfig(self.image_item, image=self.photo) root = tk.Tk() app = App(root) root.mainloop() ``` 在这个例子中,我们创建了一个`Canvas`对象,并在其中显示了一张图片。我们还添加了两个按放大缩小图片。当用户点击这些按时,我们更新缩放比例,并使用Pillow库的`resize`方法来调整图片的大小。最后,我们更新Canvas中的图片以显示新的缩放版本。 请注意,这个例子仅仅是一个起点,你可以根据自己的需要进行修改和扩展。例如,你可以添加更多的按来旋转、裁剪或调整图片的对比度和亮度。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值