gpt4 book ai didi

python - 处理 imagemagic 命令的文件名中的空格

转载 作者:行者123 更新时间:2023-12-01 07:41:04 24 4
gpt4 key购买 nike

我正在尝试根据文件名和大小调整图像大小:

import subprocess
import os

os.environ.setdefault('DJANGO_SETTINGS_MODULE', 'hiren.settings')

from hiren.settings import BASE_DIR


def resize(image_file, size):
os.chdir(BASE_DIR + '/convert/')
file_name = os.path.splitext(os.path.basename(image_file))[0]
file_ext = os.path.splitext(os.path.basename(image_file))[1]

for i in size:
cmd = ['convert', image_file, '-resize', i, file_name + i + file_ext]
# subprocess.check_call(f'convert {image_file} -resize {i} {file_name + i + file_ext}', shell=True)
subprocess.check_call(cmd, shell=True)


resize(BASE_DIR + '/convert/x/images 2.jpeg', ['308x412', '400x400'])

然后这里是错误:

Traceback (most recent call last):
File "/home/../image.py", line 26, in <module>
resize(BASE_DIR + '/convert/x/images_2.jpeg', ['308x412', '400x400'])
File "/home/.../image.py", line 23, in resize
subprocess.check_call(cmd, shell=True)
File "/usr/lib/python3.6/subprocess.py", line 311, in check_call
raise CalledProcessError(retcode, cmd)
Version: ImageMagick 6.9.7-4 Q16 x86_64 20170114 http://www.imagemagick.org
subprocess.CalledProcessError: Command '['convert', '/home/../convert/x/images_2.jpeg', '-resize', '308x412', 'images_2308x412.jpeg']' returned non-zero exit status 1.
Copyright: © 1999-2017 ImageMagick Studio LLC
License: http://www.imagemagick.org/script/license.php
Features: Cipher DPC Modules OpenMP
Delegates (built-in): bzlib djvu fftw fontconfig freetype jbig jng jpeg lcms lqr ltdl lzma openexr pangocairo png tiff wmf x xml zlib
Usage: convert-im6.q16 [options ...] file [ [options ...] file ...] [options ...] file

我使用的是Python 3.6。现在我该如何处理空格或空格较少的文件名?

更新:用双引号括起来后resize(BASE_DIR + "/convert/x/images 2.jpeg", ["308x412", "400x400"])它抛出这个错误

By default, the image format of `file' is determined by its magic
number. To specify a particular image format, precede the filename
with an image format name and a colon (i.e. ps:image) or specify the
image type as the filename suffix (i.e. image.ps). Specify 'file' as
'-' for standard input or output.
Traceback (most recent call last):
File "/home/../utils/image.py", line 26, in <module>
resize(BASE_DIR + "/convert/x/images_2.jpeg", ['308x412', '400x400'])
File "/home/../utils/image.py", line 23, in resize
subprocess.check_call(cmd, shell=True)
File "/usr/lib/python3.6/subprocess.py", line 311, in check_call
raise CalledProcessError(retcode, cmd)
subprocess.CalledProcessError: Command '['convert', '/home/../convert/x/images_2.jpeg', '-resize', '308x412', 'images_2308x412.jpeg']' returned non-zero exit status 1.

Process finished with exit code 1

最佳答案

更改为 shell=False 后修复,工作代码:

import subprocess
import os

os.environ.setdefault('DJANGO_SETTINGS_MODULE', 'hiren.settings')

from hiren.settings import BASE_DIR


def resize(image_file, size):
"""
Resize image
:param image_file: string
:param size: size list
:return:
"""
os.chdir(BASE_DIR + '/convert/')
file_name = os.path.splitext(os.path.basename(image_file))[0]
file_ext = os.path.splitext(os.path.basename(image_file))[1]

for i in size:
cmd = ['convert', image_file, '-resize', i, file_name + i + file_ext]
# subprocess.check_call(f'convert {image_file} -resize {i} {file_name + i + file_ext}', shell=True)
subprocess.check_call(cmd, shell=False)


resize(BASE_DIR + "/convert/x/malta orange-.png", ["308x412", "400x400"])

关于python - 处理 imagemagic 命令的文件名中的空格,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/56713837/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com