uccjshrimpton

Python Turtle Image Plotter

Mar 8th, 2017
133
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 2.65 KB | None | 0 0
  1. from PIL import Image
  2. import turtle
  3.  
  4. def get_image_data(image_filepath):
  5.    
  6.     image = Image.open(image_filepath)
  7.     pixels = image.load()
  8.     width, height = image.size
  9.     return width, height, pixels
  10.    
  11. def pixel_tuple_to_hex(pixel_tuple):
  12.    
  13.     hex_code = "#"
  14.     for rgb in pixel_tuple:
  15.         if len(str(hex(rgb))) < 4:
  16.             hex_code += "0" + str(hex(rgb))[-1:]
  17.         else:
  18.             hex_code += str(hex(rgb))[-2:]
  19.     return hex_code
  20.  
  21. def pixel_tuple_to_ascii(pixel_tuple):
  22.  
  23.     total = sum(pixel_tuple)
  24.     if total > 612:
  25.         return " "
  26.     if total > 459:
  27.         return "~"
  28.     if total > 306:
  29.         return "+"
  30.     if total > 153:
  31.         return "@"
  32.     else:
  33.         return "#"
  34.  
  35. def turtle_plot_pixel(turtle,size):
  36.    
  37.     turtle.begin_fill()
  38.     for i in range(4):
  39.         turtle.forward(size)
  40.         turtle.right(90)
  41.     turtle.forward(size)
  42.     turtle.end_fill()
  43.  
  44. def turtle_plot_image(image_width, image_height, image_pixels, pixel_size):
  45.  
  46.     turtlex = 0-(image_width*(pixel_size/2))
  47.     turtley = image_height*(pixel_size/2)
  48.  
  49.     canvas = turtle.Screen()
  50.     canvas.setup(width = image_width*pixel_size, height = image_height*pixel_size, startx = turtlex, starty = turtley)
  51.  
  52.     t = turtle.Turtle()
  53.     t.speed(999)
  54.     t.penup()
  55.     t.setx(turtlex)
  56.     t.sety(turtley)
  57.     t.pendown()
  58.  
  59.     for y in range(image_height):
  60.         for x in range(image_width):
  61.             t.color(pixel_tuple_to_hex(image_pixels[x,y]))
  62.             turtle_plot_pixel(t,pixel_size)
  63.         t.penup()
  64.         turtley -= pixel_size
  65.         t.setx(turtlex)
  66.         t.sety(turtley)
  67.         t.pendown()
  68.  
  69.     canvas.mainloop()
  70.  
  71. def ascii_plot_image(image_width, image_height, image_pixels, ascii_filename):
  72.     text_file = open(ascii_filename,"w")
  73.     ascii_conversion = []
  74.    
  75.     for y in range(image_height):
  76.         current_line = []
  77.         for x in range(image_width):
  78.             current_line.append(pixel_tuple_to_ascii(image_pixels[x,y]))
  79.         ascii_conversion.append(current_line)
  80.    
  81.     for line in ascii_conversion:
  82.         text_file.writelines("".join(line)+"\n")
  83.  
  84.     text_file.close()
  85.  
  86. def image_to_turtle(image_filepath,pixel_size):    
  87.     image_width, image_height, image_pixels = get_image_data(image_filepath)
  88.     turtle_plot_image(image_width, image_height, image_pixels, pixel_size)
  89.  
  90. def image_to_ascii(image_filepath,ascii_filename):
  91.     image_width, image_height, image_pixels = get_image_data(image_filepath)
  92.     ascii_plot_image(image_width, image_height, image_pixels, ascii_filename)
  93.  
  94. image_to_ascii("pika.jpg","pika_ascii.txt")
  95. image_to_turtle("pika.jpg",25)
Advertisement
Add Comment
Please, Sign In to add comment