Skip to content

Instantly share code, notes, and snippets.

@abrahamjso
Created April 23, 2013 06:37
Show Gist options
  • Save abrahamjso/5441292 to your computer and use it in GitHub Desktop.
Save abrahamjso/5441292 to your computer and use it in GitHub Desktop.
from PIL import Image, ImageDraw
from sys import argv
def gray_scale(img):
pixel = img.load()
w, h = img.size
for i in range(w):
for j in range(h):
r, g, b = pixel[i,j]
media = (r+g+b)/3
pixel[i,j] = (media, media, media)
return img
def lista_horizontal(img):
pixel = img.load()
w, h = img.size
return lista_histograma(w, h, pixel, 'horizontal')
def lista_vertical(img):
pixel = img.load()
w, h = img.size
return lista_histograma(h, w, pixel, 'vertical')
def lista_histograma(a, b, pixel, type_l):
lista = list()
for x in range(a):
suma = 0
for y in range(b):
if(type_l == 'horizontal'): suma += int(pixel[x,y][0])
else: suma += int(pixel[y,x][0])
lista.append(suma)
return lista
def lista_minimos(list_hist):
lista = list()
for i in range(len(list_hist)):
try:
if(list_hist[i-1] > list_hist[i] and list_hist[i+1] > list_hist[i]): lista.append(i)
except:
pass
lista.sort()
return lista
def dibujar_lineas(img, horizontal, vertical):
w, h = img.size
draw = ImageDraw.Draw(img)
azul = (0, 255, 0)
verde = (0, 0, 255)
for x in horizontal:
draw.line((x, 0, x, w), azul)
for y in vertical:
draw.line((0, y, h, y), verde)
img.save("histograma.png", 'PNG')
def main():
img = Image.open(argv[1])
img = gray_scale(img)
horizontal = list(lista_horizontal(img))
vertical = lista_vertical(img)
horizontal = lista_minimos(horizontal)
vertical = lista_minimos(vertical)
img = Image.open(argv[1])
dibujar_lineas(img, horizontal, vertical)
main()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment