【问题标题】:How to Zoom and Pan Image in Python?如何在 Python 中缩放和平移图像?
【发布时间】:2018-02-26 07:00:12
【问题描述】:
我有一张图片:
我想在这张图片上选一个点。但是,当我显示图像时,我只能在屏幕上看到它的一部分,如下:
我想知道如何缩小和平移图像,以便我还能够在同一图像上选择一个点并进行处理。
我尝试使用此处给出的代码:Move and zoom a tkinter canvas with mouse,但问题是这会将图像显示在不同的画布上,并且我的所有进一步处理都应该在图像本身上。
我不想使用图像调整大小功能,因为这会导致像素方向变化/像素丢失
请帮忙!
【问题讨论】:
标签:
python
image
image-processing
zooming
【解决方案1】:
您应该在处理图像本身的过程中将画布坐标转换为图像坐标。
例如,对于代码“Move and zoom a tkinter canvas with mouse”,在 Zoom 类的 __init__ 方法中添加以下事件:
self.canvas.bind('<ButtonPress-3>', self.get_coords) # get coords of the image
函数self.get_coords将鼠标右键单击事件的坐标转换为图像坐标并打印到控制台:
def get_coords(self, event):
""" Get coordinates of the mouse click event on the image """
x1 = self.canvas.canvasx(event.x) # get coordinates of the event on the canvas
y1 = self.canvas.canvasy(event.y)
xy = self.canvas.coords(self.imageid) # get coords of image's upper left corner
x2 = round((x1 - xy[0]) / self.imscale) # get real (x,y) on the image without zoom
y2 = round((y1 - xy[1]) / self.imscale)
if 0 <= x2 <= self.image.size[0] and 0 <= y2 <= self.image.size[1]:
print(x2, y2)
else:
print('Outside of the image')
另外我建议你使用更先进的缩放技术from here。尤其是粗体 EDIT 文本之后的第二个代码示例。