-
Notifications
You must be signed in to change notification settings - Fork 0
/
Threshold.py
43 lines (35 loc) · 1.15 KB
/
Threshold.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
#!/usr/bin/python
# -*- coding: utf-8 -*-
__author__ = 'Josefoo'
from math import log1p
from Tkinter import *
from tkFileDialog import askopenfilename
import tkMessageBox
from PIL import Image
class Threshold():
def __init__(self,image):
self.imag=image
self.width=image.size[0]
self.height=image.size[1]
self.newImag= Image.new(self.imag.mode,self.imag.size)
def threshold(self,threshold):
UMBRAL = threshold
for x in xrange(self.width):
for y in xrange(self.height):
r,g,b = self.imag.getpixel((x,y))
if (r+g+b)/3 > UMBRAL:
self.newImag.putpixel((x,y),(255,255,255))
else:
self.newImag.putpixel((x,y),(0,0,0))
self.newImag.save("threshold.png",self.imag.format)
self.newImag.show()
print("Done...")
def main():
Tk().withdraw()
tkMessageBox.showinfo( "Image", "Select an Image")
usrImag = askopenfilename()
thres=Threshold(Image.open(str(usrImag)))
value =int(raw_input("Give me the threshold value"))
thres.threshold(value)
if __name__=="__main__":
main()