uccjshrimpton

Functioning Bitmap to ASCII Art Converter WIP

May 9th, 2016
120
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 2.15 KB | None | 0 0
  1. file = open(input("Please enter the file location of a 24 bit colour depth bitmap image ")+".bmp","rb").read()
  2.  
  3. allbytes = []
  4. filecontent = []
  5.  
  6. for line in file:
  7.     allbytes.append(str(hex(line)[2:]))
  8.  
  9. #Metadata converted to denary and stored as variables
  10. offset = int((allbytes[13]+allbytes[12]+allbytes[11]+allbytes[10]),16)
  11. width = int((allbytes[21]+allbytes[20]+allbytes[19]+allbytes[18]),16)
  12. height = int((allbytes[25]+allbytes[24]+allbytes[23]+allbytes[22]),16)
  13. depth = int((allbytes[29]+allbytes[28]),16)        
  14.  
  15. print("Offset from 0 to image pixel data:",offset)
  16. print("Width:",width)
  17. print("Height:",height)
  18. print("Bit depth:",depth)
  19.  
  20. #Puts the pixel data in reverse order to compensate for the fact that bitmaps are printed from the bottom right rather than top left.
  21. allbytes.reverse()
  22.  
  23. #Pixel data isolated, stored as list
  24. for index in range(len(allbytes)):
  25.     filecontent.append(allbytes[index])
  26.  
  27. #Clears variable allbytes to save memory
  28. del allbytes
  29.  
  30. #Breaks up the pixel data into an array, list of RGB values within a list of pixels
  31. pixelmap = []
  32. currpixel = []
  33. for index, entry in enumerate(filecontent):
  34.     currpixel.append(entry)
  35.     if index % 3 == 0:
  36.         pixelmap.append(currpixel)
  37.         currpixel = []
  38.  
  39. #Assessing the brightness/darkness of each pixel
  40.        
  41. #black (0) = #
  42. #dark (63) = £
  43. #medium (126) = *
  44. #light (189) = ~
  45. #white (255) = " "
  46.        
  47. asciiart = []
  48.  
  49. for pixel in pixelmap:
  50.    
  51.     total = 0
  52.    
  53.     for rgb in pixel:  
  54.         total += int(rgb,16)
  55.        
  56.     if (total / 3) >= 254:
  57.         char = " "
  58.     elif (total / 3) >= 189:
  59.         char = "~"
  60.     elif (total / 3) >= 126:
  61.         char = "*"
  62.     elif (total / 3) >= 63:
  63.         char = "£"
  64.     elif (total / 3) >= 0:
  65.         char = "#"
  66.  
  67.     asciiart.append(char)
  68.  
  69. #Clears variable pixelmap to save memory
  70. del pixelmap
  71.  
  72. textfile = open(input("Enter a file name for your converted ASCII art image ")+".txt","w")
  73.  
  74. #Printing the converted image
  75. line = ""
  76. for index, pixel in enumerate(asciiart):
  77.     line = pixel+line
  78.     if index % width == 0:
  79.         textfile.write(line+"\n")
  80.         line = ""
  81. textfile.close()
Advertisement
Add Comment
Please, Sign In to add comment