From bdeb51fdb2d9d69854d2fd7a7ead6e5e06d92973 Mon Sep 17 00:00:00 2001 From: HF Date: Sat, 25 Apr 2020 08:21:01 +0200 Subject: [PATCH] add script that cleans spare pixels from images --- utils/README.md | 3 +++ utils/imageClean.py | 51 +++++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 54 insertions(+) create mode 100755 utils/imageClean.py diff --git a/utils/README.md b/utils/README.md index 24a0da5..0f1752b 100644 --- a/utils/README.md +++ b/utils/README.md @@ -31,3 +31,6 @@ Script that connects to the mysql database and does some stuff, just for testing ## proxyConvert.sh Converts a proxy list in specific txt format to a better readable list + +## imageClean.py +python3 script that takes an input image and cleares spare pixels and bot remains diff --git a/utils/imageClean.py b/utils/imageClean.py new file mode 100755 index 0000000..01e8486 --- /dev/null +++ b/utils/imageClean.py @@ -0,0 +1,51 @@ +#!/usr/bin/env python3 +# this script filters out noise froma n indexed image +import PIL.Image +import sys + + +def check_pixel(pix, x, y): + # if pixel is sourrounded by just the same color + # and max one different one + cnt_clr1 = 0 + cnt_clr2 = 0 + clr1 = pix[x-1,y-1] + clr2 = None + for xrel in range(-1, 2): + for yrel in range(-1, 2): + if not xrel and not yrel: + continue + clr = pix[x + xrel,y + yrel] + if clr == clr1: + cnt_clr1 += 1 + elif clr2 is None: + clr2 = clr + cnt_clr2 += 1 + elif clr == clr2: + cnt_clr2 += 1 + else: + return None + if cnt_clr1 > 1 and cnt_clr2 > 1: + return None + if cnt_clr1 > 1: + return clr1 + return clr2 + +def clean_image(filename): + im = PIL.Image.open(filename).convert('RGBA') + width, height = im.size + pix = im.load() + im_new = PIL.Image.new('RGBA', (width, height), (255, 0, 0, 0)) + pix_new = im_new.load() + for x in range(1, width - 1): + for y in range(1, height - 1): + target = check_pixel(pix, x, y) + if target is not None and target != pix[x, y]: + pix_new[x,y] = target + im.close() + im_new.save("%s-cleaned.png" % filename[:-4]) + im_new.close() + +if __name__ == "__main__": + filename = sys.argv[1] + clean_image(filename)