-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathimage_utils.py
105 lines (65 loc) · 3.17 KB
/
image_utils.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
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
from PIL import Image, ImageFilter
from io import BytesIO
import numpy as np
# Abrir o tipo Image como byte array
def open_image_as_byte_array(img: Image) -> bytes:
imgByteArr = BytesIO()
img.save(imgByteArr, "PNG")
imgByteArr.seek(0)
imgByteArr = imgByteArr.read()
return imgByteArr
# Abrir imagem pelo endereco dela no sistema, como numpy array
def open_image_as_numpy_array(img_path: str):
original_image = Image.open(img_path)
original_image = np.asarray(original_image)
return original_image
# Ler imagem a partir dos seus bytes
def read_image_from_bytes(image_data) -> Image:
image_bytes = BytesIO(image_data)
return Image.open(image_bytes)
# Adds <blur_radius> pixels to the TOP of the image.
def add_top_pixels(original_img, slab, slab_number: int, blur_radius: int, n_parts: int):
temp = original_img[(slab_number * int(len(original_img)/n_parts) - blur_radius) : (slab_number * int(len(original_img)/n_parts))][0 : len(original_img[0])]
slab = np.append(temp, slab, axis=0)
return slab
# Adds <blur_radius> pixels to the BOTTOM of the image.
def add_bottom_pixels(original_img, slab, slab_number: int, blur_radius: int, n_parts: int):
temp = original_img[((slab_number+1) * int(len(original_img)/n_parts)) : ((slab_number+1) * int(len(original_img)/n_parts)) + blur_radius][0 : len(original_img[0])]
slab = np.append(slab, temp, axis=0)
return slab
# Removes <blur_radius> pixels from the TOP of the image.
def remove_top_pixels(slab, blur_radius: int):
slab = slab[ blur_radius : len(slab)][0 : len(slab[0])]
return slab
# Removes <blur_radius> pixels from the BOTTOM of the image.
def remove_bottom_pixels(slab, blur_radius: int):
slab = slab[ 0 : len(slab) - blur_radius][0 : len(slab[0])]
return slab
def apply_blur(image: Image, blur_radius: float):
return image.filter(ImageFilter.BoxBlur(blur_radius))
# Splits an image into n_parts parts and returns an array
# containing each slab.
def split_img(original_img, n_parts: int, blur_radius: int) -> dict:
slabs = {}
# We need a counter to tell us where each slice is supposed to go when reconstructing the image
counter = 0
for i in range(n_parts):
temp_slab = (original_img[ i * int(len(original_img)/n_parts) : (i+1) * int(len(original_img)/n_parts) ] [0 : len(original_img[0])])
if counter != 0:
temp_slab = add_top_pixels(original_img, temp_slab, counter, blur_radius, n_parts)
if counter != (n_parts-1):
temp_slab = add_bottom_pixels(original_img, temp_slab, counter, blur_radius, n_parts)
slabs[counter] = Image.fromarray(temp_slab)
counter += 1
return slabs
# Merges all N slabs into a single image
def merge_img_slabs(slabs, n_parts: int, blur_radius: int) -> Image:
img = slabs[0]
img = remove_bottom_pixels(np.asarray(img), blur_radius)
for i in range(1, n_parts):
temp_slab = np.asarray(slabs[i])
if i != (n_parts-1):
temp_slab = remove_bottom_pixels(temp_slab, blur_radius)
temp_slab = remove_top_pixels(temp_slab, blur_radius)
img = np.append(img, temp_slab, axis=0)
return Image.fromarray(img)