def save_as_jpeg(self, f, quality=85, optimize=False, progressive=False): with self.image.convert( 'pjpeg' if progressive else 'jpeg') as converted: converted.compression_quality = quality converted.save(file=f) return JPEGImageFile(f)
def save_as_jpeg(self, f, quality=85, optimize=False, progressive=False): if self.image.mode in ['1', 'P']: image = self.image.convert('RGB') else: image = self.image # Pillow only checks presence of optimize kwarg, not its value. kwargs = {} if optimize: kwargs['optimize'] = True if progressive: kwargs['progressive'] = True kwargs['icc_profile'] = image.info.get('icc_profile') exif = image.info.get('exif') if exif: kwargs['exif'] = exif # Try saving the image and catch potential Pillow errors caused by large # EXIF data. See the issue below for details about how Pillow could # crash: # https://github.com/python-pillow/Pillow/issues/148#issuecomment-578787435 try: image.save(f, 'JPEG', quality=quality, **kwargs) except OSError as e: if 'exif' in kwargs: kwargs.pop('exif') image.save(f, 'JPEG', quality=quality, **kwargs) else: raise e return JPEGImageFile(f)
def test_jpeg_with_orientation_8(self): with open('tests/images/orientation/landscape_8.jpg', 'rb') as f: image = PillowImage.open(JPEGImageFile(f)) image = image.auto_orient() self.assert_orientation_landscape_image_is_correct(image)
def setUp(self): with open('tests/images/people.jpg', 'rb') as f: # Open the image via Pillow pillow_image = PillowImage.open(JPEGImageFile(f)) buffer_rgb = pillow_image.to_buffer_rgb() colour_image = OpenCVColorImage.from_buffer_rgb(buffer_rgb) self.image = OpenCVGrayscaleImage.from_color(colour_image)
def save_as_jpeg(self, f, quality=85): if self.image.mode in ['1', 'P']: image = self.image.convert('RGB') else: image = self.image image.save(f, 'JPEG', quality=quality) return JPEGImageFile(f)
def test_save_as_jpeg_with_icc_profile(self): with open('tests/images/trees.jpg', 'rb') as f: image = PillowImage.open(JPEGImageFile(f)) icc_profile = PILImage.open(f).info.get('icc_profile') self.assertIsNotNone(icc_profile) saved = image.save_as_jpeg(io.BytesIO()) saved_icc_profile = PILImage.open(saved.f).info.get('icc_profile') self.assertEqual(saved_icc_profile, icc_profile)
def test_jpeg_with_orientation_7(self): with open('tests/images/orientation/landscape_7.jpg', 'rb') as f: image = PillowImage.open(JPEGImageFile(f)) self.assert_exif_orientation_equals_value(image, 7) image = image.auto_orient() self.assert_orientation_landscape_image_is_correct(image) self.assert_exif_orientation_equals_value(image, 1)
def test_save_as_jpeg_with_exif(self): with open('tests/images/trees.jpg', 'rb') as f: image = PillowImage.open(JPEGImageFile(f)) exif = PILImage.open(f).info.get('exif') self.assertIsNotNone(exif) saved = image.save_as_jpeg(io.BytesIO()) saved_exif = PILImage.open(saved.f).info.get('exif') self.assertEqual(saved_exif, exif)
def save_as_jpeg(self, f, quality=85, optimize=False, progressive=False): if self.image.mode in ['1', 'P']: image = self.image.convert('RGB') else: image = self.image # Pillow only checks presence of optimize kwarg, not its value kwargs = {} if optimize: kwargs['optimize'] = True if progressive: kwargs['progressive'] = True image.save(f, 'JPEG', quality=quality, **kwargs) return JPEGImageFile(f)
def setUp(self): with open('tests/images/people.jpg', 'rb') as f: # Open the image via Pillow pillow_image = PillowImage.open(JPEGImageFile(f)) buffer_rgb = pillow_image.to_buffer_rgb() colour_image = OpenCVColorImage.from_buffer_rgb(buffer_rgb) self.image = OpenCVGrayscaleImage.from_color(colour_image) self.expected_features = [[41.0, 206.0], [16.0, 201.0], [243.0, 208.0], [79.0, 130.0], [120.0, 24.0], [43.0, 119.0], [40.0, 165.0], [37.0, 14.0], [250.0, 59.0], [98.0, 6.0], [78.0, 61.0], [201.0, 93.0], [8.0, 114.0], [189.0, 142.0], [292.0, 188.0], [201.0, 199.0], [7.0, 154.0], [198.0, 247.0], [235.0, 55.0], [22.0, 36.0]] self.expected_faces = [(272, 89, 364, 181), (91, 165, 187, 261)]
def save_as_jpeg(self, f, quality=85, optimize=False, progressive=False): if self.image.mode in ['1', 'P']: image = self.image.convert('RGB') else: image = self.image # Pillow only checks presence of optimize kwarg, not its value kwargs = {} if optimize: kwargs['optimize'] = True if progressive: kwargs['progressive'] = True # copy the icc_profile kwargs['icc_profile'] = image.info.get('icc_profile') # copy the exif info exif = image.info.get('exif') if exif: kwargs['exif'] = exif image.save(f, 'JPEG', quality=quality, **kwargs) return JPEGImageFile(f)
def save_as_jpeg(self, f, quality=85): with self.image.convert('jpeg') as converted: converted.compression_quality = quality converted.save(file=f) return JPEGImageFile(f)