def validate_options(opts): opts = Image._normalize_options(opts) if opts["mode"] not in Image.MODES: raise errors.ModeError("Invalid mode: %s" % opts["mode"]) elif opts["filter"] not in Image.FILTERS: raise errors.FilterError("Invalid filter: %s" % opts["filter"]) elif opts["format"] and opts["format"] not in Image.FORMATS: raise errors.FormatError("Invalid format: %s" % opts["format"]) elif opts["position"] not in Image.POSITIONS \ and not opts["pil"]["position"]: raise errors.PositionError( "Invalid position: %s" % opts["position"]) elif not Image._isint(opts["background"], 16) \ or len(opts["background"]) not in [3, 4, 6, 8]: raise errors.BackgroundError( "Invalid background: %s" % opts["background"]) elif opts["optimize"] and not Image._isint(opts["optimize"]): raise errors.OptimizeError( "Invalid optimize: %s", str(opts["optimize"])) elif opts["quality"] != "keep" \ and (not Image._isint(opts["quality"]) or int(opts["quality"]) > 100 or int(opts["quality"]) < 0): raise errors.QualityError( "Invalid quality: %s", str(opts["quality"])) elif opts["progressive"] and not Image._isint(opts["progressive"]): raise errors.ProgressiveError( "Invalid progressive: %s", str(opts["progressive"]))
def validate_options(opts): opts = Image._normalize_options(opts) if opts["mode"] not in Image.MODES: raise errors.ModeError("Invalid mode: %s" % opts["mode"]) elif opts["filter"] not in Image.FILTERS: raise errors.FilterError("Invalid filter: %s" % opts["filter"]) elif opts["format"] and opts["format"] not in Image.FORMATS: raise errors.FormatError("Invalid format: %s" % opts["format"]) elif opts["position"] not in Image.POSITIONS \ and not opts["pil"]["position"]: raise errors.PositionError("Invalid position: %s" % opts["position"]) elif not Image._isint(opts["background"], 16) \ or len(opts["background"]) not in [3, 4, 6, 8]: raise errors.BackgroundError("Invalid background: %s" % opts["background"]) elif opts["optimize"] and not Image._isint(opts["optimize"]): raise errors.OptimizeError("Invalid optimize: %s", str(opts["optimize"])) elif opts["quality"] != "keep" \ and (not Image._isint(opts["quality"]) or int(opts["quality"]) > 100 or int(opts["quality"]) < 0): raise errors.QualityError("Invalid quality: %s", str(opts["quality"])) elif opts["progressive"] and not Image._isint(opts["progressive"]): raise errors.ProgressiveError("Invalid progressive: %s", str(opts["progressive"]))
def validate_options(opts): opts = Image._normalize_options(opts) if opts["mode"] not in Image.MODES: raise ModeError("Invalid mode: %s" % opts["mode"]) elif opts["filter"] not in Image.FILTERS: raise FilterError("Invalid filter: %s" % opts["filter"]) elif opts["position"] not in Image.POSITIONS: raise PositionError("Invalid position: %s" % opts["position"]) elif not Image._isint(opts["background"], 16) \ or len(opts["background"]) not in [3, 4, 6, 8]: raise BackgroundError("Invalid background: %s" % opts["background"]) elif not Image._isint(opts["quality"]) \ or int(opts["quality"]) > 100 or int(opts["quality"]) < 0: raise QualityError("Invalid quality: %s", str(opts["quality"]))
def validate_options(opts): opts = Image._normalize_options(opts) if opts["mode"] not in Image.MODES: raise errors.ModeError("Invalid mode: %s" % opts["mode"]) elif opts["filter"] not in Image.FILTERS: raise errors.FilterError("Invalid filter: %s" % opts["filter"]) elif opts["format"] and opts["format"] not in Image.FORMATS: raise errors.FormatError("Invalid format: %s" % opts["format"]) elif opts["position"] not in Image.POSITIONS \ and not opts["pil"]["position"]: raise errors.PositionError("Invalid position: %s" % opts["position"]) elif not Image._isint(opts["background"], 16) \ or len(opts["background"]) not in [3, 4, 6, 8]: raise errors.BackgroundError("Invalid background: %s" % opts["background"]) elif opts["optimize"] and not Image._isint(opts["optimize"]): raise errors.OptimizeError("Invalid optimize: %s", str(opts["optimize"])) elif opts["quality"] != "keep" and \ (not Image._isint(opts["quality"]) or int(opts["quality"]) > 100 or int(opts["quality"]) < 0): raise errors.QualityError("Invalid quality: %s", str(opts["quality"])) elif opts["preserve_exif"] and not Image._isint(opts["preserve_exif"]): raise errors.PreserveExifError("Invalid preserve_exif: %s" % str(opts["preserve_exif"])) elif opts["progressive"] and not Image._isint(opts["progressive"]): raise errors.ProgressiveError("Invalid progressive: %s", str(opts["progressive"])) elif (not Image._isint(opts["retain"]) or int(opts["retain"]) > 100 or int(opts["retain"]) < 0): raise errors.RetainError("Invalid retain: %s" % str(opts["retain"])) elif "watermark_pos" in opts and not Image._istuple( opts["watermark_pos"]): raise errors.WatermarkPosError("Invalid watermark_pos: %s" % str(opts["watermark_pos"])) elif ("watermark_txt_size" in opts and not Image._isint(opts["watermark_txt_size"]) or "watermark_txt_size" in opts and int(opts["watermark_txt_size"]) > 100 or "watermark_txt_size" in opts and int(opts["watermark_txt_size"]) < 0): raise errors.WatermarkTxtError("Invalid watermark_txt_size: %s" % str(opts["watermark_txt_size"])) elif ("watermark_img_ratio" in opts and not Image._isint(opts["watermark_img_ratio"]) or "watermark_img_ratio" in opts and int(opts["watermark_img_ratio"]) > 100 or "watermark_img_ratio" in opts and int(opts["watermark_img_ratio"]) < 0): raise errors.WatermarkImgError("Invalid watermark_img_ratio: %s" % str(opts["watermark_img_ratio"])) elif "watermark_txt_color" in opts and opts[ "watermark_txt_color"] not in Image.COLORS: raise errors.WatermarkTxtError("Invalid watermark_txt_color: %s" % opts["watermark_txt_color"])
def validate_degree(deg): if deg is None or deg == "": raise errors.DegreeError("Missing degree") elif not Image._isint(deg): raise errors.DegreeError("Invalid degree: %s" % deg) elif int(deg) < 0 or int(deg) >= 360: raise errors.DegreeError("Invalid degree: %s" % deg)
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): 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 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 validate_degree(deg): if deg is None or deg == "": raise errors.DegreeError("Missing degree") elif deg == "auto": return elif not Image._isint(deg): raise errors.DegreeError("Invalid degree: {}".format(deg)) elif int(deg) < 0 or int(deg) >= 360: raise errors.DegreeError("Invalid degree: {}".format(deg))
def validate_rectangle(rect): if not rect: raise errors.RectangleError("Missing rectangle") rect = rect.split(",") if len(rect) != 4: raise errors.RectangleError("Invalid rectangle") for a in rect: if not Image._isint(a): raise errors.RectangleError("Invalid rectangle") elif int(a) < 0: raise errors.RectangleError("Region out-of-bounds")
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
def validate_options(opts): opts = Image._normalize_options(opts) if opts["mode"] not in Image.MODES: raise errors.ModeError("Invalid mode: {}".format(opts["mode"])) elif opts["filter"] not in Image.FILTERS: raise errors.FilterError("Invalid filter: {}".format(opts["filter"])) elif opts["format"] and opts["format"] not in Image.FORMATS: raise errors.FormatError("Invalid format: {}".format(opts["format"])) elif opts["position"] not in Image.POSITIONS and not opts["pil"]["position"]: raise errors.PositionError("Invalid position: {}".format(opts["position"])) elif not Image._isint(opts["background"], 16) or len(opts["background"]) not in [3, 4, 6, 8]: raise errors.BackgroundError("Invalid background: {}".format(opts["background"])) elif opts["optimize"] and not Image._isint(opts["optimize"]): raise errors.OptimizeError("Invalid optimize: {}".format(str(opts["optimize"]))) elif opts["quality"] != "keep" and (not Image._isint(opts["quality"]) or int(opts["quality"]) > 100 or int(opts["quality"]) < 0): raise errors.QualityError("Invalid quality: {}".format(str(opts["quality"]))) elif opts["preserve_exif"] and not Image._isint(opts["preserve_exif"]): raise errors.PreserveExifError("Invalid preserve_exif: {}".format(str(opts["preserve_exif"]))) elif opts["progressive"] and not Image._isint(opts["progressive"]): raise errors.ProgressiveError("Invalid progressive: {}".format(str(opts["progressive"]))) elif (not Image._isint(opts["retain"]) or int(opts["retain"]) > 100 or int(opts["retain"]) < 0): raise errors.RetainError("Invalid retain: {}".format(str(opts["retain"])))