2023 the huggingface team licensed under the apache license version 2 0 the license you may not use this file except in compliance with the license you may obtain a copy of the license at http www apache orglicenseslicense2 0 unless required by applicable law or agreed to in writing software distributed under the license is distributed on an as is basis without warranties or conditions of any kind either express or implied see the license for the specific language governing permissions and limitations under the license image to image pipeline using any automodelforimagetoimage this pipeline generates an image based on a previous image input example python from pil import image import requests from transformers import pipeline upscaler pipelineimagetoimage modelcaidasswin2srclassicalsrx264 img image openrequests gethttp images cocodataset orgval2017000000039769 jpg streamtrue raw img img resize64 64 upscaledimg upscalerimg img size 64 64 upscaledimg size 144 144 this image to image pipeline can currently be loaded from pipeline using the following task identifier imagetoimage see the list of available models on huggingface comodelshttps huggingface comodels filterimagetoimage transform the images passed as inputs args images str liststr pil image or listpil image the pipeline handles three types of images a string containing a http link pointing to an image a string containing a local path to an image an image loaded in pil directly the pipeline accepts either a single image or a batch of images which must then be passed as a string images in a batch must all be in the same format all as http links all as local paths or all as pil images timeout float optional defaults to none the maximum time in seconds to wait for fetching images from the web if none no timeout is used and the call may block forever return an image image image or a list of images listimage image containing results if the input is a single image the return will be also a single image if the input is a list of several images it will return a list of transformed images 2023 the huggingface team licensed under the apache license version 2 0 the license you may not use this file except in compliance with the license you may obtain a copy of the license at http www apache org licenses license 2 0 unless required by applicable law or agreed to in writing software distributed under the license is distributed on an as is basis without warranties or conditions of any kind either express or implied see the license for the specific language governing permissions and limitations under the license image to image pipeline using any automodelforimagetoimage this pipeline generates an image based on a previous image input example python from pil import image import requests from transformers import pipeline upscaler pipeline image to image model caidas swin2sr classical sr x2 64 img image open requests get http images cocodataset org val2017 000000039769 jpg stream true raw img img resize 64 64 upscaled_img upscaler img img size 64 64 upscaled_img size 144 144 this image to image pipeline can currently be loaded from pipeline using the following task identifier image to image see the list of available models on huggingface co models https huggingface co models filter image to image transform the image s passed as inputs args images str list str pil image or list pil image the pipeline handles three types of images a string containing a http link pointing to an image a string containing a local path to an image an image loaded in pil directly the pipeline accepts either a single image or a batch of images which must then be passed as a string images in a batch must all be in the same format all as http links all as local paths or all as pil images timeout float optional defaults to none the maximum time in seconds to wait for fetching images from the web if none no timeout is used and the call may block forever return an image image image or a list of images list image image containing result s if the input is a single image the return will be also a single image if the input is a list of several images it will return a list of transformed images float32 to uint8
from typing import List, Union

import numpy as np

from ..utils import (
    add_end_docstrings,
    is_torch_available,
    is_vision_available,
    logging,
    requires_backends,
)
from .base import PIPELINE_INIT_ARGS, Pipeline


if is_vision_available():
    from PIL import Image

    from ..image_utils import load_image

if is_torch_available():
    from ..models.auto.modeling_auto import MODEL_FOR_IMAGE_TO_IMAGE_MAPPING_NAMES

logger = logging.get_logger(__name__)


@add_end_docstrings(PIPELINE_INIT_ARGS)
class ImageToImagePipeline(Pipeline):
    

    def __init__(self, *args, **kwargs):
        super().__init__(*args, **kwargs)
        requires_backends(self, "vision")
        self.check_model_type(MODEL_FOR_IMAGE_TO_IMAGE_MAPPING_NAMES)

    def _sanitize_parameters(self, **kwargs):
        preprocess_params = {}
        postprocess_params = {}
        forward_params = {}

        if "timeout" in kwargs:
            preprocess_params["timeout"] = kwargs["timeout"]
        if "head_mask" in kwargs:
            forward_params["head_mask"] = kwargs["head_mask"]

        return preprocess_params, forward_params, postprocess_params

    def __call__(
        self, images: Union[str, List[str], "Image.Image", List["Image.Image"]], **kwargs
    ) -> Union["Image.Image", List["Image.Image"]]:
        
        return super().__call__(images, **kwargs)

    def _forward(self, model_inputs):
        model_outputs = self.model(**model_inputs)
        return model_outputs

    def preprocess(self, image, timeout=None):
        image = load_image(image, timeout=timeout)
        inputs = self.image_processor(images=[image], return_tensors="pt")
        return inputs

    def postprocess(self, model_outputs):
        images = []
        if "reconstruction" in model_outputs.keys():
            outputs = model_outputs.reconstruction
        for output in outputs:
            output = output.data.squeeze().float().cpu().clamp_(0, 1).numpy()
            output = np.moveaxis(output, source=0, destination=-1)
            output = (output * 255.0).round().astype(np.uint8)  
            images.append(Image.fromarray(output))

        return images if len(images) > 1 else images[0]