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

发布于 2024-12-03 01:08:13 字数 1547 浏览 2 评论 0原文

我试图理解 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 的无知,我对此很陌生。

I'm try to understand python doing some codes. I want to select a part of a open image with zoom and save the selected part.
Right now i'm trying to quickly save the open and zoom image by pressing a key. For explain better, i need to fire a function savefig() by pressing a keyboard key.

I tried to use urwid module :

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

def save(input):

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

And I used uinput as well to try do it the same thing:

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)

And i have the same result in this 2 codes, this error message appeared:

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)

It's strang because i never open Tkinter. Please help´me with that, How do i save the zoom image with a key event?

I'm sorry by my ignorance in python, i'm very new at this.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

岁月蹉跎了容颜 2024-12-10 01:08:13

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

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

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

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

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)使用不同的键(中没有的键)此处的列表) 或 b) 暂时禁用 matplotlib 的 's' 按键绑定进行保存。

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

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()

You don't explicitly say it, but it appears that you're using matplotlib to do this.

I can't say for certain from what you've posted, but I'd guess that what's happening is that s is already bound to "save figure" for a matplotlib figure. (And by default, matplotlib uses a Tk-based backend, thus the Tk error.)

There's no need to use the urwid module. Matplotlib has hooks to do things like this, and you're going to need to disconnect some of those hooks to do what you need.

As a simple, stand-alone example to reproduce your problem:

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()

Notice that the figure will be saved, but you'll also get a pop-up file selection dialogue to save the figure again.

You can avoid this by either: a) using a different key (something not in the list here) or b) temporarily disabling matplotlib's key binding of 's' to save.

It's pretty easy to temporarily disable it with the appropriate 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()
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文