-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathglitch.py
123 lines (106 loc) · 3.11 KB
/
glitch.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
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
# Aradiabot image glitching functions.
# Transcribed over from my 'fastglitch' repository.
from io import BytesIO, StringIO
import random, sys, PIL.Image, PIL.ImageChops, PIL.ImageDraw, os
chars = 'abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ1234567890'
import asyncio
def genImg(fname):
img = PIL.Image.open(fname)
img = img.convert('RGBA')
proto1 = RandomByteAddition(img, random.randint(1,16))
proto2 = RGBOffset(proto1, random.randint(1,64))
proto3 = PixelOffset(proto2, random.randint(1,512))
proto4 = Artifact(proto3, random.randint(1,64))
proto5 = RowSlice(proto4, random.randint(1,32))
proto6 = Noise(proto5, random.randint(25000,50000))
p = proto6.convert('RGB')
p.save('new' + fname + '.png')
proto1.close()
proto2.close()
proto3.close()
proto4.close()
proto5.close()
proto6.close()
def RandomByteAddition(image, seed):
bytesBroken = False
bytesobj = BytesIO()
image.save(bytesobj, 'jpeg')
iter = seed
bytesobj.seek(1024)
if seed > 0:
for x in range(0, iter):
bytes2 = bytesobj
bytesobj.seek(random.randint(0, 32), 1)
byte = random.choice(chars)
bytesobj.write(bytes(byte, 'utf-8'))
try:
PIL.Image.open(bytesobj)
except:
bytesBroken = True
break
if bytesBroken == True:
bytes2.seek(0)
new_img = PIL.Image.open(bytes2)
else:
bytesobj.seek(0)
new_img = PIL.Image.open(bytesobj)
return new_img
def RGBOffset(image, distance):
distance = distance * 30
r, g, b = image.split()
r = PIL.ImageChops.offset(r, distance * -1, 0)
b = PIL.ImageChops.offset(b, distance, 0)
new_img = PIL.Image.merge('RGB', (r, g, b))
return new_img
def PixelOffset(image, distance):
new_img = PIL.ImageChops.offset(image, distance)
return new_img
def RowSlice(image, sliceamount):
cps = 0
new_img = image
for x in range(sliceamount):
upbound = cps
downbound = upbound + random.randint(16, 128)
if downbound > image.height:
break
box = (0,
upbound,
new_img.width,
downbound)
reigon = new_img.crop(box)
distance = random.randint(-128, 128)
reigon = PIL.ImageChops.offset(reigon, distance, 0)
new_img.paste(reigon, box)
reigon.close()
cps = downbound
return new_img
def Artifact(image, screwamount):
tnspimg = image.convert('RGBA')
base = PIL.Image.new('RGBA', tnspimg.size, (255, 255, 255, 0))
rows = PIL.ImageDraw.Draw(base)
cps = 0
for x in range(screwamount):
leftbound = cps
rightbound = leftbound + random.randint(32, 128)
if rightbound > image.width:
break
y1 = random.randint(0, image.height - int(round(image.height / 2.0, 0)))
x1 = random.randint(leftbound, rightbound - 1)
y2 = random.randint(y1, image.height)
x2 = rightbound
color = (random.randint(0, 255),
random.randint(0, 255),
random.randint(0, 255),
random.randint(64, 200))
rows.rectangle((x1,
y1,
x2,
y2), fill=color)
cps = rightbound
new_img = PIL.Image.alpha_composite(tnspimg, base)
return new_img
def Noise(image, pixels):
for x in range(1, pixels):
image.putpixel((random.randint(1, image.width - 1), random.randint(1, image.height - 1)), (random.randint(1, 255), random.randint(1, 255), random.randint(1, 255)))
new_img = image
return new_img