ARGH
[matches/honours.git] / research / TCS / pressure / get_digits.py
1 #! /usr/bin/python
2
3 from PIL import Image
4 import Image, ImageTk, ImageDraw
5 import sys
6 import os
7
8
9
10 def PixelGood(pixel, threshold):
11         return pixel[1] > threshold
12         return (pixel[1] > (pixel[0] + pixel[2]) and pixel[1] > threshold)
13
14
15 def rect_size(e):
16         return (e[3] - e[1]) * (e[2] - e[0])
17
18 def GreyScale(pixel):
19         greyscale = 0
20         for i in range(0, len(pixel)):
21                 greyscale += pixel[i]
22         greyscale = greyscale / len(pixel)
23         return greyscale
24
25 def Process(threshold):
26         
27         xMin = image.size[0]
28         xMax = 0
29         yMin = image.size[1]
30         yMax = 0
31
32         for x in range(0, image.size[0]):
33                 for y in range(0, image.size[1]):
34                         if (PixelGood(pix[x, y], threshold)):
35                                 if (x < xMin):
36                                         xMin = x
37                                 if (x > xMax):
38                                         xMax = x
39                                 if (y < yMin):
40                                         yMin = y
41                                 if (y > yMax):
42                                         yMax = y
43
44         test = Image.new("RGB", (xMax - xMin + 1, yMax - yMin + 1), "white")
45         draw = ImageDraw.Draw(test)
46
47         for x in range(xMin, xMax+1):
48                 for y in range(yMin, yMax+1):
49                         if (PixelGood(pix[x, y], threshold)):
50                                 draw.rectangle([x-xMin, y-yMin, x-xMin, y-yMin], "black")
51                         else:
52                                 draw.rectangle([x-xMin, y-yMin, x-xMin, y-yMin], "white")
53
54         #test.show()
55         #test.resize([test.size[0]*2, test.size[1]*2]).save(output_file)
56         test.save(output_file)
57         #os.system("convert " + str(output_file) + " -resize 40% " + str(output_file))
58         sys.exit(0)
59
60
61
62 if (len(sys.argv) != 3):
63         print("Usage " + str(sys.argv[0]) + " input output")
64
65 input_file = sys.argv[1]
66 output_file = sys.argv[2]
67
68 image = Image.open(input_file)
69
70 pix = image.load()
71
72 # This displays all good pixels in the image; testing
73 """
74 draw = ImageDraw.Draw(image)
75 for x in range(0, image.size[0]):
76         for y in range(0, image.size[1]):
77                 #print(str(pix[x, y]))
78                 if (PixelGood(pix[x, y], 220)):
79                         draw.rectangle((x,y, x,y), "black")
80                 else:
81                         draw.rectangle((x,y, x,y), "white")
82
83 image.show()
84 sys.exit(0)
85 """
86
87 Process(220)
88
89

UCC git Repository :: git.ucc.asn.au