def scale(self, data, w, h, default_format='PNG'): """ use our quality setting as pil_quality """ pil_quality = getQuality() # make sure we have valid int's size = int(w), int(h) original_file = StringIO(data) image = PIL.Image.open(original_file) if image.format == 'GIF' and size[0] >= image.size[0] \ and size[1] >= image.size[1]: try: image.seek(image.tell() + 1) # original image is animated GIF and no bigger than the scale # requested # don't attempt to scale as this will lose animation original_file.seek(0) return original_file, 'gif' except EOFError: # image is not animated image.seek(0) # consider image mode when scaling # source images can be mode '1','L,','P','RGB(A)' # convert to greyscale or RGBA before scaling # preserve palletted mode (but not pallette) # for palletted-only image formats, e.g. GIF # PNG compression is OK for RGBA thumbnails original_mode = image.mode img_format = image.format and image.format or default_format if img_format in ('TIFF', 'EPS', 'PSD'): # non web image format have jpeg thumbnails target_format = 'JPEG' else: target_format = img_format if original_mode == '1': image = image.convert('L') elif original_mode == 'P': image = image.convert('RGBA') elif original_mode == 'CMYK': image = image.convert('RGBA') image.thumbnail(size, self.pil_resize_algo) # decided to only preserve palletted mode # for GIF, could also use image.format in ('GIF','PNG') if original_mode == 'P' and img_format == 'GIF': image = image.convert('P') # Avoid - IOError: cannot write mode RGBA as JPEG # See https://github.com/python-pillow/Pillow/issues/2609#issuecomment-313841918 if original_mode in ('CMYK', 'RGBA', 'LA') and target_format in ('JPG', 'JPEG'): target_format = 'PNG' thumbnail_file = StringIO() # quality parameter doesn't affect lossless formats image.save(thumbnail_file, target_format, quality=pil_quality, progressive=True) thumbnail_file.seek(0) return thumbnail_file, target_format.lower()
def scale(self, data, w, h, default_format='PNG'): """ use our quality setting as pil_quality """ pil_quality = getQuality() #make sure we have valid int's size = int(w), int(h) original_file = StringIO(data) image = PIL.Image.open(original_file) if image.format == 'GIF' and size[0] >= image.size[0] \ and size[1] >= image.size[1]: try: image.seek(image.tell() + 1) # original image is animated GIF and no bigger than the scale # requested # don't attempt to scale as this will lose animation original_file.seek(0) return original_file, 'gif' except EOFError: # image is not animated image.seek(0) # consider image mode when scaling # source images can be mode '1','L,','P','RGB(A)' # convert to greyscale or RGBA before scaling # preserve palletted mode (but not pallette) # for palletted-only image formats, e.g. GIF # PNG compression is OK for RGBA thumbnails original_mode = image.mode img_format = image.format and image.format or default_format if img_format in ('TIFF', 'EPS', 'PSD'): # non web image format have jpeg thumbnails target_format = 'JPEG' else: target_format = img_format if original_mode == '1': image = image.convert('L') elif original_mode == 'P': image = image.convert('RGBA') elif original_mode == 'CMYK': image = image.convert('RGBA') image.thumbnail(size, self.pil_resize_algo) # decided to only preserve palletted mode # for GIF, could also use image.format in ('GIF','PNG') if original_mode == 'P' and img_format == 'GIF': image = image.convert('P') thumbnail_file = StringIO() # quality parameter doesn't affect lossless formats image.save(thumbnail_file, target_format, quality=pil_quality, progressive=True) thumbnail_file.seek(0) return thumbnail_file, target_format.lower()
def testQuality(self): self.assertEqual(getQuality(), 88) settings = getSettings() settings.quality = 42 self.assertEqual(getQuality(), 42)
def plone_app_imaging_scale(self, data, w, h, default_format='PNG'): """ use our quality setting as pil_quality """ pil_quality = getQuality() #make sure we have valid int's size = int(w), int(h) original_file = StringIO(data) image = PIL.Image.open(original_file) if image.format == 'GIF' and size[0] >= image.size[0] \ and size[1] >= image.size[1]: try: image.seek(image.tell() + 1) # original image is animated GIF and no bigger than the scale # requested # don't attempt to scale as this will lose animation original_file.seek(0) return original_file, 'gif' except EOFError: # image is not animated image.seek(0) # consider image mode when scaling # source images can be mode '1','L,','P','RGB(A)' # convert to greyscale or RGBA before scaling # preserve palletted mode (but not pallette) # for palletted-only image formats, e.g. GIF # PNG compression is OK for RGBA thumbnails original_mode = image.mode img_format = image.format and image.format or default_format if img_format in ('TIFF', 'EPS', 'PSD'): # non web image format have jpeg thumbnails target_format = 'JPEG' else: target_format = img_format if original_mode == '1': image = image.convert('L') elif original_mode == 'P': image = image.convert('RGBA') elif original_mode == 'CMYK': image = image.convert('RGBA') #### custom code ###### #does not work for sizes='instanceMethod' since we don't have an instance here availableSizes = self.getAvailableSizes(None) if safe_hasattr(self, 'crop_scales'): #if our field defines crop_scales let's see if the current sizes shall be cropped if size in [availableSizes[name] for name in self.crop_scales]: image = crop(image, size) #### custom code ####### image.thumbnail(size, self.pil_resize_algo) # decided to only preserve palletted mode # for GIF, could also use image.format in ('GIF','PNG') if original_mode == 'P' and img_format == 'GIF': image = image.convert('P') thumbnail_file = StringIO() # quality parameter doesn't affect lossless formats image.save(thumbnail_file, target_format, quality=pil_quality, progressive=True) thumbnail_file.seek(0) return thumbnail_file, target_format.lower()
def testQuality(self): self.assertEqual(getQuality(), 88) # change and test again iprops = self.portal.portal_properties.imaging_properties iprops.manage_changeProperties(quality='42') self.assertEqual(getQuality(), 42)