Skip to content

Instantly share code, notes, and snippets.

@coquer
Created October 20, 2018 12:03
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save coquer/cfa75fdc725e46ce2756dc382107d851 to your computer and use it in GitHub Desktop.
Save coquer/cfa75fdc725e46ce2756dc382107d851 to your computer and use it in GitHub Desktop.
from PIL import Image
from bisect import bisect
def main():
gscale = {8:'@', 7:'#', 6:'£', 5:'=', 4: '+', 3: '|', 2: ':', 1: '.', 0: ' '}
bounds = [32,64,96,128,160,192,224]
txt = ""
jpegFilename = input('enter jpg file path: ')
txtFilename = input('enter path for new txt file: ')
pixelToCharacterScale = float(input('enter pixel to character scale: '))
jpeg = Image.open(jpegFilename)
jpeg.convert(mode = 'L')
loadedJpeg = jpeg.load()
w = jpeg.size[0]
h = jpeg.size[1]
with open(txtFilename, 'w') as newTxtFile:
for i in range(0,h):
if i % pixelToCharacterScale == 0:
for j in range (0,w):
if j % pixelToCharacterScale == 0:
brightness = loadedJpeg[j,i]
brightness = 250 - brightness[0]
index = bisect(bounds,brightness)
chosenChar = gscale[index]
txt = txt + chosenChar
txt = txt + "\n"
newTxtFile.write(txt)
if __name__ == '__main__':
main()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment