From cb5f68ed0aa3a0f1f8ae416d090a7faadace81b5 Mon Sep 17 00:00:00 2001 From: Tomas Musil Date: Thu, 5 Apr 2012 22:02:10 +0200 Subject: [PATCH 1/1] edge detection --- filter.py | 15 +++++++++++++++ imago.py | 25 +++++++++++++++++++++---- 2 files changed, 36 insertions(+), 4 deletions(-) create mode 100644 filter.py diff --git a/filter.py b/filter.py new file mode 100644 index 0000000..c378f76 --- /dev/null +++ b/filter.py @@ -0,0 +1,15 @@ +from PIL import Image + +def filter(image): + image_l = image.load() + new_image = Image.new('L', image.size) + new_image_l = new_image.load() + for x in range(2, image.size[0] - 2): + for y in range(2, image.size[1] - 2): + pix = sum([sum([image_l[a, b] for b in range(y - 2, y + 3)]) for a in range(x - 2, x + 3)]) - (25 * image_l[x, y]) + if pix > 255: + pix = 255 + if pix < 0: + pix = 0 + new_image_l[x, y] = pix + return new_image diff --git a/imago.py b/imago.py index 1d364a5..94f90a0 100755 --- a/imago.py +++ b/imago.py @@ -4,19 +4,36 @@ import Image import im_debug import sys +from filter import filter + +class Usage(Exception): + def __init__(self, msg): + self.msg = msg def main(argv=None): """Main function of the program.""" - if argv is None: - argv = sys.argv[1] + try: + if argv is None: + try: + argv = sys.argv[1] + except IndexError: + raise Usage('no arguments given') + except Usage, err: + print >>sys.stderr, err.msg + print >>sys.stderr, "for help use --help" + return 2 #TODO exception on empty argument #TODO exception on file error image = Image.open(argv) - im_debug.show(image, "Original image") + im_debug.show(image, "original image") im_l = image.convert('L') im_debug.show(im_l, "ITU-R 601-2 luma transform") + im_edges = filter(im_l) + im_debug.show(im_edges, "edge detection") + if __name__ == '__main__': - main() + main() #sys.exit(main()) + -- 2.4.2