utilities related to image handling import io import pathlib import warnings import numpy as np from keras import backend from keras apiexport import kerasexport try from pil import image as pilimage try pilimageresampling pilimage resampling except attributeerror pilimageresampling pilimage except importerror pilimage none pilimageresampling none if pilimageresampling is not none pilinterpolationmethods nearest pilimageresampling nearest bilinear pilimageresampling bilinear bicubic pilimageresampling bicubic hamming pilimageresampling hamming box pilimageresampling box lanczos pilimageresampling lanczos kerasexport keras utils arraytoimg keras preprocessing image arraytoimg def arraytoimgx dataformatnone scaletrue dtypenone dataformat backend standardizedataformatdataformat if dtype is none dtype backend floatx if pilimage is none raise importerror could not import pil image the use of arraytoimg requires pil x np asarrayx dtypedtype if x ndim 3 raise valueerror expected image array to have rank 3 single image fgot array with shape x shape original numpy array x has format height width channel or channel height width but target pil image has format width height channel if dataformat channelsfirst x x transpose1 2 0 if scale x x np minx xmax np maxx if xmax 0 x xmax x 255 if x shape2 4 rgba return pilimage fromarrayx astypeuint8 rgba elif x shape2 3 rgb return pilimage fromarrayx astypeuint8 rgb elif x shape2 1 grayscale if np maxx 255 32bit signed integer grayscale image pil mode i return pilimage fromarrayx 0 astypeint32 i return pilimage fromarrayx 0 astypeuint8 l else raise valueerrorfunsupported channel number x shape2 kerasexport keras utils imgtoarray keras preprocessing image imgtoarray def imgtoarrayimg dataformatnone dtypenone dataformat backend standardizedataformatdataformat if dtype is none dtype backend floatx numpy array x has format height width channel or channel height width but original pil image has format width height channel x np asarrayimg dtypedtype if lenx shape 3 if dataformat channelsfirst x x transpose2 0 1 elif lenx shape 2 if dataformat channelsfirst x x reshape1 x shape0 x shape1 else x x reshapex shape0 x shape1 1 else raise valueerrorfunsupported image shape x shape return x kerasexportkeras utils saveimg keras preprocessing image saveimg def saveimgpath x dataformatnone fileformatnone scaletrue kwargs dataformat backend standardizedataformatdataformat img arraytoimgx dataformatdataformat scalescale if img mode rgba and fileformat jpg or fileformat jpeg warnings warn the jpg format does not support rgba images converting to rgb img img convertrgb img savepath formatfileformat kwargs kerasexportkeras utils loadimg keras preprocessing image loadimg def loadimg path colormodergb targetsizenone interpolationnearest keepaspectratiofalse if pilimage is none raise importerror could not import pil image the use of loadimg requires pil if isinstancepath io bytesio img pilimage openpath elif isinstancepath pathlib path bytes str if isinstancepath pathlib path path strpath resolve with openpath rb as f img pilimage openio bytesiof read else raise typeerror fpath should be pathlike or io bytesio not typepath if colormode grayscale if image is not already an 8bit 16bit or 32bit grayscale image convert it to an 8bit grayscale image if img mode not in l i 16 i img img convertl elif colormode rgba if img mode rgba img img convertrgba elif colormode rgb if img mode rgb img img convertrgb else raise valueerror colormode must be grayscale rgb or rgba if targetsize is not none widthheighttuple targetsize1 targetsize0 if img size widthheighttuple if interpolation not in pilinterpolationmethods raise valueerror invalid interpolation method specified supported methods are format interpolation joinpilinterpolationmethods keys resample pilinterpolationmethodsinterpolation if keepaspectratio width height img size targetwidth targetheight widthheighttuple cropheight width targetheight targetwidth cropwidth height targetwidth targetheight set back to input height width if cropheight cropwidth is not smaller cropheight minheight cropheight cropwidth minwidth cropwidth cropboxhstart height cropheight 2 cropboxwstart width cropwidth 2 cropboxwend cropboxwstart cropwidth cropboxhend cropboxhstart cropheight cropbox cropboxwstart cropboxhstart cropboxwend cropboxhend img img resizewidthheighttuple resample boxcropbox else img img resizewidthheighttuple resample return img kerasexportkeras preprocessing image smartresize def smartresize x size interpolationbilinear dataformatchannelslast backendmodulenone backendmodule backendmodule or backend if lensize 2 raise valueerror fexpected size to be a tuple of 2 integers but got size img backendmodule converttotensorx if lenimg shape is not none if lenimg shape 3 or lenimg shape 4 raise valueerror expected an image array with shape height width channels or batchsize height width channels but fgot input with incorrect rank of shape img shape shape backendmodule shapeimg if dataformat channelslast height width shape3 shape2 else height width shape2 shape1 targetheight targetwidth size set back to input height width if cropheight cropwidth is not smaller if isinstanceheight int and isinstancewidth int for jax we need to keep the slice indices as static integers cropheight intfloatwidth targetheight targetwidth cropheight minheight cropheight cropwidth intfloatheight targetwidth targetheight cropwidth minwidth cropwidth cropboxhstart intfloatheight cropheight 2 cropboxwstart intfloatwidth cropwidth 2 else cropheight backendmodule cast backendmodule castwidth targetheight float32 targetwidth int32 cropheight backendmodule numpy minimumheight cropheight cropheight backendmodule castcropheight int32 cropwidth backendmodule cast backendmodule castheight targetwidth float32 targetheight int32 cropwidth backendmodule numpy minimumwidth cropwidth cropwidth backendmodule castcropwidth int32 cropboxhstart backendmodule cast backendmodule castheight cropheight float32 2 int32 cropboxwstart backendmodule cast backendmodule castwidth cropwidth float32 2 int32 if dataformat channelslast if lenimg shape 4 img img cropboxhstart cropboxhstart cropheight cropboxwstart cropboxwstart cropwidth else img img cropboxhstart cropboxhstart cropheight cropboxwstart cropboxwstart cropwidth else if lenimg shape 4 img img cropboxhstart cropboxhstart cropheight cropboxwstart cropboxwstart cropwidth else img img cropboxhstart cropboxhstart cropheight cropboxwstart cropboxwstart cropwidth img backendmodule image resize img sizesize interpolationinterpolation dataformatdataformat if isinstancex np ndarray return np arrayimg return img converts a 3d numpy array to a pil image instance usage python from pil import image img np random random size 100 100 3 pil_img keras utils array_to_img img args x input data in any form that can be converted to a numpy array data_format image data format can be either channels_first or channels_last defaults to none in which case the global setting keras backend image_data_format is used unless you changed it it defaults to channels_last scale whether to rescale the image such that minimum and maximum values are 0 and 255 respectively defaults to true dtype dtype to use none means the global setting keras backend floatx is used unless you changed it it defaults to float32 defaults to none returns a pil image instance original numpy array x has format height width channel or channel height width but target pil image has format width height channel rgba rgb grayscale 32 bit signed integer grayscale image pil mode i converts a pil image instance to a numpy array usage python from pil import image img_data np random random size 100 100 3 img keras utils array_to_img img_data array keras utils image img_to_array img args img input pil image instance data_format image data format can be either channels_first or channels_last defaults to none in which case the global setting keras backend image_data_format is used unless you changed it it defaults to channels_last dtype dtype to use none means the global setting keras backend floatx is used unless you changed it it defaults to float32 returns a 3d numpy array numpy array x has format height width channel or channel height width but original pil image has format width height channel saves an image stored as a numpy array to a path or file object args path path or file object x numpy array data_format image data format either channels_first or channels_last file_format optional file format override if omitted the format to use is determined from the filename extension if a file object was used instead of a filename this parameter should always be used scale whether to rescale image values to be within 0 255 kwargs additional keyword arguments passed to pil image save loads an image into pil format usage python image keras utils load_img image_path input_arr keras utils img_to_array image input_arr np array input_arr convert single image to a batch predictions model predict input_arr args path path to image file color_mode one of grayscale rgb rgba default rgb the desired image format target_size either none default to original size or tuple of ints img_height img_width interpolation interpolation method used to resample the image if the target size is different from that of the loaded image supported methods are nearest bilinear and bicubic if pil version 1 1 3 or newer is installed lanczos is also supported if pil version 3 4 0 or newer is installed box and hamming are also supported by default nearest is used keep_aspect_ratio boolean whether to resize images to a target size without aspect ratio distortion the image is cropped in the center with target aspect ratio before resizing returns a pil image instance if image is not already an 8 bit 16 bit or 32 bit grayscale image convert it to an 8 bit grayscale image set back to input height width if crop_height crop_width is not smaller resize images to a target size without aspect ratio distortion image datasets typically yield images that have each a different size however these images need to be batched before they can be processed by keras layers to be batched images need to share the same height and width you could simply do in tf or jax equivalent python size 200 200 ds ds map lambda img resize img size however if you do this you distort the aspect ratio of your images since in general they do not all have the same aspect ratio as size this is fine in many cases but not always e g for image generation models this can be a problem note that passing the argument preserve_aspect_ratio true to resize will preserve the aspect ratio but at the cost of no longer respecting the provided target size this calls for python size 200 200 ds ds map lambda img smart_resize img size your output images will actually be 200 200 and will not be distorted instead the parts of the image that do not fit within the target size get cropped out the resizing process is 1 take the largest centered crop of the image that has the same aspect ratio as the target size for instance if size 200 200 and the input image has size 340 500 we take a crop of 340 340 centered along the width 2 resize the cropped image to the target size in the example above we resize the 340 340 crop to 200 200 args x input image or batch of images as a tensor or numpy array must be in format height width channels or batch_size height width channels size tuple of height width integer target size interpolation string interpolation to use for resizing defaults to bilinear supports bilinear nearest bicubic lanczos3 lanczos5 data_format channels_last or channels_first backend_module backend module to use if different from the default backend returns array with shape size 0 size 1 channels if the input image was a numpy array the output is a numpy array and if it was a backend native tensor the output is a backend native tensor set back to input height width if crop_height crop_width is not smaller for jax we need to keep the slice indices as static integers
import io
import pathlib
import warnings

import numpy as np

from keras import backend
from keras.api_export import keras_export

try:
    from PIL import Image as pil_image

    try:
        pil_image_resampling = pil_image.Resampling
    except AttributeError:
        pil_image_resampling = pil_image
except ImportError:
    pil_image = None
    pil_image_resampling = None


if pil_image_resampling is not None:
    PIL_INTERPOLATION_METHODS = {
        "nearest": pil_image_resampling.NEAREST,
        "bilinear": pil_image_resampling.BILINEAR,
        "bicubic": pil_image_resampling.BICUBIC,
        "hamming": pil_image_resampling.HAMMING,
        "box": pil_image_resampling.BOX,
        "lanczos": pil_image_resampling.LANCZOS,
    }


@keras_export(
    [
        "keras.utils.array_to_img",
        "keras.preprocessing.image.array_to_img",
    ]
)
def array_to_img(x, data_format=None, scale=True, dtype=None):
    

    data_format = backend.standardize_data_format(data_format)
    if dtype is None:
        dtype = backend.floatx()
    if pil_image is None:
        raise ImportError(
            "Could not import PIL.Image. "
            "The use of `array_to_img` requires PIL."
        )
    x = np.asarray(x, dtype=dtype)
    if x.ndim != 3:
        raise ValueError(
            "Expected image array to have rank 3 (single image). "
            f"Got array with shape: {x.shape}"
        )

    
    
    
    if data_format == "channels_first":
        x = x.transpose(1, 2, 0)
    if scale:
        x = x - np.min(x)
        x_max = np.max(x)
        if x_max != 0:
            x /= x_max
        x *= 255
    if x.shape[2] == 4:
        
        return pil_image.fromarray(x.astype("uint8"), "RGBA")
    elif x.shape[2] == 3:
        
        return pil_image.fromarray(x.astype("uint8"), "RGB")
    elif x.shape[2] == 1:
        
        if np.max(x) > 255:
            
            return pil_image.fromarray(x[:, :, 0].astype("int32"), "I")
        return pil_image.fromarray(x[:, :, 0].astype("uint8"), "L")
    else:
        raise ValueError(f"Unsupported channel number: {x.shape[2]}")


@keras_export(
    [
        "keras.utils.img_to_array",
        "keras.preprocessing.image.img_to_array",
    ]
)
def img_to_array(img, data_format=None, dtype=None):
    

    data_format = backend.standardize_data_format(data_format)
    if dtype is None:
        dtype = backend.floatx()
    
    
    
    x = np.asarray(img, dtype=dtype)
    if len(x.shape) == 3:
        if data_format == "channels_first":
            x = x.transpose(2, 0, 1)
    elif len(x.shape) == 2:
        if data_format == "channels_first":
            x = x.reshape((1, x.shape[0], x.shape[1]))
        else:
            x = x.reshape((x.shape[0], x.shape[1], 1))
    else:
        raise ValueError(f"Unsupported image shape: {x.shape}")
    return x


@keras_export(["keras.utils.save_img", "keras.preprocessing.image.save_img"])
def save_img(path, x, data_format=None, file_format=None, scale=True, **kwargs):
    
    data_format = backend.standardize_data_format(data_format)
    img = array_to_img(x, data_format=data_format, scale=scale)
    if img.mode == "RGBA" and (file_format == "jpg" or file_format == "jpeg"):
        warnings.warn(
            "The JPG format does not support RGBA images, converting to RGB."
        )
        img = img.convert("RGB")
    img.save(path, format=file_format, **kwargs)


@keras_export(["keras.utils.load_img", "keras.preprocessing.image.load_img"])
def load_img(
    path,
    color_mode="rgb",
    target_size=None,
    interpolation="nearest",
    keep_aspect_ratio=False,
):
    
    if pil_image is None:
        raise ImportError(
            "Could not import PIL.Image. The use of `load_img` requires PIL."
        )
    if isinstance(path, io.BytesIO):
        img = pil_image.open(path)
    elif isinstance(path, (pathlib.Path, bytes, str)):
        if isinstance(path, pathlib.Path):
            path = str(path.resolve())
        with open(path, "rb") as f:
            img = pil_image.open(io.BytesIO(f.read()))
    else:
        raise TypeError(
            f"path should be path-like or io.BytesIO, not {type(path)}"
        )

    if color_mode == "grayscale":
        
        
        if img.mode not in ("L", "I;16", "I"):
            img = img.convert("L")
    elif color_mode == "rgba":
        if img.mode != "RGBA":
            img = img.convert("RGBA")
    elif color_mode == "rgb":
        if img.mode != "RGB":
            img = img.convert("RGB")
    else:
        raise ValueError('color_mode must be "grayscale", "rgb", or "rgba"')
    if target_size is not None:
        width_height_tuple = (target_size[1], target_size[0])
        if img.size != width_height_tuple:
            if interpolation not in PIL_INTERPOLATION_METHODS:
                raise ValueError(
                    "Invalid interpolation method {} specified. Supported "
                    "methods are {}".format(
                        interpolation,
                        ", ".join(PIL_INTERPOLATION_METHODS.keys()),
                    )
                )
            resample = PIL_INTERPOLATION_METHODS[interpolation]

            if keep_aspect_ratio:
                width, height = img.size
                target_width, target_height = width_height_tuple

                crop_height = (width * target_height) // target_width
                crop_width = (height * target_width) // target_height

                
                
                crop_height = min(height, crop_height)
                crop_width = min(width, crop_width)

                crop_box_hstart = (height - crop_height) // 2
                crop_box_wstart = (width - crop_width) // 2
                crop_box_wend = crop_box_wstart + crop_width
                crop_box_hend = crop_box_hstart + crop_height
                crop_box = [
                    crop_box_wstart,
                    crop_box_hstart,
                    crop_box_wend,
                    crop_box_hend,
                ]
                img = img.resize(width_height_tuple, resample, box=crop_box)
            else:
                img = img.resize(width_height_tuple, resample)
    return img


@keras_export("keras.preprocessing.image.smart_resize")
def smart_resize(
    x,
    size,
    interpolation="bilinear",
    data_format="channels_last",
    backend_module=None,
):
    
    backend_module = backend_module or backend
    if len(size) != 2:
        raise ValueError(
            f"Expected `size` to be a tuple of 2 integers, but got: {size}."
        )
    img = backend_module.convert_to_tensor(x)
    if len(img.shape) is not None:
        if len(img.shape) < 3 or len(img.shape) > 4:
            raise ValueError(
                "Expected an image array with shape `(height, width, "
                "channels)`, or `(batch_size, height, width, channels)`, but "
                f"got input with incorrect rank, of shape {img.shape}."
            )
    shape = backend_module.shape(img)
    if data_format == "channels_last":
        height, width = shape[-3], shape[-2]
    else:
        height, width = shape[-2], shape[-1]
    target_height, target_width = size

    
    
    if isinstance(height, int) and isinstance(width, int):
        
        crop_height = int(float(width * target_height) / target_width)
        crop_height = min(height, crop_height)
        crop_width = int(float(height * target_width) / target_height)
        crop_width = min(width, crop_width)
        crop_box_hstart = int(float(height - crop_height) / 2)
        crop_box_wstart = int(float(width - crop_width) / 2)
    else:
        crop_height = backend_module.cast(
            backend_module.cast(width * target_height, "float32")
            / target_width,
            "int32",
        )
        crop_height = backend_module.numpy.minimum(height, crop_height)
        crop_height = backend_module.cast(crop_height, "int32")
        crop_width = backend_module.cast(
            backend_module.cast(height * target_width, "float32")
            / target_height,
            "int32",
        )
        crop_width = backend_module.numpy.minimum(width, crop_width)
        crop_width = backend_module.cast(crop_width, "int32")

        crop_box_hstart = backend_module.cast(
            backend_module.cast(height - crop_height, "float32") / 2, "int32"
        )
        crop_box_wstart = backend_module.cast(
            backend_module.cast(width - crop_width, "float32") / 2, "int32"
        )

    if data_format == "channels_last":
        if len(img.shape) == 4:
            img = img[
                :,
                crop_box_hstart : crop_box_hstart + crop_height,
                crop_box_wstart : crop_box_wstart + crop_width,
                :,
            ]
        else:
            img = img[
                crop_box_hstart : crop_box_hstart + crop_height,
                crop_box_wstart : crop_box_wstart + crop_width,
                :,
            ]
    else:
        if len(img.shape) == 4:
            img = img[
                :,
                :,
                crop_box_hstart : crop_box_hstart + crop_height,
                crop_box_wstart : crop_box_wstart + crop_width,
            ]
        else:
            img = img[
                :,
                crop_box_hstart : crop_box_hstart + crop_height,
                crop_box_wstart : crop_box_wstart + crop_width,
            ]

    img = backend_module.image.resize(
        img, size=size, interpolation=interpolation, data_format=data_format
    )

    if isinstance(x, np.ndarray):
        return np.array(img)
    return img