Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #!C:\Users\MeshCollider\AppData\Local\Programs\Python\Python35-32\python.exe -u
- from PIL import Image, ImageDraw, ImageFont
- import re, cgi
- from urllib.request import urlopen
- import io
- def doStuff():
- print('Content-type: text/html\n')
- print('<title>Here is your code!</title>')
- print("<script src=\"script.js\"></script>")
- form = cgi.FieldStorage()
- if "image" not in form or "repeatText" not in form:
- print("<H1>Error</H1>")
- print("Please fill in the correct information.")
- return
- p = re.compile('[\w]+.(png|jpg|jpeg)')
- imageLoc = p.match(form["image"].value)
- image_file_path = "http://i.imgur.com/" + imageLoc.group()
- repeat_text = form["repeatText"].value
- alpha = 0
- if "transparent" in form:
- alpha = 1
- scale = 1
- if "scale" in form:
- try:
- scale = float(form["scale"].value)
- except Exception:
- print("fail")
- print("<p>image:", image_file_path)
- print("<p>repeatText:", repeat_text)
- print("<p>scale:", scale)
- print("<p>alpha:", alpha)
- repeat_text_array = list(repeat_text)
- def scale_image(image, new_width=100, new_height=0):
- (original_width, original_height) = image.size
- if new_height == 0:
- aspect_ratio = original_height/float(original_width)
- new_height = int(aspect_ratio * new_width)
- new_image = image.resize((new_width, new_height))
- return new_image
- im = None
- try:
- fd = urlopen(image_file_path)
- image_file = io.BytesIO(fd.read())
- im = Image.open(image_file).convert('RGBA')
- #im = Image.open(file).convert('RGBA')
- except Exception as e:
- #print("Unable to open image file {image_filepath}.".format(image_filepath=image_file_path))
- #print(e)
- print ("An error occured when opening the image. Please ensure you entered the correct image URL and try again")
- return
- drawer = ImageDraw.Draw(im)
- font = ImageFont.truetype('Verdana.ttf', 14)
- fontSize = drawer.textsize(repeat_text, font=font)
- fontOffset = font.getoffset(repeat_text)
- charsAcross = int((im.size[0]/(fontSize[0]+fontOffset[0]))*len(repeat_text)*scale)
- charsHigh = int((im.size[1]/(fontSize[1]+fontOffset[1]))*scale)
- image = scale_image(im, charsAcross, charsHigh)
- pixels_in_image = list(image.getdata())
- count = 0
- output = ""
- image_ascii = []
- previous = (0, 0, 0, 1)
- color = "#000000"
- transparent = False
- for pixel_value in pixels_in_image:
- r,g,b,a = pixel_value
- bbcode = ""
- if count % charsAcross == 0: # start of the line
- previous = ((r * a), (g * a), (b * a), a)
- begin = True
- end = False
- 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
- end = False # no difference so don't end or start again
- begin = False
- else: # too large difference, we end and begin a new one
- previous = ((r * a), (g * a), (b * a), a)
- end = True
- begin = True
- a = a/255
- r = hex(int(r * a)).split('x')[1].zfill(2)
- g = hex(int(g * a)).split('x')[1].zfill(2)
- b = hex(int(b * a)).split('x')[1].zfill(2)
- color = "#" + str(r) + str(g) + str(b)
- if a < 0.1 and alpha == 1:
- color = "transparent"
- character = repeat_text_array[count % len(repeat_text)]
- if end:
- bbcode += "[/color]"
- if begin:
- bbcode += "[color=" + color + "]"
- bbcode += character
- output += bbcode
- if count % charsAcross == (charsAcross-1):
- output += "[/color]"
- image_ascii.append(output)
- output = ""
- count += 1
- print("<br /><br />Here is your code: ")
- print("<input type=\"button\" onclick=\"selectText('selectable')\" value=\"Select All\" /><br />")
- print("<div id=\"selectable\" style=\"border:1px solid #ccc;font:16px/26px Georgia, Garamond, Serif;overflow:auto;\">")
- print("<br />".join(image_ascii))
- print("</div>")
- try:
- doStuff()
- except Exception:
- print("somthing went wrong")
Advertisement
Add Comment
Please, Sign In to add comment