winx
winx

Reputation: 13

list out the images of higher resolution using python

Im trying to write a python script which will walk through a directory and traverse the all sub directories and if any jpg or png image has higher resolution than 2048*2048 then print out the name of those images. I am not able to traverse the sub-directories. Can anyone plz look into the code

import os
import matplotlib.image as plt

root_path = 'E:\newfolder'
img_list = os.listdir(root_path)

for img_name in img_list:

    if img_name.endswith(('.png', '.jpg')):
        img = plt.imread(root_path+'/'+img_name)

        if img.shape[0] > 2048 and img.shape[1] > 2048:
            print(root_path, img_name) 

Upvotes: 1

Views: 809

Answers (3)

KailasMM
KailasMM

Reputation: 106

crawl subdirectory using os.walk

import os
import pathlib
from PIL import Image

def crawlImages(directory):
    allowedExtensions = ['.jpg', '.png']
    for root, dirs, files in os.walk(directory):
        for f in files:
            if pathlib.Path(f).suffix in allowedExtensions:
                fileName = os.path.abspath(os.path.join(root, f))
                image = Image.open(fileName)
                width, height = image.size

                # checking minimum image width and height
                if width > 2400 and height > 2400:
                    print(fileName, width, height)


crawlImages('E:\\music')

Upvotes: 1

DV82XL
DV82XL

Reputation: 6629

Here's a complete and tested solution that is similar to answers by @Paul and @BrainCity. You'll see that I prefer using small, clear functions, since these encourage reusable code.

You can do the image processing using matplotlib as you do in your question, but you need to install Pillow in order to handle .JPG images, since matplotlib only supports PNG natively. I prefer using Pillow directly unless you're already using matplotlib for other stuff.

from pathlib import Path
from PIL import Image

def print_high_res_images(directory: str):
    root_path = Path(directory).resolve()
    high_res_images = get_high_res_images(root_path)

    if high_res_images:
        print('High resolution images:')
        for file_path in high_res_images:
            print(file_path)

def get_high_res_images(root_path: Path) -> []:
    return [path for path in root_path.rglob("*.*") if is_high_res_image(path)]

def is_high_res_image(file_path: Path) -> bool:
    if is_image(file_path):
        image = Image.open(file_path)
        width, height = image.size
        return width > 2048 and height > 2048

    return False

def is_image(file: Path) -> bool:
    return file.suffix.lower() in ['.png', '.jpg']

# Test our new function:
print_high_res_images(r'E:\newfolder')

Upvotes: 1

Paul M.
Paul M.

Reputation: 10799

If you're using Python 3.5+, you can use the pathlib module, and then use a recursive glob pattern to find all files in all subdirectories. Then, you filter the paths and retain only those whose suffix is .png or .jpg:

from pathlib import Path

for path in [path for path in Path("dir/to/images").rglob("*.*") if path.suffix.lower() in (".png", ".jpg")]:
    # image = Image(path)
    # if image dimensions greater than 2048 x 2048:
    #     print(path)

print(path) will print out the entire absolute path of the current image, but if you just want to print out the name, you can print(path.name), which will include the suffix (file extension). If you just want the name of the file without the extension, you can print(path.stem).

Upvotes: 1

Related Questions