3

I have an array of images in Python3 like this...

images = [
    "/var/www/html/myfolder/images/1.jpg",
    "/var/www/html/myfolder/images/441.jpg",
    "/var/www/html/myfolder/images/15.jpg",
    "/var/www/html/myfolder/images/78.jpg",
]

Instead of specifying the image like this I would like to pass an absolute path and have python create me the images list out of the .jpg images that are in that path.

What is my best approach?

fightstarr20
  • 11,682
  • 40
  • 154
  • 278
  • I have the path already saved as `path` in my script, stuck with actually scanning the folder to get the jpg files – fightstarr20 Aug 11 '19 at 15:20
  • Possible duplicate of [How do I list all files of a directory?](https://stackoverflow.com/questions/3207219/how-do-i-list-all-files-of-a-directory) – Trenton McKinney Aug 11 '19 at 15:33

7 Answers7

8

You can make use of glob.

glob.glob(pathname, *.jpg, recursive=False)

Return a possibly-empty list of path names that match pathname, which must be a string containing a path specification. pathname can be either absolute (like /usr/src/Python-1.5/Makefile) or relative (like ../../Tools//.gif), and can contain shell-style wildcards. Broken symlinks are included in the results (as in the shell).

If recursive is true, the pattern “**” will match any files and zero or more directories and subdirectories. If the pattern is

followed by an os.sep, only directories and subdirectories match.

let's say your abs path is myfolder

import glob
images = glob.glob('images/*.jpg')

https://docs.python.org/3/library/glob.html#glob.glob

Talha Junaid
  • 2,351
  • 20
  • 29
5

The pathlib module in Python 3 makes this easy:

from pathlib import Path
images = Path("/var/www/html/myfolder/images").glob("*.jpg")

Want all jpg images recursively under that directory instead? Use .glob("*/**.jpg").

Note that this is creating an array of Path objects. If you want strings, just convert them:

image_strings = [str(p) for p in images]
mk12
  • 25,873
  • 32
  • 98
  • 137
3

If you specify the path there are a number of ways to find all the files in that directory. Once you have that list you can simply iterate through it and create the images.

See: How do I list all files of a directory?

A good way to do it is using os.listdir:

import os

# specify the img directory path
path = "path/to/img/folder/"

# list files in img directory
files = os.listdir(path)

for file in files:
    # make sure file is an image
    if file.endswith(('.jpg', '.png', 'jpeg')):
        img_path = path + file

        # load file as image...
Grobbed
  • 317
  • 2
  • 12
  • 1
    Thanks for posting this. It is very helpful. I did notice a typo. You forgot the single quote after '.png. – DataGirl Dec 28 '21 at 21:22
1

To scan just the top level

import os
path = "path/to/img/folder/"
jpgs = [os.path.join(path, file)
        for file in os.listdir(path)
        if file.endswith('.jpg')]

To scan recursively, replace the last line with

jpgs = [os.path.join(root, file)
        for root, dirs, files in os.walk(path)
        for file in files
        if file.endswith('.jpg')]
Kamaraju Kusumanchi
  • 1,809
  • 19
  • 12
0
import os

images=[]

def getFiles(path):
    for file in os.listdir(path):
        if file.endswith(".jpg"):
            images.append(os.path.join(path, file))

images list:

filesPath = "/var/www/html/myfolder/images"

getFiles(filesPath)
print(images)
Himanshu
  • 666
  • 1
  • 8
  • 18
0
  • The modern method is to use pathlib which treats paths as objects, not strings. As an object, all paths then have methods to access various components of the path, (e.g. .suffix, .stem).
  • pathlib also has:
    • .glob build-in
    • a .open method (e.g. Path.open(mode='r'))
  • Python 3's pathlib Module: Taming the File System

Code:

from pathlib import Path

jpg_files = Path('/some_path').glob('*.jpg')

for file in jpg_files:
    with file.open(mode='r') as f:
        ...
        do some stuff
Trenton McKinney
  • 56,955
  • 33
  • 144
  • 158
0

It already exists a solution in imutils package

from imutils import paths
path_img = "my_folder"
images = list(paths.list_images(path_img))
tCot
  • 307
  • 2
  • 7