33 lines
777 B
Python
33 lines
777 B
Python
import numpy as np
|
|
from PIL import Image, ImageDraw
|
|
import torch
|
|
|
|
|
|
def tensor2pil(image):
|
|
return Image.fromarray(np.clip(255. * image.cpu().numpy().squeeze(), 0, 255).astype(np.uint8))
|
|
|
|
def pil2tensor(image):
|
|
return torch.from_numpy(np.array(image).astype(np.float32) / 255.0).unsqueeze(0)
|
|
|
|
|
|
def crop_ndarray4(npimg, crop_region):
|
|
x1 = crop_region[0]
|
|
y1 = crop_region[1]
|
|
x2 = crop_region[2]
|
|
y2 = crop_region[3]
|
|
|
|
cropped = npimg[:, y1:y2, x1:x2, :]
|
|
|
|
return cropped
|
|
|
|
|
|
def crop_image(image, crop_region):
|
|
return crop_ndarray4(np.array(image), crop_region)
|
|
|
|
|
|
def empty_pil_tensor(w=64, h=64):
|
|
image = Image.new("RGB", (w, h))
|
|
draw = ImageDraw.Draw(image)
|
|
draw.rectangle((0, 0, w-1, h-1), fill=(0, 0, 0))
|
|
return pil2tensor(image)
|