Python 脚本中的 Sips 命令不起作用 - “错误 4:未指定文件”和 "not a valid file - skipping"

Sips command in a Python script is not working - “Error 4: no file was specified” and "not a valid file - skipping"

正在尝试通过 Python 脚本调整某些图像的大小(仅宽度)。 这是一个 Python 脚本:

# -*- coding: utf-8 -*-

import subprocess
import os


# New width
new_width = '200'

# Create for converted images
create_directory_out = subprocess.run(['mkdir', '-p', './Result'])

# Directory with started images
directory_source = 'Source'

#  Directory with converted images
directory_out = 'Result'

# get list of started images to variable files
files = os.listdir(directory_source)

# Filtre by mask .jpg to variable images
images = filter(lambda x: x.endswith('.jpg'), files)

img_list = list(images)

# Loop of convert images by sips
for file_name in img_list:
    print(file_name)
    subprocess.run(['sips', '--resampleWidth', 'new_width', '--out', './directory_out/file_name', './directory_source/file_name', ])

我收到一个错误:

face-04.jpg
Warning: ./directory_source/file_name not a valid file - skipping
Error 4: no file was specified
Try 'sips --help' for help using this tool
face-04.jpg

但终端中的 sips 命令正在运行:

sips --resampleWidth 200 --out ./Result/face-04.jpg ./Source/face-04.jpg 

还有什么问题?

在此先感谢您的帮助。

您将文字与变量混淆了:

subprocess.run(['sips', '--resampleWidth', 'new_width', '--out', './directory_out/file_name', './directory_source/file_name', ])

尝试从字面上访问 './directory_out/file_name'

你需要实际使用你的变量和join目录和文件名:

subprocess.run(['sips', '--resampleWidth', 'new_width', '--out', os.path.join(directory_out,file_name), os.path.join(directory_source,file_name)])

旁白:

create_directory_out = subprocess.run(['mkdir', '-p', './Result'])

可以替换为本机 python 调用:

os.makedirs(directory_out)