|
import torch |
|
import numpy as np |
|
from PIL import Image |
|
from torchvision.transforms import Compose, Resize, Grayscale, ToTensor, ToPILImage |
|
|
|
|
|
|
|
transform_gs = Compose( |
|
[Resize((128, 128)), Grayscale(num_output_channels=1), ToTensor()] |
|
) |
|
|
|
def process_gs_image(image): |
|
""" |
|
Function to process the grayscale image. |
|
""" |
|
|
|
original_size = image.size |
|
|
|
|
|
image = transform_gs(image) |
|
|
|
|
|
image = image.unsqueeze(0) |
|
|
|
|
|
return image, original_size |
|
|
|
def inverse_transform_cs(tensor, original_size): |
|
""" |
|
Function to convert the tensor back to the color image and resize it to its original size. |
|
""" |
|
|
|
to_pil = ToPILImage() |
|
pil_image = to_pil(tensor.squeeze(0)) |
|
|
|
|
|
pil_image = pil_image.resize(original_size) |
|
|
|
return pil_image |