forked from chiuhans111/Glitch-Image-Generator
-
Notifications
You must be signed in to change notification settings - Fork 0
/
GlitchImageGenerator.py
64 lines (46 loc) · 1.56 KB
/
GlitchImageGenerator.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
# Glitch Image Generator
# Author: Hans chiu (twitter: @chiu_hans)
from PIL import Image, ImageFile
import numpy as np
import io
ImageFile.LOAD_TRUNCATED_IMAGES = True
def img2data(img, quality=10):
"""encode the image"""
file = io.BytesIO()
file.name = 'file.jpg'
img.save(file, quality=quality)
return list(file.getvalue())
def data2img(data):
"""decode the image"""
file = io.BytesIO()
file.write(bytearray(data))
return Image.open(file)
def glitch(img, amount=10, quality=10):
"""glitch the image"""
img = img.convert('RGB')
while True:
# only break the loop when image is successfully glitched
try:
data = img2data(img, quality=quality)
for t in range(amount):
# glitch the data
i = np.random.randint(0, len(data))
data[i] ^= 1 << np.random.randint(8)
data = np.array(data).astype('uint8')
result = data2img(data)
if np.sum(np.abs(np.array(result) - np.array(img))) == 0:
raise Exception('same')
if np.sum(np.abs(np.array(result))) == 0:
raise Exception('empty')
except Exception as e:
# glitch again, this one doesn't work
continue
break
return result
# The following code is just a example:
img_array = np.random.rand(64, 64, 3)*255
img = Image.fromarray(img_array.astype('uint8'))
img = glitch(img, 20, 80)
img = img.resize([512, 512], Image.NEAREST)
img = glitch(img, 50, 80)
img.save(f'./glitched_img.jpg')