Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

PIL how to display multiple images side by side

3,453 views
Skip to first unread message

suresh.amritapuri

unread,
Jan 8, 2010, 4:43:45 PM1/8/10
to
Hi,

In PIL, how to display multiple images in say m rows and n colums when
I have m*n images.

suresh

Sean DiZazzo

unread,
Jan 8, 2010, 10:43:04 PM1/8/10
to
On Jan 8, 1:43 pm, "suresh.amritapuri" <suresh.amritap...@gmail.com>
wrote:

Sounds like a good project to learn PIL with.

~Sean

Lie Ryan

unread,
Jan 10, 2010, 12:24:01 AM1/10/10
to

Tkinter has PhotoImage widget and PIL has support for this widget:
http://www.pythonware.com/library/pil/handbook/imagetk.htm


Alf P. Steinbach

unread,
Jan 10, 2010, 12:51:58 AM1/10/10
to
* Lie Ryan:

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

suresh.amritapuri

unread,
Jan 11, 2010, 3:19:03 PM1/11/10
to

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

Alf P. Steinbach

unread,
Jan 11, 2010, 5:07:34 PM1/11/10
to
* suresh.amritapuri:

> On Jan 9, 9:51 pm, "Alf P. Steinbach" <al...@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.

I suggest you use the grid layout manager.

Uh, "geometry mangler".


Cheers,

- Alf

0 new messages