1

I am trying to resize image based on file name and size :

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'])

then here is errors :

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

I am using python 3.6. Now how can I handle empty space or space less file name ?

update : After wraping in double quote resize(BASE_DIR + "/convert/x/images 2.jpeg", ["308x412", "400x400"]) it throws this error

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
pyprism
  • 2,928
  • 10
  • 46
  • 85

1 Answers1

1

Fixed after changing to shell=False , working codes:

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"])
pyprism
  • 2,928
  • 10
  • 46
  • 85