Created
February 4, 2015 16:40
-
-
Save JanneSalokoski/5e45f586cff3ed90fadd to your computer and use it in GitHub Desktop.
A thing that generates an random "image" in a very inefficient way, and "compresses" the inefficient image in a very basic way. Is very slow.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/bin/env python3 | |
import random | |
import time | |
import hashlib | |
import re | |
random.seed() | |
def getRandomColor(): | |
"""Generates a random color.""" | |
r = str(random.randrange(0, 255)) | |
b = str(random.randrange(0, 255)) | |
g = str(random.randrange(0, 255)) | |
a = str(random.randrange(0, 255)) | |
color = r + "," + b + "," + g + "," + a | |
return color | |
def getRandomImage(size): | |
"""Generates a random image.""" | |
image = "" | |
y = 1 | |
for i in range(0, size): | |
color = getRandomColor() | |
location = str(i) + str(y) | |
pixel = "{" + location + ":" + color + "}" | |
image = image + pixel | |
if y == 1920: | |
image = image + "{\\n}" | |
i = 0 | |
y = y + 1 | |
i = i + 1 | |
return image | |
def writeImage(image, path, suffix=""): | |
"""Writes the image to the given path.""" | |
#print(image) | |
hash = hashlib.sha224(image.encode("utf-8")).hexdigest() | |
name = "2015-02-03_" + hash + suffix | |
path = path + name + ".txt" | |
f = open(path, "w") | |
f.write(image) | |
f.close() | |
def compress(image): | |
"""Compresses the given image""" | |
compressed = "" | |
#1,1:255,255,255,255 | |
for r in range(0, 255): | |
r = r + 1 | |
for b in range(0, 255): | |
b = b + 1 | |
for g in range(0, 255): | |
g = g + 1 | |
for a in range(0, 255): | |
color = (str(r) + "," + str(b) + | |
"," + str(g) + "," + str(a)) | |
print(color) | |
pattern = re.compile("{\w*,\w*:" + color + "}") | |
pixels = re.findall(pattern, image) | |
compressed = compressed + "{" | |
for pixel in pixels: | |
coordinates = re.findall("\w*,\w*:") | |
coordinate = coordinates[0] | |
compressed = compressed + color + ":" + coordinate | |
compressed = compressed + "}" | |
a = a + 1 | |
return compressed | |
def main(): | |
"""Runs the program.""" | |
path = "" | |
size = 2073600 #1080p file. | |
image = getRandomImage(size) | |
writeImage(image, path) | |
compressed = compress(image) | |
writeImage(compressed, path, "_COMPRESSED") | |
main() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment