Skip to content

Instantly share code, notes, and snippets.

@SaulG
Created April 25, 2013 02:51
from PIL import ImageDraw
from sys import argv
import numpy as np
from Tkinter import *
import Image
import ImageTk
DEBUG = True
def grayScale(image):
global DEBUG
width, height = image.size
if DEBUG:
print "Prueba de impresion de proporciones w: %s h: %s"%(width, height)
img = image.load()
for y in xrange(height):
for x in xrange(width):
r, g, b = img[ x, y ]
escala = int(r+g+b/3)
img[ x, y] = escala, escala, escala
return Image.fromarray(np.array(image))
def detectHole(image):
xIndex, yIndex = indicesDondeHayObjetos(image)
width, height = image.size
image = image.convert('RGB')
img = image.load()
#print xIndex
#print yIndex
for y in xrange(height):
for x in xrange(width):
r, g, b = img[ x, y ]
if x in xIndex:
#print "Encontre uno : ",x
img[x, y] = 255, 0, 0
if y in yIndex:
#print "Encontre uno : ",y
img[x, y] = 0, 0, 255
return Image.fromarray(np.array(image))
def indicesDondeHayObjetos(image):
global DEBUG
width, height = image.size
if DEBUG:
print "Prueba de impresion de proporciones w: %s h: %s"%(width, height)
image = image.convert('L')
img = image.load()
img_array = np.array(image)
xSum = histogramAxisX(img_array)
ySum = histogramAxisY(img_array)
#print xSum, ySum
return indices(xSum, np.amin(xSum)), indices(ySum, np.amin(ySum))
def indices(array, min):
#print "Valor min: ",min
return np.where(array == min)[0]
def histogramAxisX(array):
return np.sum( array, axis=0 )
def histogramAxisY(array):
return np.sum( array, axis=1 )
def printValues(x, y):
print "Tamanio de x: %s y: %s "%(len(x), len(y))
for j in xrange(len(y)):
for i in xrange(len(x)):
print "%s %s"%(x[i], y[j])
return
def tk(imagen):
root = Tk()
width, height = imagen.size
canvas = Canvas(root, width=width, height=height)
canvas.pack(expand=YES, fill=BOTH)
imagen_canvas = ImageTk.PhotoImage(imagen)
imagen_canvas_setting = canvas.create_image((2, 2), image=imagen_canvas, anchor=NW)
root.mainloop()
return
def main():
global DEBUG
imagen = Image.open(argv[1])
try:
DEBUG = argv[2]
except:
DEBUG = False
grayScale(imagen)
imagen = detectHole(imagen)
return tk(imagen)
main()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment