MeshCollider

Untitled

Dec 10th, 2016
97
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.46 KB | None | 0 0
  1. #!C:\Users\MeshCollider\AppData\Local\Programs\Python\Python35-32\python.exe -u
  2.  
  3. from PIL import Image, ImageDraw, ImageFont
  4. import re, cgi
  5. from urllib.request import urlopen
  6. import io
  7.  
  8. def doStuff():
  9. print('Content-type: text/html\n')
  10. print('<title>Here is your code!</title>')
  11. print("<script src=\"script.js\"></script>")
  12. form = cgi.FieldStorage()
  13. if "image" not in form or "repeatText" not in form:
  14. print("<H1>Error</H1>")
  15. print("Please fill in the correct information.")
  16. return
  17.  
  18. p = re.compile('[\w]+.(png|jpg|jpeg)')
  19. imageLoc = p.match(form["image"].value)
  20.  
  21. image_file_path = "http://i.imgur.com/" + imageLoc.group()
  22. repeat_text = form["repeatText"].value
  23.  
  24. alpha = 0
  25. if "transparent" in form:
  26. alpha = 1
  27.  
  28. scale = 1
  29. if "scale" in form:
  30. try:
  31. scale = float(form["scale"].value)
  32. except Exception:
  33. print("fail")
  34.  
  35.  
  36. print("<p>image:", image_file_path)
  37. print("<p>repeatText:", repeat_text)
  38. print("<p>scale:", scale)
  39. print("<p>alpha:", alpha)
  40.  
  41. repeat_text_array = list(repeat_text)
  42.  
  43. def scale_image(image, new_width=100, new_height=0):
  44. (original_width, original_height) = image.size
  45.  
  46. if new_height == 0:
  47. aspect_ratio = original_height/float(original_width)
  48. new_height = int(aspect_ratio * new_width)
  49.  
  50. new_image = image.resize((new_width, new_height))
  51. return new_image
  52.  
  53. im = None
  54. try:
  55. fd = urlopen(image_file_path)
  56. image_file = io.BytesIO(fd.read())
  57. im = Image.open(image_file).convert('RGBA')
  58. #im = Image.open(file).convert('RGBA')
  59. except Exception as e:
  60. #print("Unable to open image file {image_filepath}.".format(image_filepath=image_file_path))
  61. #print(e)
  62. print ("An error occured when opening the image. Please ensure you entered the correct image URL and try again")
  63. return
  64.  
  65. drawer = ImageDraw.Draw(im)
  66. font = ImageFont.truetype('Verdana.ttf', 14)
  67. fontSize = drawer.textsize(repeat_text, font=font)
  68. fontOffset = font.getoffset(repeat_text)
  69.  
  70. charsAcross = int((im.size[0]/(fontSize[0]+fontOffset[0]))*len(repeat_text)*scale)
  71. charsHigh = int((im.size[1]/(fontSize[1]+fontOffset[1]))*scale)
  72.  
  73. image = scale_image(im, charsAcross, charsHigh)
  74. pixels_in_image = list(image.getdata())
  75. count = 0
  76.  
  77. output = ""
  78. image_ascii = []
  79. previous = (0, 0, 0, 1)
  80. color = "#000000"
  81. transparent = False
  82.  
  83. for pixel_value in pixels_in_image:
  84. r,g,b,a = pixel_value
  85. bbcode = ""
  86.  
  87. if count % charsAcross == 0: # start of the line
  88. previous = ((r * a), (g * a), (b * a), a)
  89. begin = True
  90. end = False
  91. elif (abs(previous[0] - (r * a)) < 5) and (abs(previous[1] - (g * a)) < 5) and (abs(previous[2] - (b * a)) < 5) and (abs(previous[3] - a) < 5): # somewhere in the middle, check difference with previous char
  92. end = False # no difference so don't end or start again
  93. begin = False
  94. else: # too large difference, we end and begin a new one
  95. previous = ((r * a), (g * a), (b * a), a)
  96. end = True
  97. begin = True
  98.  
  99. a = a/255
  100.  
  101. r = hex(int(r * a)).split('x')[1].zfill(2)
  102. g = hex(int(g * a)).split('x')[1].zfill(2)
  103. b = hex(int(b * a)).split('x')[1].zfill(2)
  104.  
  105. color = "#" + str(r) + str(g) + str(b)
  106. if a < 0.1 and alpha == 1:
  107. color = "transparent"
  108. character = repeat_text_array[count % len(repeat_text)]
  109.  
  110. if end:
  111. bbcode += "[/color]"
  112. if begin:
  113. bbcode += "[color=" + color + "]"
  114.  
  115. bbcode += character
  116.  
  117. output += bbcode
  118.  
  119. if count % charsAcross == (charsAcross-1):
  120. output += "[/color]"
  121. image_ascii.append(output)
  122. output = ""
  123.  
  124. count += 1
  125.  
  126. print("<br /><br />Here is your code: ")
  127. print("<input type=\"button\" onclick=\"selectText('selectable')\" value=\"Select All\" /><br />")
  128. print("<div id=\"selectable\" style=\"border:1px solid #ccc;font:16px/26px Georgia, Garamond, Serif;overflow:auto;\">")
  129. print("<br />".join(image_ascii))
  130. print("</div>")
  131.  
  132. try:
  133. doStuff()
  134. except Exception:
  135. print("somthing went wrong")
Advertisement
Add Comment
Please, Sign In to add comment