|
@@ -0,0 +1,161 @@
|
|
|
+#!/usr/bin/env python3
|
|
|
+
|
|
|
+from PIL import Image
|
|
|
+
|
|
|
+import sys, os, json, argparse
|
|
|
+
|
|
|
+class imgGrey():
|
|
|
+ """
|
|
|
+ Uses Grey scaling to tell what is black and white or grey in an image
|
|
|
+ """
|
|
|
+
|
|
|
+ def __init__(self, on='X', off=' ', intensity=64):
|
|
|
+ """
|
|
|
+ Here is where you set stuff like ON, OFF and INTENSITY
|
|
|
+ """
|
|
|
+ self.ON = on
|
|
|
+ self.OFF = off
|
|
|
+ self.INTENSITY = intensity
|
|
|
+
|
|
|
+ def img_point(self, pix, x, y):
|
|
|
+ """
|
|
|
+ img_point, returns a pixel of an image,
|
|
|
+ given the x and y on the image.
|
|
|
+ """
|
|
|
+ return pix[x, y]
|
|
|
+
|
|
|
+ def img_avg(self, pix, x, y):
|
|
|
+ """
|
|
|
+ img_avg, returns the average brightness 0-255,
|
|
|
+ given pixel, and the x and y on the image calls img_point,
|
|
|
+ to get the individual rgb values to calculate,
|
|
|
+ brightness. (Grey scale)
|
|
|
+ """
|
|
|
+ rgb = self.img_point(pix, 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(self, pix, x, y):
|
|
|
+ """
|
|
|
+ is_set, returns True or False of calculating,
|
|
|
+ the brightness of the given point on a image,
|
|
|
+ compared to given intensity.
|
|
|
+
|
|
|
+ True means the brightness at the given x and y,
|
|
|
+ is Less Than which means its dark.
|
|
|
+
|
|
|
+ False means the brightness at the given x and y,
|
|
|
+ is Greater Than which means its bright. (Grey Scale)
|
|
|
+ """
|
|
|
+ avg = self.img_avg(pix, x,y)
|
|
|
+ return (avg < self.INTENSITY)
|
|
|
+
|
|
|
+ def scan_img(self, pix, size):
|
|
|
+ """
|
|
|
+ scan_img, looks at a image and looks for dark pixels,
|
|
|
+ if it is a dark pixel record the number and resize the,
|
|
|
+ returned values to show where the most dark pixels on the,
|
|
|
+ image are located. (Grey Scale)
|
|
|
+
|
|
|
+ given pixel, and image size.
|
|
|
+ returns start x, y and end x, y and total number of dark pixels.
|
|
|
+ """
|
|
|
+ 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 = self.is_set(pix, 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,total)
|
|
|
+
|
|
|
+ def output_image(self, pix, size):
|
|
|
+ """
|
|
|
+ For the size of the area we have reduced down to where the majority of dark pixels,
|
|
|
+ are located, store all that into a list and return the list.
|
|
|
+
|
|
|
+ given pixel for function passing.
|
|
|
+ returns multiple strings in a list that are edited to use characters to represent,
|
|
|
+ the dark and light pixels of the image. (Grey Scale)
|
|
|
+ """
|
|
|
+ result = []
|
|
|
+ ex = size[0]; sx = 0
|
|
|
+ ey = size[1]; sy = 0
|
|
|
+ for y in range(sy,ey):
|
|
|
+ s = ''
|
|
|
+ for x in range(sx,ex):
|
|
|
+ if self.is_set(pix, x,y):
|
|
|
+ s += self.ON
|
|
|
+ else:
|
|
|
+ s += self.OFF
|
|
|
+ result.append(s)
|
|
|
+
|
|
|
+ return result
|
|
|
+
|
|
|
+ def run(self, fname):
|
|
|
+ """
|
|
|
+ run, represents a single execution of components to the image,
|
|
|
+ those components do the following...
|
|
|
+
|
|
|
+ We open and load the image, and get it's size,
|
|
|
+ then we scan_img for dark and light pixels, <-- This narrows the image down to just the majority of dark pixels
|
|
|
+ then from that we output the image line by line onto the screen after it has been output_image d into list form,
|
|
|
+ Where we ask the user what the word is, and after that we save all that to a file in the data directory.
|
|
|
+ """
|
|
|
+
|
|
|
+ if not os.path.exists(fname):
|
|
|
+ raise FileNotFoundError("Could not find '{0}'".format(fname))
|
|
|
+
|
|
|
+ print(f"Loading: {fname}")
|
|
|
+ im = Image.open(fname)
|
|
|
+ pix = im.load()
|
|
|
+ size = im.size
|
|
|
+ pal = im.getpalette()
|
|
|
+ print(f"Size: {size[0]} x {size[1]}")
|
|
|
+
|
|
|
+ sx = 0; ex = size[0]
|
|
|
+ sy = 0; ey = size[1]
|
|
|
+ total = 0
|
|
|
+
|
|
|
+ sx, sy, ex, ey, total = self.scan_img(pix, size)
|
|
|
+ print(f"Chars within ({sx}, {sy}) - ({ex}, {ey}) total {total} pixels")
|
|
|
+
|
|
|
+ img_s = self.output_image(pix, size)
|
|
|
+ for l in img_s:
|
|
|
+ print(l)
|
|
|
+
|
|
|
+ word = input('Word: ')
|
|
|
+ return word
|
|
|
+
|
|
|
+if __name__ == '__main__':
|
|
|
+ imgTest = imgGrey()
|
|
|
+ imgTest.run('test.png')
|