forked from Adityaranjanpatra/Btecky2
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Image-upscale.py
33 lines (26 loc) · 891 Bytes
/
Image-upscale.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
import torch
from PIL import Image
from torchvision import transforms
from ESRGAN import ESRGAN
# Load the ESRGAN model
model = ESRGAN()
model.load_state_dict(torch.load("path/to/ESRGAN_model.pth"))
model.eval()
# Load the image to be upscaled
img = Image.open("path/to/image.jpg")
# Define the transformation to be applied to the image
transform = transforms.Compose(
[
transforms.Resize((224, 224)),
transforms.ToTensor(),
transforms.Normalize((0.5, 0.5, 0.5), (0.5, 0.5, 0.5)),
]
)
# Apply the transformation to the image
img_tensor = transform(img).unsqueeze(0)
# Upscale the image using the ESRGAN model
upscaled_img_tensor = model(img_tensor)
# Convert the upscaled image back to a PIL image
upscaled_img = transforms.ToPILImage()(upscaled_img_tensor[0].data.clamp(0, 1))
# Save the upscaled image
upscaled_img.save("path/to/upscaled_image.jpg")