-
Notifications
You must be signed in to change notification settings - Fork 52
/
image_commons.py
50 lines (42 loc) · 1.67 KB
/
image_commons.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
"""
This module contains functions used to manipulate images in OpenCV and PIL's Image.
"""
import cv2
import numpy as np
from PIL import Image
def image_as_nparray(image):
"""
Converts PIL's Image to numpy's array.
:param image: PIL's Image object.
:return: Numpy's array of the image.
"""
return np.asarray(image)
def nparray_as_image(nparray, mode='RGB'):
"""
Converts numpy's array of image to PIL's Image.
:param nparray: Numpy's array of image.
:param mode: Mode of the conversion. Defaults to 'RGB'.
:return: PIL's Image containing the image.
"""
return Image.fromarray(np.asarray(np.clip(nparray, 0, 255), dtype='uint8'), mode)
def load_image(source_path):
"""
Loads RGB image and converts it to grayscale.
:param source_path: Image's source path.
:return: Image loaded from the path and converted to grayscale.
"""
source_image = cv2.imread(source_path)
return cv2.cvtColor(source_image, cv2.COLOR_BGR2GRAY)
def draw_with_alpha(source_image, image_to_draw, coordinates):
"""
Draws a partially transparent image over another image.
:param source_image: Image to draw over.
:param image_to_draw: Image to draw.
:param coordinates: Coordinates to draw an image at. Tuple of x, y, width and height.
"""
x, y, w, h = coordinates
image_to_draw = image_to_draw.resize((h, w), Image.ANTIALIAS)
image_array = image_as_nparray(image_to_draw)
for c in range(0, 3):
source_image[y:y + h, x:x + w, c] = image_array[:, :, c] * (image_array[:, :, 3] / 255.0) \
+ source_image[y:y + h, x:x + w, c] * (1.0 - image_array[:, :, 3] / 255.0)