#!/usr/bin/env python3 from PIL import Image from pprint import pprint import sys, time, os, requests, random, json, argparse parser = argparse.ArgumentParser(description='Unknown program that breaks in strange ways.') parser.add_argument("--download", help="Download Images", action="store_true") parser.add_argument("JSON", type=str, nargs="?", help="Filename to save results", default="test.js") args = parser.parse_args() pprint(args) # Should we spend the time to download image, and process it? (True = Yes, False = No) # DOWNLOAD = False DOWNLOAD = args.download # Should we add the JSON in a file? (True is filename, False = do not do) # JSONME = 'test.js' JSONME = args.JSON # NOTE: To begin the insert of the JSONIFIED image and word its # // T # A JS comment with a uppercase T # To stop its # // t # A JS comment with a lowercase t # httpbin.org/headers sess = requests.Session() head = {'User-Agent': 'Mozilla/5.0 (Macintosh; Intel Mac OS X 10_10_1) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/39.0.2171.95 Safari/537.36'} sess.headers.update(head) ON = 'X' OFF = '.' DIR = 'data' INTENSITY = 75 im = '' pix = [0, 0] size = [0, 0] pal = '' if not os.path.exists('in'): os.mkdir('in') if not os.path.exists('data'): os.mkdir('data') def download(howhard, index): global sess """ Download an image based upon how hard it is. Note: This sucks, because it overwrites images. Why not save the images and how hard? Quit running your script on floppy disks! """ r = sess.get(f'http://s0urce.io/client/img/word/{howhard}/{index}') if(r.status_code == 200): with open(f'in/{howhard}_{index}.png', 'wb') as f: f.write(r.content) return f'{howhard}_{index}.png ' + str(r.status_code) return r.status_code def img_point(x, y): global pix return pix[x, y] def img_avg(x, y): global im, pal rgb = img_point(x,y) if(im.mode == 'P'): rgb = pal[rgb*3:(rgb+1)*3] if(im.mode == 'I'): return rgb >> 8 return int( ( rgb[0] + rgb[1] + rgb[2] ) / 3 ) def is_set(x, y): global INTENSITY avg = img_avg(x,y) return (avg < INTENSITY) def scan_img(): global sx, sy, ex, ey, total total = 0 sx = size[0]; ex = 0 sy = size[1]; ey = 0 for y in range( 0,size[1] ): for x in range( 0,size[0] ): pnt_is = is_set(x,y) if (pnt_is): total += 1 if x < sx: sx = x if x > ex: ex = x if y < sy: sy = y if y > ey: ey = y #print (sx,ex,sy,ey) # give us a little border to work with if sx > 0: sx -= 1 if ex < size[0]: ex += 1 if sy > 0: sy -= 1 if ey < size[1]: ey += 1 #print (sx,ex,sy,ey) return(sx,sy,ex,ey) def save_image(): result = [] for y in range(sy,ey): s = '' for x in range(sx,ex): if is_set(x,y): s += ON else: s += OFF result.append(s) return result def run(difficult): for x in range(0, 70): fname = f'in/{difficult}_{x}.png' try: with open(fname, 'r') as f: f = f except FileNotFoundError: print("File does not exist") continue print(f"Loading: {fname}") im = Image.open(fname) pix = im.load() size = im.size print(f"Size: {size[0]} x {size[1]}") if(im.mode == 'P'): pal = im.getpalette() sx = 0 ex = size[0] sy = 0 ey = size[1] total = 0 scan_img() print(f"Chars within ({sx}, {sy}) - ({ex}, {ey}) total {total} pixels") img_s = save_image() for l in img_s: print(l) img_s.append(input('Word: ')) with open(f'{DIR}/{difficult}_{x}.txt', 'w') as f: for i_s in img_s: f.write(f'{i_s}\n') print(f"Image saved to '{DIR}/{difficult}_{x}.txt' in byte string") # os.remove(f'{fname}') # Grr No bad bean, keep file for error checking # print(f"File '{fname}' automatically removed") if (DOWNLOAD == True): print("Downloading s0urce.io Words") print("EASY") # time.sleep(5) for e in range(0, 62): print(download('e', e)) # time.sleep(random.randint(10, 15)) print("MEDIUM") # time.sleep(5) for m in range(0, 66): print(download('m', m)) # time.sleep(random.randint(10, 15)) print("HARD") # time.sleep(5) for h in range(0, 55): print(download('h', h)) # time.sleep(random.randint(10, 15)) # Img Processing run('e') # Answer the questions run('m') run('h') # ---------------------------------------------------------------------------------------- # All below was in a seperate dataJS.py file... but now I have fixed it so it's 1 script! if (JSONME.lower() != 'false'): print("Now exporting to JSON") print(f"Targeting file: '{JSONME}'") time.sleep(5) def test(t): global DIR fname = f'{DIR}/{t}.txt' r = [] try: with open(fname, 'r') as f: for l in f: r.append(l.strip()) return r except FileNotFoundError: return None def insertJS(item): global JSON item = json.dumps(item) item = f'{item},' r = [] try: with open(f'{JSONME}', 'r') as f: for l in f: if l != '': r.append(l.strip('\n')) else: r.append('') except FileNotFoundError: print(f"File {JSONME} Not Found!") sys.exit() c = 0 for e in r: if('// T' == e): temp = r[c+1] del r[c+1] r.insert(c+1, item) r.insert(c+2, temp) elif('// t' == e): break c += 1 with open(f'{JSONME}', 'w') as f: for e in r: f.write(f'{e}\n') for x in range(0, 183): te = test(x) if(te != None): word = te insertJS(word) print("Complete")