display each images from a directory

Hello All,

i'm tring to write python code to display images from a directory .. but i'm not able to learn how to use the matplotlib events
to handle how the images will be displaied.

simpli i need to pass to the code the path to directory where i have several tiff files and disply them one at time using a key event.

to display a single image i'm using somethin like :

#!/usr/bin/env python
from pylab import *
try:
    import Image
except ImportError, exc:
    raise SystemExit("PIL must be installed to run this example")
import glob

def dispimg(input):
    image = Image.open(input)
    im = imshow(image, origin='lower')
    show()

···

###########

i tried to define a key action like :

def onpress(event):
    if event.key=='a':
        fig.canvas.draw()

# and set my directory with :

fig = figure()
fig.canvas.mpl_connect('key_press_event', onpress)
TIFFS = glob.glob("/home/habcam/habcamdata/20100820_1840/*.tif")
print "num TIFFS %d" % len(TIFFS)

##############

but i'm not able to learn how to plot them and update the displayed image using the key bindings :

for tif in TIFFS:
    print tif
    show()

thanks a lot for Any help!

Massimo.

Massimo,

You might want to try a different approach. Instead of having a keypress binding, why not just have a normal for-loop with a call to dispimg() and some sort of blocking input like a wait for keypress or something? You might even be able to do something with a while loop that checks for a ‘q’ input (for quit) and then have two keys be used for forward and backwards progression through your list of TIFFS.

Another thing to consider is just not use matplotlib at all, and just use linux’s image viewer (called “eog”) which you can give it the first file in a directory, and you can use the arrow keys to move through the list of all image files in that directory.

I hope that helps,

Ben Root

···

On Mon, Nov 29, 2010 at 1:48 PM, Massimo Di Stefano <massimodisasha@…287…> wrote:

Hello All,

i’m tring to write python code to display images from a directory … but i’m not able to learn how to use the matplotlib events

to handle how the images will be displaied.

simpli i need to pass to the code the path to directory where i have several tiff files and disply them one at time using a key event.

to display a single image i’m using somethin like :

#!/usr/bin/env python

from pylab import *

try:

import Image

except ImportError, exc:

raise SystemExit("PIL must be installed to run this example")

import glob

def dispimg(input):

image = Image.open(input)

im = imshow(image, origin='lower')

show()

###########

i tried to define a key action like :

def onpress(event):

if event.key=='a':

    fig.canvas.draw()

and set my directory with :

fig = figure()

fig.canvas.mpl_connect(‘key_press_event’, onpress)

TIFFS = glob.glob(“/home/habcam/habcamdata/20100820_1840/*.tif”)

print “num TIFFS %d” % len(TIFFS)

##############

but i’m not able to learn how to plot them and update the displayed image using the key bindings :

for tif in TIFFS:

print tif

show()

thanks a lot for Any help!

Massimo.