Greyscale, im_debug
authorTomas Musil <tomik.musil@gmail.com>
Tue, 20 Mar 2012 23:08:49 +0000 (00:08 +0100)
committerTomas Musil <tomik.musil@gmail.com>
Tue, 20 Mar 2012 23:31:41 +0000 (00:31 +0100)
greyscale.py: Short script to test different methods of
transforming RGB images to greyscale.
im_debug.py: Simple script using pygame to display images for
debugging purposes.
image.jpg: Photo of a goban with game (guess which one) being
replayed. Something to test these scripts on.

greyscale_test.py [new file with mode: 0644]
im_debug.py [new file with mode: 0644]
image.jpg [new file with mode: 0644]

diff --git a/greyscale_test.py b/greyscale_test.py
new file mode 100644 (file)
index 0000000..97ed3ac
--- /dev/null
@@ -0,0 +1,20 @@
+import Image, ImageMath
+import im_debug
+import sys
+
+im = Image.open('./image.jpg')
+im_debug.show(im, 'Original image')
+
+im_l = im.convert('L')
+im_debug.show(im_l, 'ITU-R 601-2 luma transform')
+
+im_rgb = im.convert('L', (float(1)/3, float(1)/3, float(1)/3, 0))
+im_debug.show(im_rgb, '(R+G+B)/3 transform')
+
+s1, s2 = im.size
+im2 = Image.new('L', (s1 * 2, s2))
+im2.paste(im_l, (0, 0, s1, s2))
+im2.paste(im_rgb, (s1, 0, 2*s1, s2))
+im_debug.show(im2, 'Both transforms side by side')
+
+im_debug.show(ImageMath.eval("convert(abs(a - b)*5, 'L')", a=im_l, b=im_rgb), 'Difference')
diff --git a/im_debug.py b/im_debug.py
new file mode 100644 (file)
index 0000000..9b72ed3
--- /dev/null
@@ -0,0 +1,23 @@
+import pygame
+import Image
+
+def show(image, caption=''):
+    if image.mode != 'RGB':
+        image = image.convert('RGB')
+    pygame.init()
+    if caption:
+        caption = "Imago: " + caption
+    else:
+        caption = "Imago"
+    pygame.display.set_caption(caption)
+    pygame.display.set_mode(image.size)
+    main_surface = pygame.display.get_surface()
+    picture = pygame.image.frombuffer(image.tostring(), image.size, image.mode)
+    main_surface.blit(picture, (0, 0))
+    pygame.display.update()
+    while True:
+        events = pygame.event.get()
+        for event in events:
+            if event.type == pygame.QUIT or event.type == pygame.KEYDOWN:
+                pygame.quit()
+                return
diff --git a/image.jpg b/image.jpg
new file mode 100644 (file)
index 0000000..a66aae9
Binary files /dev/null and b/image.jpg differ