[go: up one dir, main page]

Menu

[r3]: / histogram.py  Maximize  Restore  History

Download this file

56 lines (41 with data), 1.3 kB

 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
44
45
46
47
48
49
50
51
52
53
54
55
#! /usr/bin/env python
# -*- coding: utf-8 -*-
import math
from PIL import Image
NUMBEROFIMAGES = 700
NUMBEROFBINS = 512
NOOFDIVISION = int(round(pow(NUMBEROFBINS,1.0/3)))
SCALE = 256*256*256
def getbin(r,g,b):
rp=r/(256/NOOFDIVISION)
gp=g/(256/NOOFDIVISION)
bp=b/(256/NOOFDIVISION)
#print rp, gp, bp
#color = rp*NOOFDIVISION**2+gp*NOOFDIVISION**1+bp
#factor = SCALE/NUMBEROFBINS
return rp*NOOFDIVISION**2+gp*NOOFDIVISION**1+bp
fout = open('histogram.txt','w')
fout.write(str(NUMBEROFIMAGES)+'\n')
fout.write(str(NUMBEROFBINS)+'\n')
print 'starting...'
for m in range(1,NUMBEROFIMAGES+1):
print 'generating HISTOGRAM for',str(m)+'.jpg'
fout.write(str(m)+'.jpg\n')
im = Image.open(str(m)+'.jpg')
freq = [0 for i in range(NUMBEROFBINS)]
pixels = list(im.getdata())
width, height = im.size
for i in pixels:
r,g,b = i
ind = getbin(r,g,b)
#print ind
freq[ind] = freq[ind]+1
for i in range(NUMBEROFBINS):
fout.write(str(freq[i]/(width*height*1.0)))
#print freq[i]/(width*height*1.0)
fout.write('\n')
countp=0;
for i in range(NUMBEROFBINS): countp=countp+freq[i]/(width*height*1.0)
print countp
fout.close()
print 'Done!'