def save(self, **kwargs): """Returns a buffer to the image for saving, supports the following optional keyword arguments: format - The format to save as: see Image.FORMATS optimize - The image file size should be optimized quality - The quality used to save JPEGs: integer from 1 - 100 """ opts = Image._normalize_options(kwargs) outfile = BytesIO() if opts["pil"]["format"]: fmt = opts["pil"]["format"] else: fmt = self._orig_format save_kwargs = dict(quality=int(opts["quality"])) if int(opts["optimize"]): save_kwargs["optimize"] = True try: self.img.save(outfile, fmt, **save_kwargs) except IOError as e: raise errors.ImageSaveError(str(e)) self.img.format = fmt outfile.seek(0) return outfile
def save(self, **kwargs): """Returns a buffer to the image for saving, supports the following optional keyword arguments: format - The format to save as: see Image.FORMATS optimize - The image file size should be optimized preserve_exif - Preserve the Exif information in JPEGs progressive - The output should be progressive JPEG quality - The quality used to save JPEGs: integer from 1 - 100 """ opts = Image._normalize_options(kwargs) outfile = BytesIO() if opts["pil"]["format"]: fmt = opts["pil"]["format"] else: fmt = self._orig_format save_kwargs = dict() if Image._isint(opts["quality"]): save_kwargs["quality"] = int(opts["quality"]) if int(opts["optimize"]): save_kwargs["optimize"] = True if int(opts["progressive"]): save_kwargs["progressive"] = True if int(opts["preserve_exif"]): save_kwargs["exif"] = self._exif color = color_hex_to_dec_tuple(opts["background"]) if self.img.mode == "RGBA": self._background(fmt, color) if fmt == "JPEG": if self.img.mode == "P": # Converting old GIF and PNG files to JPEG can raise # IOError: cannot write mode P as JPEG # https://mail.python.org/pipermail/python-list/2000-May/036017.html self.img = self.img.convert("RGB") elif self.img.mode == "RGBA": # JPEG does not have an alpha channel so cannot be # saved as RGBA. It must be converted to RGB. self.img = self.img.convert("RGB") if self._orig_format == "JPEG": self.img.format = self._orig_format save_kwargs["subsampling"] = "keep" if opts["quality"] == "keep": save_kwargs["quality"] = "keep" try: self.img.save(outfile, fmt, **save_kwargs) except IOError as e: raise errors.ImageSaveError(str(e)) self.img.format = fmt outfile.seek(0) return outfile
def save(self, **kwargs): """Returns a buffer to the image for saving, supports the following optional keyword arguments: format - The format to save as: see Image.FORMATS optimize - The image file size should be optimized preserve_exif - Preserve the Exif information in JPEGs progressive - The output should be progressive JPEG quality - The quality used to save JPEGs: integer from 1 - 100 """ opts = Image._normalize_options(kwargs) outfile = BytesIO() if opts["pil"]["format"]: fmt = opts["pil"]["format"] else: fmt = self._orig_format save_kwargs = dict() if Image._isint(opts["quality"]): save_kwargs["quality"] = int(opts["quality"]) if int(opts["optimize"]): save_kwargs["optimize"] = True if int(opts["progressive"]): save_kwargs["progressive"] = True if int(opts["preserve_exif"]): save_kwargs["exif"] = self._exif color = color_hex_to_dec_tuple(opts["background"]) if self.img.mode == "RGBA": self._background(fmt, color) if self._orig_format == "JPEG": self.img.format = self._orig_format save_kwargs["subsampling"] = "keep" if opts["quality"] == "keep": save_kwargs["quality"] = "keep" try: self.img.save(outfile, fmt, **save_kwargs) except IOError as e: raise errors.ImageSaveError(str(e)) self.img.format = fmt outfile.seek(0) return outfile