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

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