开发者

Key event in savefig in python. How to do it?

开发者 https://www.devze.com 2023-04-02 02:54 出处:网络
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.

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"开发者_Go百科)
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.


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

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号