CivArchive
    ← All articles
    Published September 21, 2024by zentrocdot

    Image to Tensor and Tensor to Image

    233 views2 reactions0 comments on CivitAI0 collected
    tensortool guideimage

    Foreword

    While working on models I asked myself if image data can be in principle stored in Tensors. The answer can be found in what follows.

    Example

    I am using one of my images for the following explanations:

    Image to Tensor and Back

    I will show that an arbitrary image can be transformed in an Tensor and afterwards can be retrieved as image back again.

    The following Python3 script

    #!/usr/bin/python3
    
    # Import the required Python modules.
    from torchvision.transforms import transforms
    from PIL import Image
    
    # Set the file names.
    FN_IN = "sample.jpeg"
    FN_OUT = "sample_new.jpeg"
    
    # Read a PIL image.
    image_in = Image.open(FN_IN)
    
    # Define a transform to convert a PIL image into a Torch tensor.
    transform_tensor = transforms.Compose([transforms.PILToTensor()])
    
    # Convert the PIL image into a Torch tensor
    image_tensor = transform_tensor(image_in)
    
    # Print the converted Torch tensor in short form into the screen.
    print(image_tensor)
    
    # Define a transform to convert the Torch tensor into a PIL image.
    transform_image = transforms.ToPILImage()
    
    # Create a new PIL image.
    image_out = transform_image(image_tensor)
    
    # Show the PIL image.
    image_out.show()
    
    # Save the PIL image.
    image_out.save(FN_OUT, "jpeg")

    creates a Torch tensor like the ones in the given models we all deal with

    tensor([[[ 17,  16,  15,  ...,  12,  12,  12],
             [ 16,  16,  15,  ...,  11,  11,  11],
             [ 15,  15,  15,  ...,  10,  10,  10],
             ...,
             [158, 158, 158,  ..., 167, 166, 166],
             [159, 159, 158,  ..., 167, 166, 166],
             [161, 160, 159,  ..., 167, 167, 167]],
             [[ 18,  17,  16,  ...,  13,  13,  13],
             [ 17,  17,  16,  ...,  12,  12,  12],
             [ 16,  16,  16,  ...,  11,  11,  11],
             ...,
             [168, 168, 168,  ..., 180, 179, 179],
             [169, 169, 168,  ..., 180, 179, 179],
             [171, 170, 169,  ..., 180, 180, 180]],
             [[ 22,  21,  20,  ...,  15,  15,  15],
             [ 21,  21,  20,  ...,  14,  14,  14],
             [ 20,  20,  20,  ...,  13,  13,  13],
             ...,
             [178, 178, 178,  ..., 189, 188, 188],
             [179, 179, 178,  ..., 189, 188, 188],
             [181, 180, 179,  ..., 189, 189, 189]]], dtype=torch.uint8)

    and then it recreates my input image

    from the image I used for writing the Torch tensor.

    Side Note

    In [2] there is a Bash script called image_difference.bash which compares both images to check if input and output image nearly the same images.

    Conclusion

    It is theoretically possible that image data can be found in .saftensor files.

    Finally

    Have a nice day. Have fun. Be inspired!

    Resources

    [1] https://github.com/zentrocdot/artificial-intelligence-tools/tree/main/python/image2image

    [2] https://github.com/zentrocdot/artificial-intelligence-tools/tree/main/bash