Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- file = open(input("Please enter the file location of a 24 bit colour depth bitmap image ")+".bmp","rb").read()
- allbytes = []
- filecontent = []
- for line in file:
- allbytes.append(str(hex(line)[2:]))
- #Metadata converted to denary and stored as variables
- offset = int((allbytes[13]+allbytes[12]+allbytes[11]+allbytes[10]),16)
- width = int((allbytes[21]+allbytes[20]+allbytes[19]+allbytes[18]),16)
- height = int((allbytes[25]+allbytes[24]+allbytes[23]+allbytes[22]),16)
- depth = int((allbytes[29]+allbytes[28]),16)
- print("Offset from 0 to image pixel data:",offset)
- print("Width:",width)
- print("Height:",height)
- print("Bit depth:",depth)
- #Puts the pixel data in reverse order to compensate for the fact that bitmaps are printed from the bottom right rather than top left.
- allbytes.reverse()
- #Pixel data isolated, stored as list
- for index in range(len(allbytes)):
- filecontent.append(allbytes[index])
- #Clears variable allbytes to save memory
- del allbytes
- #Breaks up the pixel data into an array, list of RGB values within a list of pixels
- pixelmap = []
- currpixel = []
- for index, entry in enumerate(filecontent):
- currpixel.append(entry)
- if index % 3 == 0:
- pixelmap.append(currpixel)
- currpixel = []
- #Assessing the brightness/darkness of each pixel
- #black (0) = #
- #dark (63) = £
- #medium (126) = *
- #light (189) = ~
- #white (255) = " "
- asciiart = []
- for pixel in pixelmap:
- total = 0
- for rgb in pixel:
- total += int(rgb,16)
- if (total / 3) >= 254:
- char = " "
- elif (total / 3) >= 189:
- char = "~"
- elif (total / 3) >= 126:
- char = "*"
- elif (total / 3) >= 63:
- char = "£"
- elif (total / 3) >= 0:
- char = "#"
- asciiart.append(char)
- #Clears variable pixelmap to save memory
- del pixelmap
- textfile = open(input("Enter a file name for your converted ASCII art image ")+".txt","w")
- #Printing the converted image
- line = ""
- for index, pixel in enumerate(asciiart):
- line = pixel+line
- if index % width == 0:
- textfile.write(line+"\n")
- line = ""
- textfile.close()
Advertisement
Add Comment
Please, Sign In to add comment