python - python 中 savefig 中的关键事件。怎么做?

标签 python image-processing keyboard matplotlib

我试图理解 python 的一些代码。我想选择缩放打开图像的一部分并保存所选部分。 现在我正在尝试通过按键快速保存打开和缩放的图像。为了更好地解释,我需要通过按键盘按键来触发函数 savefig() 。

我尝试使用 urwid 模块:

import urwid
import PIL
import Image
im=Image.Open("fig.tif")
imshow(im) 

def save(input):

    if input in ('s'):
        savefig("fig2.png")

我也使用 uinput 来尝试做同样的事情:

import uinput 
import PIL
import Image 

def main():
    capabilities = {uinput.EV_KEY: (uinput.KEY_S)}
    device = uinput.Device(name="python-uinput-keyboard", capabilities=capabilities)
    if device.emit(uinput.EV_KEY, uinput.KEY_S, 1):
        savefig("sat.png")

im=Image.open("fig.tif")
imshow(im)

我在这两个代码中得到了相同的结果,出现了此错误消息:

Exception in Tkinter callback
Traceback (most recent call last):
File "/usr/lib/python2.6/lib-tk/Tkinter.py", line 1413, in call
return self.func(*args)
File "/usr/lib/pymodules/python2.6/matplotlib/backends/backend_tkagg.py", line 312, in key_press
FigureCanvasBase.key_press_event(self, key, guiEvent=event)
File "/usr/lib/pymodules/python2.6/matplotlib/backend_bases.py", line 1143, in key_press_event
self.callbacks.process(s, event)
File "/usr/lib/pymodules/python2.6/matplotlib/cbook.py", line 163, in process func(*args, **kwargs)
File "/usr/lib/pymodules/python2.6/matplotlib/backend_bases.py", line 1703, in key_press self.canvas.toolbar.save_figure(self.canvas.toolbar)
TypeError: save_figure() takes exactly 1 argument (2 given)

这很奇怪,因为我从来没有打开过 Tkinter。请帮我解决这个问题,如何保存带有关键事件的缩放图像?

很抱歉我对Python的无知,我对此很陌生。

最佳答案

您没有明确说明,但看​​来您正在使用 matplotlib 来执行此操作。

我无法从您发布的内容中确定,但我猜发生的情况是 s 已经绑定(bind)到 matplotlib 图形的“保存图形”。 (默认情况下,matplotlib 使用基于 Tk 的后端,因此会出现 Tk 错误。)

无需使用urwid模块。 Matplotlib 有钩子(Hook)来做这样的事情,你需要断开其中一些钩子(Hook)来完成你需要的事情。

作为一个简单的独立示例来重现您的问题:

import matplotlib.pyplot as plt
import numpy as np

def save(event):
    if event.key == 's':
        print 'Saved figure'
        event.canvas.figure.savefig('temp.png')

fig, ax = plt.subplots()
ax.imshow(np.random.random((10,10)))
fig.canvas.mpl_connect('key_press_event', save)
plt.show()

请注意,图形将被保存,但您还会看到一个弹出文件选择对话框,用于再次保存图形。

您可以通过以下任一方式避免这种情况:a) 使用不同的键( the list here 中没有的键)或 b) 暂时禁用 matplotlib 的 's' 按键绑定(bind)进行保存。

使用适当的 matplotlibrc setting 可以很容易地暂时禁用它。 .

import matplotlib.pyplot as plt
import matplotlib as mpl
import numpy as np

# Disable the 's' interactive keybinding
mpl.rcParams['keymap.save'] = ''

def save(event):
    if event.key == 's':
        print 'Saved figure'
        event.canvas.figure.savefig('temp.png')

fig, ax = plt.subplots()
ax.imshow(np.random.random((10,10)))
fig.canvas.mpl_connect('key_press_event', save)
plt.show()

关于python - python 中 savefig 中的关键事件。怎么做?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/7299826/

相关文章:

python - 为什么我的程序可以处理大于一的数字

python - 在 psycopg2 中使用 "with statement"关闭连接的正确方法是什么?

python - 在认知服务 API 中创建人员组时出现问题

python - 如何找到图像中最常见的像素值?

ruby-on-rails - 使用 Rmagick 或 ImageMagick 在背景上放置标题

batch-file - 检测击键的批处理文件。如何?

c# - 使用 SendKeys 发送特殊字符

python - 如何用Python下载网页上的PDF文件

matlab - 几乎垂直边缘的亚像素边缘检测

c# - 如何捕获 Ctrl+Shift+N?