Exemple #1
0
def write_image(data: bytes, prefix: str) -> Optional[Image.Image]:
    path_prefix = find_image_path(prefix)
    dirname = os.path.dirname(path_prefix)
    if not os.path.exists(dirname):
        os.makedirs(dirname)
    try:
        # save original image
        with open(path_prefix + '.jpg', 'wb') as f:
            f.write(data)

        img = Image.open(BytesIO(data))
        if img.mode != 'RGB':
            img = img.convert('RGB')

        for name, size in config.image_sizes.items():
            path = f"{path_prefix}-{name}.jpg"
            resize_image(img, size).save(path, quality=90)
        return img
    except OSError:
        logger.exception("write_image() failed")

        # cleanup
        rm_f(prefix + '.jpg')
        rm_f(prefix + '-S.jpg')
        rm_f(prefix + '-M.jpg')
        rm_f(prefix + '-L.jpg')

        return None
Exemple #2
0
def write_image(data, prefix):
    path_prefix = find_image_path(prefix)
    dirname = os.path.dirname(path_prefix)
    if not os.path.exists(dirname):
        os.makedirs(dirname)
    try:
        # save original image
        with open(path_prefix + '.jpg', 'wb') as f:
            f.write(data)

        img = Image.open(BytesIO(data))
        if img.mode != 'RGB':
            img = img.convert('RGB')

        for name, size in config.image_sizes.items():
            path = "%s-%s.jpg" % (path_prefix, name)
            resize_image(img, size).save(path, quality=90)
        return img
    except IOError as e:
        print('ERROR:', str(e))

        # cleanup
        rm_f(prefix + '.jpg')
        rm_f(prefix + '-S.jpg')
        rm_f(prefix + '-M.jpg')
        rm_f(prefix + '-L.jpg')

        return None