-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathutils.py
46 lines (30 loc) · 1.17 KB
/
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
import tempfile
import uuid
import cv2
import numpy
from PIL.Image import Image
from PIL import Image as image_main
def generate_uuid() -> str:
return str(uuid.uuid4())
def open_image_pil(image_path: str) -> Image:
return image_main.open(image_path)
def save_image_pil(pil_image: Image, image_path: str) -> str:
pil_image.save(image_path)
return image_path
def save_image_pil_in_temp(pil_image: Image, image_name: str) -> str:
folder_path = str(tempfile.gettempdir())
image_path = folder_path + '/' + image_name + '.' + pil_image.format.lower()
return save_image_pil(pil_image, image_path)
def convert_pil_to_cv(pil_image: Image):
if pil_image.mode != 'RGB':
pil_image = pil_image.convert('RGB')
return cv2.cvtColor(numpy.array(pil_image), cv2.COLOR_RGB2BGR)
def convert_cv_to_pil(cv_image) -> Image:
return image_main.fromarray(cv2.cvtColor(cv_image, cv2.COLOR_BGR2RGB))
def debug_image_cv(cv_image):
cv2.namedWindow('Debug Image', cv2.WINDOW_NORMAL)
cv2.imshow('Debug Image', cv_image)
cv2.waitKey(0)
cv2.destroyAllWindows()
def debug_image_pil(pil_image: Image):
debug_image_cv(convert_pil_to_cv(pil_image))