使用 Python 处理单个目录中所有图像的脚本



这是我到目前为止尝试过的。图像位于.jpg文件中。我正在尝试编写一个可以运行 resize.py 的脚本,以一次调整所有图像的大小。此.py脚本中未使用导入

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

返回给我的错误是

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

错误:语法无效

你可以用以下内容来简化这一点,你可以在shell上键入它(它不是Python代码):

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

如果你想完全在 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)

这里your_resize_functionresize.py内部运行的任何代码。

最新更新