|
| 1 | +import json |
| 2 | +from typing import Any, BinaryIO |
| 3 | + |
| 4 | +from abstract_api.bases import BaseService |
| 5 | +from abstract_api.exceptions import ClientRequestError, ResponseParseError |
| 6 | + |
| 7 | +from .image_processing_response import ImageProcessingResponse |
| 8 | +from .strategies import BaseStrategy |
| 9 | + |
| 10 | + |
| 11 | +class ImageProcessing(BaseService): |
| 12 | + """AbstractAPI image processing service. |
| 13 | +
|
| 14 | + Used to convert, compress, or optimize an image. |
| 15 | +
|
| 16 | + Attributes: |
| 17 | + _subdomain: Image processing service subdomain. |
| 18 | + """ |
| 19 | + _subdomain: str = "images" |
| 20 | + |
| 21 | + def upload( |
| 22 | + self, |
| 23 | + image: BinaryIO, |
| 24 | + lossy: bool | None = None, |
| 25 | + quality: int | None = None, |
| 26 | + resize: BaseStrategy | None = None |
| 27 | + ) -> ImageProcessingResponse: |
| 28 | + """Can convert, compress, or optimize an image. |
| 29 | +
|
| 30 | + Args: |
| 31 | + image: The image to be processed, it should be a file-like or a |
| 32 | + file opened in binary reading mode. |
| 33 | + lossy: If True, the API will perform a lossy compression on the |
| 34 | + image, reducing the size massively with a small drop in |
| 35 | + image quality. If False, the image size will only be reduced |
| 36 | + slightly (10% - 20% at most), but there will be no reduction |
| 37 | + in image quality. The default value is False if this is not |
| 38 | + provided. |
| 39 | + quality: This is an integer between 0 and 100 that determines |
| 40 | + the quality level for lossy compression. If not submitted |
| 41 | + it will be determined intelligently by AbstractAPI. |
| 42 | + Generally a quality above 95 is useless and may result in |
| 43 | + an image that is larger than the input image, and a quality |
| 44 | + below 25 will result in an image so low in quality that it |
| 45 | + will be useless. |
| 46 | + resize: This is an instance of BaseStrategy subclasses that |
| 47 | + specifies how to resize the image. If not provided, we will |
| 48 | + only compress the image as desired. |
| 49 | +
|
| 50 | + Returns: |
| 51 | + ImageProcessingResponse representing API call response. |
| 52 | + """ |
| 53 | + return self._process( |
| 54 | + image=image, |
| 55 | + lossy=lossy, |
| 56 | + quality=quality, |
| 57 | + resize=resize |
| 58 | + ) |
| 59 | + |
| 60 | + def url( |
| 61 | + self, |
| 62 | + url: str, |
| 63 | + lossy: bool | None = None, |
| 64 | + quality: int | None = None, |
| 65 | + resize: BaseStrategy | None = None |
| 66 | + ) -> ImageProcessingResponse: |
| 67 | + """Can convert, compress, or optimize an image in the given URL. |
| 68 | +
|
| 69 | + Args: |
| 70 | + url: The URL of the image that you would like to edit. |
| 71 | + Note that is cannot be more than 32 MB in size. |
| 72 | + lossy: If True, the API will perform a lossy compression on the |
| 73 | + image, reducing the size massively with a small drop in |
| 74 | + image quality. If False, the image size will only be reduced |
| 75 | + slightly (10% - 20% at most), but there will be no reduction |
| 76 | + in image quality. The default value is False if this is not |
| 77 | + provided. |
| 78 | + quality: This is an integer between 0 and 100 that determines |
| 79 | + the quality level for lossy compression. If not submitted |
| 80 | + it will be determined intelligently by AbstractAPI. |
| 81 | + Generally a quality above 95 is useless and may result in |
| 82 | + an image that is larger than the input image, and a quality |
| 83 | + below 25 will result in an image so low in quality that it |
| 84 | + will be useless. |
| 85 | + resize: This is an instance of BaseStrategy subclasses that |
| 86 | + specifies how to resize the image. If not provided, we will |
| 87 | + only compress the image as desired. |
| 88 | +
|
| 89 | + Returns: |
| 90 | + ImageProcessingResponse representing API call response. |
| 91 | + """ |
| 92 | + return self._process( |
| 93 | + url=url, |
| 94 | + lossy=lossy, |
| 95 | + quality=quality, |
| 96 | + resize=resize |
| 97 | + ) |
| 98 | + |
| 99 | + def _process( |
| 100 | + self, |
| 101 | + image: BinaryIO | None = None, |
| 102 | + url: str | None = None, |
| 103 | + lossy: bool | None = None, |
| 104 | + quality: int | None = None, |
| 105 | + resize: BaseStrategy | None = None |
| 106 | + ) -> ImageProcessingResponse: |
| 107 | + |
| 108 | + if image is None and url is None: |
| 109 | + raise ClientRequestError("Image or URL must be passed") |
| 110 | + |
| 111 | + data: dict[str, Any] = {"api_key": self._api_key} |
| 112 | + if resize is not None: |
| 113 | + data["resize"] = resize.json() |
| 114 | + if lossy is not None: |
| 115 | + data["lossy"] = lossy |
| 116 | + if quality is not None: |
| 117 | + data["quality"] = quality |
| 118 | + |
| 119 | + action = "upload/" if image is not None else "url/" |
| 120 | + service_kwargs: dict[str, Any] = { |
| 121 | + "_action": action, |
| 122 | + "_method": "POST" |
| 123 | + } |
| 124 | + if action == "upload/": |
| 125 | + service_kwargs["_files"] = { |
| 126 | + "image": image, |
| 127 | + "data": (None, json.dumps(data)) |
| 128 | + } |
| 129 | + else: |
| 130 | + service_kwargs["_body"] = data | {"url": url} |
| 131 | + |
| 132 | + response = self._service_request(**service_kwargs) |
| 133 | + |
| 134 | + try: |
| 135 | + image_processing_response = ImageProcessingResponse( |
| 136 | + response=response |
| 137 | + ) |
| 138 | + except Exception as e: |
| 139 | + raise ResponseParseError( |
| 140 | + "Failed to parse response as ImageProcessingResponse" |
| 141 | + ) from e |
| 142 | + |
| 143 | + return image_processing_response |
0 commit comments