-2

I would like to learn how to let it continuously run to calculate every single image in a folder of standard deviation and mean of the images.

my current code is like this, it's basic manual code...

For current code is this it:

im = Image.open(r'path of the image')
stat = ImageStat.Stat(im)
img = mahotas.imread(r'path of the image')
mean = img.mean()
print(str(mean))
print(stat.stddev)
Michael M.
  • 10,486
  • 9
  • 18
  • 34
Neko Lim
  • 53
  • 5

2 Answers2

1

Read the entire files in the directory by listdir and iterate the files through for loop

import os
from PIL import Image, ImageStat

dir_path = "Your Directory/Image Folder/"
listOfImageFiles = os.listdir(dir_path)
fileext = ('.png', 'jpg', 'jpeg')

for imageFile in listOfImageFiles:
    if imageFile.endswith(fileext):
       im = Image.open(os.path.join(dir_path,imageFile))
       stat = ImageStat.Stat(im)
       img = mahotas.imread(os.path.join(dir_path,imageFile))
       mean = img.mean()
       print(str(mean))
       print(stat.stddev)
Hari E
  • 526
  • 2
  • 14
  • `os.listdir` also returns folders, so you need to check whether `imageFile` is actually a file. Furthermore, I think, the file ending of `imageFile` should also be checked. Your solution only works if all files in `Your Directory/Image Folder/` are appropriate image files. – Green绿色 Jan 13 '23 at 01:55
  • Hi, thanks for the reply, the code is running will but if only can calculate one of the image only and it cant proceed with another images, below is the message I get... DecompressionBombWarning: Image size (92875872 pixels) exceeds limit of 89478485 pixels, could be decompression bomb DOS attack. warnings.warn( 187.59779772286822 [69.11878315244641] – Neko Lim Jan 13 '23 at 01:58
  • Green, Yes you are right. `os.walk` will be the right choice – Hari E Jan 13 '23 at 01:59
  • `import warnings warnings.simplefilter('ignore', Image.DecompressionBombWarning)` to ignore such warning. – Hari E Jan 13 '23 at 02:01
  • If the answer helps , Please mark this as accepted answer :) – Hari E Jan 13 '23 at 02:04
1

If I understand your question correctly, the os.walk function is what you need. It walks the file tree recursively and returns all folder and file names within each directory. Check the documentation for details. Here's how you would use it to compute the mean and standard deviation for each image file in a folder:

import os

IMAGE_FORMATS = {'.jpg', '.jpeg', '.png'}

for root_path, _, filenames in os.walk('/path/to/your/folder'):
    for filename in filenames:
        _, ext = os.path.splitext(filename)
        if ext in IMAGE_FORMATS:
            full_path = os.path.join(root_path, filename)
            im = Image.open(full_path)
            stat = ImageStat.Stat(im)
            img = mahotas.imread(full_path)
            mean = img.mean()
            print(f"Image {full_path}: mean={mean}, stddev={stat.stddev}")

Note: the difference of os.walk to os.listdir is that os.walk walks the file tree recursively, i.e., it will also walk through each sub-folder and look for images there. os.listdir only lists the files and directories of exactly the folder you provide, but doesn't walk through sub-folders.

Green绿色
  • 1,620
  • 1
  • 16
  • 43
  • Hi, for the mahotas.imread(r'path of the image') section there, as i put my path like "/Users/user/desktop/internal_triggered", i have error for it like this PermissionError: [Errno 13] Permission denied: – Neko Lim Jan 13 '23 at 02:34
  • Sorry, I overlooked that line. It should be `mahotas.imread(full_path)`. I changed my answer accordingly. – Green绿色 Jan 13 '23 at 02:41