Created
October 20, 2018 12:03
-
-
Save coquer/cfa75fdc725e46ce2756dc382107d851 to your computer and use it in GitHub Desktop.
LeoScott1203 jpgTOtxt - https://www.reddit.com/r/Python/comments/9pmw4k/the_code_for_the_image_to_text_program/
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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