我怎样才能获得一个函数 return 一个文件夹中的所有图像,以便其他函数与 IronPython 一起使用?

How can I get a function to return all the images within a folder for other functions to use with IronPython?

我有一些代码可以很好地制作标签式图片查看器,只有在单独设置每个图像的路径时,标签式图片查看器才具有正确数量的标签。我希望它能获取一个文件夹中的所有图像,而不必为每个图像写一行,

pathToFile1 = (r'C:\Users\...\Desktop\one.jpg')
pathToFile2 = (r'C:\Users\...\Desktop\two.jpg')
pathToFile3 = (r'C:\Users\...\Desktop\three.jpg')

以后再用

def setupImages(self):
    return [
            Image.FromFile(pathToFile1),
            Image.FromFile(pathToFile2),
            Image.FromFile(pathToFile3),
        ]

然后使用下面的函数...

def setupPanel(self, parent):
     tabControl = TabControl()
     tabControl.Dock = DockStyle.Fill
     tabControl.Alignment = TabAlignment.Bottom
     for i in range(3):
         tabPage = TabPage()
         tabPage.Text = 'Image %s' % i
         tabPage.Controls.Add(self.getPictureBox(self.images[i])
         tabControl.TabPages.Add(tabPage)
     parent.Controls.Add(tabControl)

使用 Artog 的 setupImages 函数的完整代码。

import clr
clr.AddReference('System.Windows.Forms')
clr.AddReference('System.Drawing')
from os import listdir
from os.path import isfile, join
from System.Drawing import Image
from System.Windows.Forms import (
    Application, DockStyle, Form, Orientation, PictureBox,
    PictureBoxSizeMode, SplitContainer,
    TabAlignment, TabControl, TabPage
)



class MainForm(Form):
    def __init__(self):

        Form.__init__(self)
        self.images = self.setupImages() 
        splitter = SplitContainer()
        splitter.Orientation = Orientation.Vertical
        splitter.Dock = DockStyle.Fill

        def SwapOrientation(sender, event):
            if sender.Orientation == Orientation.Vertical:
                sender.Orientation = Orientation.Horizontal
            else:
                sender.Orientation = Orientation.Vertical

            splitter.DoubleClick += SwapOrientation
            self.setupPanel(splitter.Panel1)
            self.setupPanel(splitter.Panel2)
            self.Controls.Add(splitter)
            self.Text = 'Xaar Rogues Gallery'
            self.Show()

    def setupImages(self):
        mypath = (r'C:\Users\priper\Desktop\RoguesGalleryImages') # No 
        need to wrap with ()
        valid_file_endings = ['jpg','png','jpeg'] # Add / remove as needed

        # Best to start variable names with lower case, so as not to 
        confuse with classes
        image_filenames = [ 
            Image.FromFile(f) for f in listdir(mypath) 
            if isfile(join(mypath, f)) 
            and f[-3:] in valid_file_endings # check if last three 
            characters of filename is in the list of valid endings
         ]
        # print len(image_filenames)
        # print(image_filenames)
        return image_filenames 

    def getPictureBox(self, image):
        pictureBox = PictureBox()
        pictureBox.SizeMode = PictureBoxSizeMode.StretchImage
        pictureBox.Image = image
        pictureBox.Dock = DockStyle.Fill
        return pictureBox

    def setupPanel(self, parent):
        tabControl = TabControl()
        tabControl.Dock = DockStyle.Fill
        tabControl.Alignment = TabAlignment.Bottom
        for i in range(3):
            tabPage = TabPage()
            tabPage.Text = 'Image %s' % i
            tabPage.Controls.Add(self.getPictureBox(self.images[i]))
            tabControl.TabPages.Add(tabPage)
        parent.Controls.Add(tabControl)

Application.EnableVisualStyles()
form = MainForm()
Application.Run(form)

无需单独硬编码即可自动获取文件夹中图片的代码,如果有一种方法可以自动用图片名称命名选项卡就好了。

正如评论中所讨论的那样,您的代码中已经有了解决方案。只需将逻辑移入函数内即可完成:)

def setupImages(self):
    mypath = 'C:\Users\...\Desktop\RoguesGalleryImages' # No need to wrap with ()
    valid_file_endings = ['jpg','png','jpeg'] # Add / remove as needed

    # Best to start variable names with lower case, so as not to confuse with classes
    image_filenames = [ 
        Image.FromFile(f) for f in listdir(mypath) 
        if isfile(join(mypath, f)) 
        and f[-3:] in valid_file_endings # check if last three characters of filename is in the list of valid endings
    ]
    # print len(image_filenames)
    # print(image_filenames)
    return image_filenames