QWERTY
QWERTY

Reputation: 49

Script to process all images in a single directory using Python

This is what i have tried so far. Images are in .jpg file. I am trying to write a script that can run resize.py to resize all the images at once. No import is used in this .py script

for file in *.jpg; do
  python resize.py "$file"
done

Error returned to me was

  File "test.py", line 2
    for file in *.jpg; do
                ^

SyntaxError: invalid syntax

Upvotes: 1

Views: 2327

Answers (1)

Burhan Khalid
Burhan Khalid

Reputation: 174728

You can simplify this with the following, which you would type on the shell (its not Python code):

find /path/to/image/dir -name "*.jpg" -exec python /path/to/resize.py {} \;

If you want to do this entirely in Python:

import glob

from resize import your_resize_function

for image_file in glob.iglob('/path/to/image/dir/*.jpg'):
    your_resize_function(image_file)

Here your_resize_function is whatever code is running inside resize.py.

Upvotes: 2

Related Questions