Advertisement
Guest User

Untitled

a guest
Apr 1st, 2015
226
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.85 KB | None | 0 0
  1. #!/usr/bin/env python3
  2.  
  3. import random
  4. import time
  5. import hashlib
  6. import re
  7.  
  8. random.seed()
  9.  
  10. def getRandomColor():
  11. """Generates a random color."""
  12. r = str(random.randrange(0, 255))
  13. b = str(random.randrange(0, 255))
  14. g = str(random.randrange(0, 255))
  15. a = str(random.randrange(0, 255))
  16.  
  17. color = r + "," + b + "," + g + "," + a
  18. return color
  19.  
  20. def getRandomImage(size):
  21. """Generates a random image."""
  22. image = ""
  23. y = 1
  24. for i in range(0, size):
  25. color = getRandomColor()
  26. location = str(i) + str(y)
  27.  
  28. pixel = "{" + location + ":" + color + "}"
  29. image = image + pixel
  30.  
  31. if y == 1920:
  32. image = image + "{\\n}"
  33. i = 0
  34. y = y + 1
  35.  
  36. i = i + 1
  37. return image
  38.  
  39. def writeImage(image, path, suffix=""):
  40. """Writes the image to the given path."""
  41. #print(image)
  42. hash = hashlib.sha224(image.encode("utf-8")).hexdigest()
  43. name = "2015-02-03_" + hash + suffix
  44. path = path + name + ".txt"
  45.  
  46. f = open(path, "w")
  47. f.write(image)
  48. f.close()
  49.  
  50. def compress(image):
  51. """Compresses the given image"""
  52. compressed = ""
  53. #1,1:255,255,255,255
  54. for r in range(0, 255):
  55. r = r + 1
  56. for b in range(0, 255):
  57. b = b + 1
  58. for g in range(0, 255):
  59. g = g + 1
  60. for a in range(0, 255):
  61. color = (str(r) + "," + str(b) +
  62. "," + str(g) + "," + str(a))
  63. print(color)
  64.  
  65. pattern = re.compile("{\w*,\w*:" + color + "}")
  66. pixels = re.findall(pattern, image)
  67.  
  68. compressed = compressed + "{"
  69.  
  70. for pixel in pixels:
  71. coordinates = re.findall("\w*,\w*:")
  72. coordinate = coordinates[0]
  73. compressed = compressed + color + ":" + coordinate
  74.  
  75. compressed = compressed + "}"
  76.  
  77. a = a + 1
  78. return compressed
  79.  
  80. def main():
  81. """Runs the program."""
  82. path = ""
  83. size = 2073600 #1080p file.
  84.  
  85. image = getRandomImage(size)
  86. writeImage(image, path)
  87.  
  88. compressed = compress(image)
  89. writeImage(compressed, path, "_COMPRESSED")
  90.  
  91. main()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement