File size: 694 Bytes
846d481
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
import torch
import img2gray

from PIL import Image
import numpy as np

print(dir(img2gray))

img = Image.open("/home/ubuntu/Projects/img2gray/kernel-builder-logo-color.png").convert("RGB")
img = np.array(img)
img_tensor = torch.from_numpy(img)
print(img_tensor.shape)  # HWC
img_tensor = img_tensor.permute(2, 0, 1).unsqueeze(0).contiguous().cuda()  # BCHW
print(img_tensor.shape)  # BCHW

gray_tensor = img2gray.img2gray(img_tensor).squeeze()
print(gray_tensor.shape)  # B1HW

# save the output image
gray_img = gray_tensor.cpu().numpy()  # 1HW -> HW
gray_img = Image.fromarray(gray_img.astype(np.uint8), mode="L")

gray_img.save("/home/ubuntu/Projects/img2gray/kernel-builder-logo-gray.png")