如何让函数返回文件夹中的所有图像,以便其他函数与IronPython一起使用?

如何让函数返回文件夹中的所有图像,以便其他函数与IronPython一起使用?,python,function,directory,ironpython,Python,Function,Directory,Ironpython,我有一些代码可以很好地制作一个选项卡式的图片查看器,只有当每个图像的路径被单独设置时,它才有适当数量的选项卡。我希望它能够获取文件夹中的所有图像,而不必为每个图像写一行 pathToFile1 = (r'C:\Users\...\Desktop\one.jpg') pathToFile2 = (r'C:\Users\...\Desktop\two.jpg') pathToFile3 = (r'C:\Users\...\Desktop\three.jpg') 然后以后使用 def setupIm

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

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 

你已经在使用os.ListDir为什么不再次使用?我已经努力走到这一步了,我现在迷路了。你做得很好,唯一缺少的是检查图像文件的结尾该文件夹应该只保存图像,所以我不太担心,我不知道如何获得进入setupImages函数的路径,现在脑子里全是雾:-太棒了,要么像使用pathToFile1一样使用ImageFileName。或者您在函数中执行listdir逻辑I走了这么远,然后在os.listdirpath:if file.endswith'.jpg':files.appendos.path.joinpath,file printos.path.joinpath,文件打印“\n”*2打印文件返回[Image.FromFilepathToFile1,Image.FromFilepathToFile2,Image.FromFilepathToFile3,]我可能错了,但它似乎只提供字符串路径,而没有返回图像,我猜这是因为setupImages最初返回的是Image.FromFilepathToFile1,现在它只是字符串路径。我尝试了“return Image.FromFileimage\u filenames”,但这开始指向self.images=self.setupImages的行,这是一个问题。哦,对不起,是的。。将编辑和修复。只是缺少Image.fromfhi Artog,我仍然无法让它工作。我已经编辑了代码来添加你的函数-它对你有用吗?我收到文件夹中第一个图像的“IOError:[Errno 2]”。我知道该图像在那里,因为此选项卡查看器的早期迭代发现它可以,但只能使用每个图像的固定路径。您可以尝试将image.FromFilef更改为image.FromFilejoinmypath,f