代码之家  ›  专栏  ›  技术社区  ›  pyprism

为imagemagic命令处理文件名中的空间

  •  0
  • pyprism  · 技术社区  · 5 年前

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

    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 回复  |  直到 5 年前
        1
  •  1
  •   pyprism    5 年前

    更改为后已修复 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"])