forked from gooddeLink/Flask
-
Notifications
You must be signed in to change notification settings - Fork 0
/
mosaic.txt
38 lines (25 loc) · 1.42 KB
/
mosaic.txt
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
# 받은 url을 image 형태로 변환 ------------------------------------------------------------
def url_to_image(url):
resp = urllib.request.urlopen(url)
image = np.asarray(bytearray(resp.read()), dtype='uint8')
image = cv2.imdecode(image, cv2.IMREAD_COLOR)
return image
img = url_to_image('https://cdn.mkhealth.co.kr/news/photo/202011/51210_51487_4422.jpg')
cv2.imshow(img)
# image를 모자이크 처리 --------------------------------------------------------------------------------
face_cascade = cv2.CascadeClassifier('haarcascade_frontalface_default.xml')
src = img
src_gray = cv2.cvtColor(src, cv2.COLOR_BGR2GRAY)
faces = face_cascade.detectMultiScale(src_gray, 1.3, 5)
ratio = 0.1
for x, y, w, h in faces:
small = cv2.resize(src[y: y + h, x: x + w], None, fx=ratio, fy=ratio, interpolation=cv2.INTER_NEAREST)
src[y: y + h, x: x + w] = cv2.resize(small, (w, h), interpolation=cv2.INTER_NEAREST)
cv2.imshow(src)
# array -> image -> url-----------------------------------------------------------------------------------
src = cv2.cvtColor(src, cv2.COLOR_BGR2RGB)
img_file = Image.fromarray(src) # array -> image
buffered = BytesIO()
img_file.save(buffered, format="PNG")
img_str = base64.b64encode(buffered.getvalue()) # image -> url
img_base64 = bytes("data:image/png;base64,", encoding='utf-8') + img_str