def image_style(image, style):
    """ Return the url of different image style
    Construct appropriately if not exist
    See 9cms-crop.odt

    Available styles
     - thumbnail: create a thumbnail restricted to the smaller dimension
     - thumbnail-upscale: create a thumbnail that is upscaled if smaller
     - thumbnail-crop: create a thumbnail that is cropped to the exact dimension

    :param image: ImageFieldFile
    :param style: Specify style to return image
    :return: image url of specified style
    """
    if not image:  # pragma: nocover
        return image
    # original url full: /media/ninecms/basic/image/test.png
    url = image.url
    # original url without file: /media/ninecms/basic/image
    url_path = '/'.join(url.split('/')[:-1])
    # original path full: ~/ninecms/media/ninecms/basic/image/test.png
    img_path_file_name = str(image.file)
    # original file: test.png
    img_file_name = os.path.basename(img_path_file_name)

    # style url without file: /media/ninecms/basic/image/large
    style_url_path = '/'.join((url_path, style))
    # style url full: /media/ninecms/basic/image/large/test.png
    style_url = '/'.join((style_url_path, img_file_name))
    # style path without file: ~/ninecms/media/ninecms/basic/image/large
    style_path = os.path.join(os.path.dirname(img_path_file_name), style)
    # style path full: ~/ninecms/media/ninecms/basic/image/large/test.png
    style_path_file_name = os.path.join(style_path, img_file_name)

    # the style dict
    style_def = settings.IMAGE_STYLES[style]

    if not os.path.exists(style_path_file_name):
        if not os.path.exists(style_path):
            os.makedirs(style_path)
        by = chr(120)   # x
        plus = chr(43)  # +

        # remove original path file name as it may contain spaces, before splitting
        # exception: usually file not exists (db or memcached inconsistency)
        try:
            # noinspection PyUnresolvedReferences
            source_size_str = check_output(['identify', img_path_file_name]).decode()
        except CalledProcessError:  # pragma: nocover
            return url
        source_size_str = source_size_str[len(img_path_file_name):].split(' ')[2]

        source_size_array = source_size_str.split(by)
        source_size_x = int(source_size_array[0])
        source_size_y = int(source_size_array[1])
        target_size_x = style_def['size'][0]
        target_size_y = style_def['size'][1]
        target_size_str = str(target_size_x) + by + str(target_size_y)

        # thumbnail
        if style_def['type'] == 'thumbnail':
            if source_size_x > target_size_x and target_size_y > source_size_y:
                target_size_str = source_size_str
            call(['convert', img_path_file_name, '-thumbnail', target_size_str, '-antialias', style_path_file_name])

        # thumbnail-upscale
        elif style_def['type'] == 'thumbnail-upscale':
            call(['convert', img_path_file_name, '-thumbnail', target_size_str, '-antialias', style_path_file_name])

        # thumbnail-crop
        elif style_def['type'] == 'thumbnail-crop':
            source_ratio = float(source_size_x) / float(source_size_y)
            target_ratio = float(target_size_x) / float(target_size_y)
            if source_ratio > target_ratio:  # crop vertically
                crop_target_size_x = source_size_y * target_ratio
                crop_target_size_y = source_size_y
                offset = (source_size_x - crop_target_size_x) / 2
                crop_size_str = str(crop_target_size_x) + by + str(crop_target_size_y) + plus + str(offset) + plus + '0'
            else:  # crop horizontally
                crop_target_size_x = source_size_x
                crop_target_size_y = source_size_x / target_ratio
                offset = (source_size_y - crop_target_size_y) / 2
                crop_size_str = str(crop_target_size_x) + by + str(crop_target_size_y) + plus + '0' + plus + str(offset)
            call(['convert', img_path_file_name, '-crop', crop_size_str, style_path_file_name])
            call(['convert', style_path_file_name, '-thumbnail', target_size_str, '-antialias', style_path_file_name])
            # moderators ^ and \> for -thumbnail and -resize do not work consistently:
            # "invalid argument for option `-resize'"
            # call(['convert', path_file_name, '-thumbnail', target_size_str + '^', '-gravity', 'center', '-extent',
            #      target_size_str, '-antialias', style_path_file_name])

        # # crop
        # elif style_def['type'] == 'crop':
        #    call(['convert', img_path_file_name, '-gravity', 'center', '-crop', target_size_str, style_path_file_name])
        #     call(['convert', img_path_file_name, '-gravity', 'center', '-background', 'None', '-extent',
        #           target_size_str, style_path_file_name])
    return style_url
dataset/ETHPy150Open Wtower/django-ninecms/ninecms/utils/media.py/image_style/swapped operands of `>`