Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 7 additions & 2 deletions sqlalchemy_media/processors.py
Original file line number Diff line number Diff line change
Expand Up @@ -394,8 +394,13 @@ def process(self, descriptor: StreamDescriptor, context: dict):
# Cropping
if self.crop:
img = img.crop(self.crop)

img.save(output_buffer, format=format_)
try:
img.save(output_buffer, format=format_)
except (OSError):
if img.mode in ("RGBA", "P"):
img = img.convert("RGB")
img.save(output_buffer, format=format_)

self._update_context(img, format_, context)
output_buffer.seek(0)
descriptor.replace(output_buffer, position=0, **context)
Expand Down
Binary file added sqlalchemy_media/tests/stuff/rgba-cat.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
15 changes: 15 additions & 0 deletions sqlalchemy_media/tests/test_image_processors.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ def setUpClass(cls):
cls.stuff_path = join(cls.this_dir, 'stuff')
cls.cat_jpeg = join(cls.stuff_path, 'cat.jpg')
cls.cat_png = join(cls.stuff_path, 'cat.png')
cls.cat_png_alpha = join(cls.stuff_path, 'rgba-cat.png')
cls.dog_jpg = join(cls.stuff_path, 'dog_213X160.jpg')

def test_resize_reformat(self):
Expand All @@ -32,6 +33,20 @@ def test_resize_reformat(self):
'height': 150,
'extension': '.jpg'
})

with AttachableDescriptor(self.cat_png_alpha) as d:
ctx = dict(
length=100000,
extension='.jpg',
)
ImageProcessor(fmt='jpeg', width=200).process(d, ctx)

self.assertDictEqual(ctx, {
'content_type': 'image/jpeg',
'width': 200,
'height': 150,
'extension': '.jpg'
})

with AttachableDescriptor(self.cat_jpeg) as d:
# Checking when not modifying stream.
Expand Down