Tkinter - 并排显示同一显示器中每两个文件夹的所有图像对

时间:2015-12-07 22:43:52

标签: python image tkinter tk

Tkinter - 并排显示同一显示器中每两个目录的所有图像对

我希望在另一个循环中使用Tkinter,在文件夹(png)中发布所有图像,包括所有可能的对!例如如果特定文件夹下有10个图像,我想 显示(10!)100对图像(我不介意一些双重图像)。

直到这个时间我的代码是:

<pre>
import os, sys
    import random
    import numpy as np
    from multiprocessing import Pool
    from Tkinter import *
    from PIL import Image, ImageTk
    import tkFont

    path1='/'
    dirlist=os.listdir(path1)
    ln=len(dirlist)

    #counters
    ln
    size_im=5
    ii=0


    for ii in range(0,2):
       root1=Tk()
       j=ii+1
       dl1=dirlist[0+j]
       image1 = Image.open(os.path.join(path1,dl1))
       image1 = image1.resize((image1.size[0]/size_im,image1.size[1]/size_im), Image.ANTIALIAS)
       display1 = ImageTk.PhotoImage(image1)
       label1 = Label(root1, image=display1)
       label1.pack()
       for kk in range(0,2):
          root2=Tk()
          m=kk+1
          dl2=dirlist[0+m]
          image2 = Image.open(os.path.join(path1,dl2))
          image2 = image2.resize((image2.size[0]/size_im,image2.size[1]/size_im), Image.ANTIALIAS)
          display2 = ImageTk.PhotoImage(image2)
          label2 = Label(root2, image=display2)
          label2.pack()
          root1.mainloop()
          root2.mainloop()

#ultimately, i would like something the below one in photo pairs

for ii in range(0,5):
    j=ii+1
    dl1=0+j
    for kk in range(0,5):
        m=kk+1
        dl2=0+m
        print [dl1, dl2]
<code>

1 个答案:

答案 0 :(得分:-1)

这里是可以显示多个图像的python代码 进入相同的canva,多个图像并排,多个图像一个在另一个下,所有图像到一个目录/文件夹。要显示所有可能的图像对:例如:如果同一文件夹中只存在3个图像,则显示的对将是以下图像:1-1,1-2,1-3,2-1,2 -2,2-3,3-1,3-2,3-3。你可以调整计数器。

我以这种方式制作了道路,可调节。而已。 主要思想是一个接一个地进入两个循环,并在一个根/主循环下对两个图像使用相同的标签(label1)。

        <pre>
    # importing
    import os
    import sys
    import random
    import numpy as np
    from multiprocessing import Pool
    from Tkinter import *
    from PIL import Image, ImageTk
    import tkFont
    # the path that must contain supported (only) image formats
    path1 = '/'
    dirlist = os.listdir(path1)

    # to find the number of images under the directory
    ln = len(dirlist)

    # counters
    # the number of images under the directory
    ln
    sm = 5
    ii = 0
    # making two loops, with main function inside of these loops
    # two counters for two files,directories (here, the same one)
    for ii in range(0, len - 1):
        for kk in range(0, len - 1):
                # open the main loop
                root1 = Tk()
                # increasing one unit each time the loop is repeated
                j = ii + 1
                dl1 = dirlist[0 + j]  # in each loop, next image is loaded
                # joining the path with image filename
                im1 = Image.open(os.path.join(path1, dl1))
                im1 = image1.resize(im1.size[0] / sm, im1.size[1] / sm)
                # ready to display image in tk
                display1 = ImageTk.PhotoImage(image1)
                # labelling image is very essential
                label1 = Label(root1, im=display1)
                label1.pack()
                # second counter for the same directory, file
                # to make the second pair
                m = kk + 1
                dl2 = dirlist[0 + m]
                # second image from the same directory is loaded
                im2 = Image.open(os.path.join(path1, dl2))
                im2 = image2.resize(im2.size[0] / sm, im2.size[1] / sm)
                # ready to display
                display2 = ImageTk.PhotoImage(image2)
                # load it to the same label pack, in order
                # to be displayed in the same canvas
                label1 = Label(root1, image=display2)
                label1.pack()
                # closing the main loop
                root1.mainloop()
                # all possible pairs of two images inside
                # a directory will be displayed one next to other
                # If you have lets sya 10 images then 10*10=100
                # canvas with two images each will be diplayed
<code>