简体   繁体   English

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

[英]Tkinter - post all pairs of images per two of a folder in the same display, side by side

Tkinter - post all pairs of images per two of a directory in the same display, side by side Tkinter-在同一显示中并排发布目录中每两个目录的所有图像对

I would like one loop inside another, in order to use Tkinter, to post all the images inside a folders (png), including all the possible pairs of them! 为了使用Tkinter,我想在另一个循环中将所有图像发布到文件夹(png)中,包括所有可能的对! eg If 10 images exist under a specific folder, i would like to display (10!) 100 pairs of images (i dont mind some double ones). 例如,如果在一个特定的文件夹下存在10张图像,我想显示(10!)100对图像(我不介意一些重复的图像)。

my code until this time is: 在这段时间之前,我的代码是:

<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>

Here, it is python code that can display multiple images into the same canva, multiple images side by side, multiple images one under another, all images into one directory / folder. 在这里,这是python代码,可以将多个图像显示在同一个canva中,并排显示多个图像,一个接一个显示多个图像,所有图像都显示在一个目录/文件夹中。 To display all possible pairs of images: for example: if only 3 images exist in the same folder, their pairs that will be displayed will be the following ones: 1-1,1-2,1-3,2-1,2-2,2-3,3-1,3-2,3-3. 要显示所有可能的图像对,例如:如果同一文件夹中仅存在3张图像,则将显示的图像对将是以下图像对:1-1,1-2,1-3,2-1,2 -2,2-3,3-1,3-2,3-3。 you may adjust counters. 您可以调整计数器。

I made the path in such way, to be adjustable. 我以这种方式进行了调整。 nothing more. 而已。 The main idea was to enter the two loops one under the other as well to use the same label (label1) for both images under one root/main loop. 主要思想是在一个根/主循环下对两个图像使用相同的标签(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>

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM