我正在尝试根据文件名和大小调整图像大小:
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
答案 0 :(得分: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"])