Skip to content Skip to sidebar Skip to footer

How To Import All Images From A User Specified Folder In Python Using 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

Solution 1:

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 osfor dirpath, dirnames, filenames inos.walk('FOLDER'):
    for filename in filenames:
        file_path = os.path.join(dirpath, filename)
        if imghdr.what(file_path):
            print file_path, ' is an image'

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.

Post a Comment for "How To Import All Images From A User Specified Folder In Python Using Pygame"