Subscribe
from PIL import Image
import os
import sys
from math import sqrt
def process_dir(dir):
for f in os.listdir(dir):
file = dir + os.sep + f
if os.path.isdir(file):
process_dir(file)
else:
dofile(dir,f)
return
def dofile(dir,f):
im = Image.open(dir + os.sep + f)
thewidth = im.size[0]
theheight = im.size[1]
cv=[]
pix = im.load()
for line in range(0,theheight):
pixellist = []
for pixel in range(0,thewidth):
avg = (pix[pixel,line][0] + pix[pixel,line][1] + pix[pixel,line][2]) / 3
pixellist.append(avg)
lineavg = 0
for pixel in pixellist:
lineavg = lineavg + pixel
lineavg = lineavg / thewidth
linecv = 0
for pixel in pixellist:
linecv = linecv + (pixel-lineavg)**2
linecv = sqrt(linecv)
cv.append(linecv)
max = 0
maxtest = 0
for line in range(len(cv)):
if cv[line] > maxtest:
max = line
maxtest = cv[line]
ratio = 1900 / 2448
hratio = 0.112
sratio = .388
maxbottom = max + int(sratio * theheight)
maxtop = max - int(sratio * theheight)
if maxtop <> maxtop = 1
maxbottom = maxtop + (sratio * 2 * theheight)
if maxbottom > (theheight - 1):
maxbottom = theheight-1
maxtop = maxbottom - (sratio * 2 * theheight)
cd = im.crop((1,int(maxtop),thewidth-1,int(maxbottom)))
rd = cd.resize((640,480))
print dir + os.sep + "small" + f
#rd.save(dir + os.sep + "small" + f,quality=95, optimize=1)
rd.save(dir + os.sep + "small" + f)
thedir = 'K:\photoframe'
process_dir(thedir)
>
You are not logged in, either login or create an account to post comments
posted by Blazecock Pileon at 9:20 PM on September 26, 2007