-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathimage2hate.py
More file actions
63 lines (48 loc) · 1.75 KB
/
Copy pathimage2hate.py
File metadata and controls
63 lines (48 loc) · 1.75 KB
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
"""Sticker hate.
Usage: image2hate.py [--front=EMOJI] [--back=EMOJI] [--width=WIDTH] <impath>
Arguments:
<impath> Path to hated image
Options:
-h --help Show this screen.
--version Show version.
--front=EMOJI Set front emoji [default: 😡]
--back=EMOJI Set background emoji [default: 🖤]
--width=WIDTH Set with of resulting "image" [default: 20]
"""
from io import BytesIO
from docopt import docopt
from PIL import Image
import numpy as np
import requests
from scipy.ndimage.filters import median_filter
def fit_image(img: np.ndarray, size: int) -> np.ndarray:
small_im = img
filter_sz: int = 2
while small_im.shape[1] > size:
small_im = median_filter(img, size = (filter_sz, filter_sz)
)[::filter_sz, ::filter_sz]
filter_sz += 1
return small_im
def load_image(path: str) -> np.ndarray:
im_str = path
if 'http://' in path or 'https://' in path:
im_str = BytesIO(requests.get(path).content)
img = Image.open(im_str)
gray_img = np.mean(img, axis=2)
return gray_img
def array2text(array: np.ndarray, front_fill: str, back_fill: str) -> str:
emoim = ''
for row in array > np.mean(array):
line = ''.join([front_fill if val else back_fill for val in row])
emoim += line + '\n'
return emoim
if __name__ == '__main__':
arguments = docopt(__doc__, version='Polina 😡')
im_path = arguments['<impath>']
front_emoji = arguments['--front']
back_emoji = arguments['--back']
size_shrink = int(arguments['--width'])
img: np.ndarray = load_image(im_path)
gray_img = fit_image(img, size_shrink)
hate = array2text(gray_img, front_emoji, back_emoji)
print(hate)