File size: 1,024 Bytes
67f3560
 
c08bf6c
67f3560
 
 
 
 
 
33581d9
5ccc3f6
 
 
 
33581d9
5ccc3f6
 
33581d9
5ccc3f6
67f3560
 
 
 
 
c08bf6c
 
 
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
import numpy as np
import io
from PIL import Image, ImageFilter, ImageChops
from torchvision import transforms

def softmax(vector):
    e = np.exp(vector - np.max(vector))  # for numerical stability
    return e / e.sum()

def augment_image(img_pil, methods, rotate_degrees=0, noise_level=0, sharpen_strength=1):
    for method in methods:
        if method == "rotate":
            img_pil = img_pil.rotate(rotate_degrees)
        elif method == "add_noise":
            noise = np.random.normal(0, noise_level, img_pil.size[::-1] + (3,)).astype(np.uint8)
            img_pil = Image.fromarray(np.clip(np.array(img_pil) + noise, 0, 255).astype(np.uint8))
        elif method == "sharpen":
            img_pil = img_pil.filter(ImageFilter.UnsharpMask(radius=2, percent=sharpen_strength, threshold=3))
    return img_pil, img_pil

def convert_pil_to_bytes(image, format='JPEG'):
    img_byte_arr = io.BytesIO()
    image.save(img_byte_arr, format=format)
    img_byte_arr = img_byte_arr.getvalue()
    return img_byte_arr