uccjshrimpton

BMP to ASCII Art Converter 2.0

Jul 8th, 2016
221
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 1.93 KB | None | 0 0
  1. def get_file():
  2.     image_file = open(input("Please enter the file location of a 24 bit colour depth bitmap image ")+".bmp","rb")
  3.     all_bytes = list(image_file.read())
  4.  
  5.     for index, byte in enumerate(all_bytes):
  6.         all_bytes[index] = str(hex(byte)[2:])
  7.  
  8.     #[x:x:-1] = start, end, steps
  9.     offset = int(str("".join(all_bytes[13:9:-1])),16)
  10.     width = int(str("".join(all_bytes[21:17:-1])),16)
  11.     height = int(str("".join(all_bytes[25:21:-1])),16)
  12.     depth = int(str("".join(all_bytes[29:27:-1])),16)
  13.  
  14.     print("Offset:",offset)
  15.     print("Width:",width)
  16.     print("Height:",height)
  17.     print("Depth:",depth)
  18.  
  19.     #pixel data stored from bottom right to top left
  20.     all_pixel_data = all_bytes[offset:]
  21.     map_bits(all_pixel_data,offset,width,height,depth)
  22.  
  23. def get_symbol(RGB):
  24.  
  25.     total = 0
  26.     for colour in RGB:  
  27.         total += int(colour,16)
  28.        
  29.     if (total / 3) >= 254:
  30.         char = " "
  31.     elif (total / 3) >= 189:
  32.         char = "~"
  33.     elif (total / 3) >= 126:
  34.         char = "`"
  35.     elif (total / 3) >= 63:
  36.         char = "£"
  37.     elif (total / 3) >= 0:
  38.         char = "#"
  39.  
  40.     return char
  41.  
  42. def map_bits(all_pixel_data,offset,width,height,depth):
  43.     count = 0
  44.     pixel_data = []
  45.     horizontal_line = []
  46.     ascii_image = []
  47.  
  48.     for byte in all_pixel_data:
  49.         pixel_data.append(byte)
  50.         count += 1
  51.        
  52.         if count == 3:
  53.             count = 0
  54.            
  55.             horizontal_line.append(get_symbol(pixel_data))
  56.             pixel_data = []
  57.            
  58.             if len(horizontal_line) == width:
  59.                 horizontal_line = horizontal_line[::-1]
  60.                 ascii_image.insert(0,horizontal_line)
  61.                 horizontal_line = []
  62.  
  63. def create_file(ascii_image):
  64.     text_file = open("pixel_data.txt","w")
  65.  
  66.     for line in ascii_image:
  67.         count += 1
  68.         text_file.write("".join(line)+"\n")
  69.     text_file.close()
  70.  
  71. get_file()
Advertisement
Add Comment
Please, Sign In to add comment