Skip to content

Instantly share code, notes, and snippets.

@schinken
Created April 21, 2012 22:18
Show Gist options
  • Save schinken/2439945 to your computer and use it in GitHub Desktop.
Save schinken/2439945 to your computer and use it in GitHub Desktop.
pil wrong colors
try:
im = Image.open( infile )
except IOError:
print "Cant load", infile
sys.exit(1)
curW, curH = size = im.size
curW = float( curW )
curH = float( curH )
print (curW/curH)
print "Image width x height:", curW, "x", curH
print "Output image-size:", newW, "x", newH
cropH = curW*(newH/newW)
cropH = round(cropH)
cropW = newW*(cropH/newH)
print "Maximum dimensions to crop", cropW, "x", cropH
x1, y1 = int( (curW-cropW)/2 ), int( (curH-cropH)/2 )
x2, y2 = int( cropW+x1 ), int( cropH+y1 )
print "Cropping from", x1, "x", y1, "to", x2, "x", y2
frames = []
try:
while 1:
frame = im.convert('RGB').crop( (x1,y1,x2,y2) )
frame.thumbnail( newsize )
data = frame.load()
newframe = {}
sizex, sizey = frame.size
for y in range(0, sizey-1 ):
for x in range(0, sizex-1 ):
pixel = data[ x, y ]
newframe[ x,y ] = int( toGrayscale( pixel ) )
sys.stdout.write( str( newframe[ x,y ] ) )
print
frames.append( newframe )
im.seek( im.tell() + 1 )
print "\n"
except EOFError:
pass # end of sequence
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment