|  | @@ -0,0 +1,181 @@
 | 
	
		
			
				|  |  | +
 | 
	
		
			
				|  |  | +from PIL import Image
 | 
	
		
			
				|  |  | +import os
 | 
	
		
			
				|  |  | +import subprocess
 | 
	
		
			
				|  |  | +
 | 
	
		
			
				|  |  | +ON = "X"  # Dark pixel in an image
 | 
	
		
			
				|  |  | +OFF = "."  # Light pixel in an image
 | 
	
		
			
				|  |  | +
 | 
	
		
			
				|  |  | +INTENSITY = 75
 | 
	
		
			
				|  |  | +# How bright does something have to be to trigger it being a dark or light pixel?
 | 
	
		
			
				|  |  | +# Looks like around 75 removes the extra stuff that s0urce.io does to prevent it from being just matching images.
 | 
	
		
			
				|  |  | +
 | 
	
		
			
				|  |  | +GREEN_DIFF = 10
 | 
	
		
			
				|  |  | +# How much brighter the green channel must be (compared to the others),
 | 
	
		
			
				|  |  | +# to be called green.
 | 
	
		
			
				|  |  | +
 | 
	
		
			
				|  |  | +
 | 
	
		
			
				|  |  | +
 | 
	
		
			
				|  |  | +def img_point(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(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 = 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(pix, x, y):
 | 
	
		
			
				|  |  | +    global INTENSITY
 | 
	
		
			
				|  |  | +    """
 | 
	
		
			
				|  |  | +    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 = img_avg(pix, x, y)
 | 
	
		
			
				|  |  | +    return avg < INTENSITY
 | 
	
		
			
				|  |  | +
 | 
	
		
			
				|  |  | +
 | 
	
		
			
				|  |  | +def is_green(image, x, y):
 | 
	
		
			
				|  |  | +    """
 | 
	
		
			
				|  |  | +    Is this pixel Green?
 | 
	
		
			
				|  |  | +    """
 | 
	
		
			
				|  |  | +    (red, green, blue, _) = image.getpixel((x, y))
 | 
	
		
			
				|  |  | +    # Find the difference between green and the other values.
 | 
	
		
			
				|  |  | +    other = red
 | 
	
		
			
				|  |  | +    if blue > other:
 | 
	
		
			
				|  |  | +        other = blue
 | 
	
		
			
				|  |  | +    diff = green - other
 | 
	
		
			
				|  |  | +    return diff > GREEN_DIFF
 | 
	
		
			
				|  |  | +
 | 
	
		
			
				|  |  | +
 | 
	
		
			
				|  |  | +def scan_img(image):
 | 
	
		
			
				|  |  | +    """
 | 
	
		
			
				|  |  | +    Given an image, look at a image and find dark pixels,
 | 
	
		
			
				|  |  | +
 | 
	
		
			
				|  |  | +    also find the bounding box size.
 | 
	
		
			
				|  |  | +    (Where's the top,left,right,bottom-most pixels at?)
 | 
	
		
			
				|  |  | +
 | 
	
		
			
				|  |  | +    returns start x, y and end x, y and total number of dark pixels.
 | 
	
		
			
				|  |  | +    """
 | 
	
		
			
				|  |  | +    total = 0
 | 
	
		
			
				|  |  | +    size = image.size    
 | 
	
		
			
				|  |  | +    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(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
 | 
	
		
			
				|  |  | +
 | 
	
		
			
				|  |  | +    return (sx, sy, ex, ey, total)
 | 
	
		
			
				|  |  | +
 | 
	
		
			
				|  |  | +
 | 
	
		
			
				|  |  | +def output_image(image):
 | 
	
		
			
				|  |  | +    """
 | 
	
		
			
				|  |  | +    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)
 | 
	
		
			
				|  |  | +    """
 | 
	
		
			
				|  |  | +    size = image.size
 | 
	
		
			
				|  |  | +    result = []
 | 
	
		
			
				|  |  | +
 | 
	
		
			
				|  |  | +    for y in range(0, size[1]):
 | 
	
		
			
				|  |  | +        s = ""
 | 
	
		
			
				|  |  | +        for x in range(0, size[0]):
 | 
	
		
			
				|  |  | +            # if is_set(pix, x, y):
 | 
	
		
			
				|  |  | +            if not is_green(image, x, y):
 | 
	
		
			
				|  |  | +                s += ON
 | 
	
		
			
				|  |  | +            else:
 | 
	
		
			
				|  |  | +                s += OFF
 | 
	
		
			
				|  |  | +        result.append(s)
 | 
	
		
			
				|  |  | +
 | 
	
		
			
				|  |  | +    return result
 | 
	
		
			
				|  |  | +
 | 
	
		
			
				|  |  | +
 | 
	
		
			
				|  |  | +def image_cleaner(source, destination, padfile=True):
 | 
	
		
			
				|  |  | +    """ Given a source image, clean it and save to destination
 | 
	
		
			
				|  |  | +
 | 
	
		
			
				|  |  | +    If padfile is set, we use imagemagik to add additional padding
 | 
	
		
			
				|  |  | +    around the image, which helps with the conversion process.
 | 
	
		
			
				|  |  | +    """
 | 
	
		
			
				|  |  | +    image = Image.open(source)
 | 
	
		
			
				|  |  | +    size = image.size
 | 
	
		
			
				|  |  | +
 | 
	
		
			
				|  |  | +    for y in range(0, size[1]):
 | 
	
		
			
				|  |  | +        for x in range(0, size[0]):
 | 
	
		
			
				|  |  | +            (r, g, b, _) = image.getpixel( (x,y) )
 | 
	
		
			
				|  |  | +            high = r
 | 
	
		
			
				|  |  | +            if b > high:
 | 
	
		
			
				|  |  | +                high = b
 | 
	
		
			
				|  |  | +            diff = g - high
 | 
	
		
			
				|  |  | +            is_green = diff > 0 # GREEN_DIFF
 | 
	
		
			
				|  |  | +
 | 
	
		
			
				|  |  | +            if is_green:
 | 
	
		
			
				|  |  | +                image.putpixel( (x,y), (255,255,255,255) )
 | 
	
		
			
				|  |  | +            else:
 | 
	
		
			
				|  |  | +                image.putpixel( (x,y), (0, 0, 0, 255) )
 | 
	
		
			
				|  |  | +
 | 
	
		
			
				|  |  | +    if padfile:
 | 
	
		
			
				|  |  | +        tmpfile = "temp.png"
 | 
	
		
			
				|  |  | +        image.save(tmpfile)
 | 
	
		
			
				|  |  | +        output = subprocess.run(
 | 
	
		
			
				|  |  | +            ["convert", tmpfile, "-bordercolor", "White",
 | 
	
		
			
				|  |  | +            "-border", "10x10", "-alpha", "off", destination],
 | 
	
		
			
				|  |  | +            stderr=subprocess.DEVNULL,
 | 
	
		
			
				|  |  | +            # capture_output=False,
 | 
	
		
			
				|  |  | +            shell=False,
 | 
	
		
			
				|  |  | +        )
 | 
	
		
			
				|  |  | +        os.unlink(tmpfile)
 | 
	
		
			
				|  |  | +
 | 
	
		
			
				|  |  | +    else:            
 | 
	
		
			
				|  |  | +        image.save(destination)
 | 
	
		
			
				|  |  | +
 |