如何使用pygame从python中的用户指定文件夹中导入所有图像 [英] How to import all images from a user specified folder in python using pygame

查看:116
本文介绍了如何使用pygame从python中的用户指定文件夹中导入所有图像的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我是python的新手,我正在pygame上工作.对于我的项目,我需要从用户指定的文件夹中导入所有图像.pygame中有什么功能可以只从文件夹中导入图像文件?或指导我仅过滤导入文件夹中所有文件中的图像文件.抱歉,如果这个问题太基本了.

I m new to python, and I'm working on pygame. For my project, i need to import all the images from a user specified folder. Is there any function in pygame to import only the image files from the folder? or guide me to filter only the image files among all files from the imported folder. Sorry, if the question is too basic.

推荐答案

我不了解pygame,但是使用普通python的用户很容易在文件夹中获取所有图像文件:

I don't know about pygame but you with plain python is fairly simple to get all image files within a folder:

import imghdr
import os

for dirpath, dirnames, filenames in os.walk('FOLDER'):
    for filename in filenames:
        file_path = os.path.join(dirpath, filename)
        if imghdr.what(file_path):
            print file_path, ' is an image'

这可以通过递归地浏览文件夹"来实现.我们使用imghdr内置模块检查文件是否为图像.您甚至可以通过检查imghdr.what()函数返回的内容来过滤出您不感兴趣的图像类型.

This works by exploring 'FOLDER' recursively. We check if the file is an image by using the imghdr builtin module. You can even filter out image types you're not interested by checking what the imghdr.what() function returns.

这篇关于如何使用pygame从python中的用户指定文件夹中导入所有图像的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆