Don't convert images blindly to RGB. Only convert to RGB or RGBA when saving to JPEG and mode is 'P'. documents.tests.test_models.MultiPageTiffTestCase passes again.

This commit is contained in:
Roberto Rosario
2015-09-16 15:55:11 -04:00
parent 60e64b92fd
commit dbd676646b
2 changed files with 12 additions and 3 deletions

View File

@@ -93,7 +93,7 @@ class Python(ConverterBase):
file_object.seek(0)
else:
try:
image = Image.open(self.file_object).convert('RGB')
image = Image.open(self.file_object)
except IOError as exception:
error_message = _('Exception determining PDF page count; %s') % exception
logger.error(error_message)

View File

@@ -163,7 +163,7 @@ class ConverterBase(object):
self.file_object.seek(0)
try:
self.image = Image.open(self.file_object).convert('RGB')
self.image = Image.open(self.file_object)
except IOError:
# Cannot identify image file
self.image = self.convert(page_number=page_number)
@@ -176,7 +176,16 @@ class ConverterBase(object):
self.seek(0)
image_buffer = StringIO()
self.image.save(image_buffer, format=output_format)
new_mode = self.image.mode
if output_format.upper() == 'JPEG':
if self.image.mode == 'P':
new_mode = 'RGB'
if 'transparency' in self.image.info:
new_mode = 'RGBA'
self.image.convert(new_mode).save(image_buffer, format=output_format)
image_buffer.seek(0)
return image_buffer