0

尝试通过 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 

还有什么问题?

在此先感谢您的帮助。

4

1 回答 1

1

您将文字与变量混合在一起:

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)
于 2017-01-29T16:36:48.610 回答