PIL how to display multiple images side by side
suresh.amritapuri
suresh.amritapuri at gmail.com
Mon Jan 11 15:19:03 EST 2010
On Jan 9, 9:51 pm, "Alf P. Steinbach" <al... at start.no> wrote:
> * Lie Ryan:
>
> > On 1/9/2010 8:43 AM, suresh.amritapuri wrote:
> >> Hi,
>
> >> In PIL, how to display multiple images in say m rows and n colums when
> >> I have m*n images.
>
> >> suresh
>
> > Tkinter has PhotoImage widget and PIL has support for this widget:
> >http://www.pythonware.com/library/pil/handbook/imagetk.htm
>
> Maybe I've misunderstood something (in that case glad to learn!), but I believe
> PhotoImage is not a widget, and that a PhotoImage has to be presented in e.g. a
> Label widget or some other widget that's able to display images.
>
> Cheers,
>
> - Alf
Hi,
Let me paste the code I have adapted from somewhere. I dont get a clue
on how to go further so that all the images would stay in their
respective positions.
thanks
suresh
def button_click_exit_mainloop (event):
event.widget.quit() # this will cause mainloop to unblock.
root = Tkinter.Tk()
root.bind("<Button>", button_click_exit_mainloop)
root.geometry('+%d+%d' % (100,100))
names = ["me1.jpg","me2.jpg","me1.jpg","me2.jpg","me1.jpg","me2.jpg"]
shape = (2,3)
xlen,ylen = 0,0
for i in range(shape[0]):
for j in range(shape[1]):
try:
images = [Image.open(f) for f in names]
images = [x.resize((300,200)) for x in images]
xsize = sum(im.size[0] for im in images)
ysize = sum(im.size[1] for im in images)
root.geometry('%dx%d' % (xsize,ysize))
tkpi = [ImageTk.PhotoImage(im) for im in images]
index = i*shape[1]+j
label_image = Tkinter.Label(root, image=tkpi[index])
label_image.place(x=xlen,y=ylen,width=images[index].size
[0],height=images[index].size[1])
if j == shape[1]-1:
ylen = ylen + images[index].size[1]
xlen = 0
else:
xlen = xlen + images[index].size[0]
print index,xlen,ylen
root.mainloop() # wait until user clicks the window
except Exception, e:
# This is used to skip anything not an image.
# Image.open will generate an exception if it cannot open a
file.
# Warning, this will hide other errors as well.
pass
More information about the Python-list
mailing list