In this short post, Mike Fletcher and the effbot show (and discuss detailed variation) an excellent approach for the task you want to do.
Edit: as for the 10K requirement, it's hard (to say the least;-) to predict how well an image will compress, depending on the image's format, since today's compression algorithms are so subtle. If you want your thumbnail to be just about as large (in pixels) as feasible while respecting a <10K requirement, you may have to use a "trial and error" approach making successively more refined guesses about the scaling factor, until you reach an acceptable result.
For example, here's a "binary search" approach to getting the correct size (there may well be better strategies!), with ample print statements &c to explain what's going on...:
import Image
import cStringIO
import math
import os
import stat
# try no more than 10 times, then give up
MAX_TRIES = 10
def getThumbnail(filename, max_bytes=(10*1024)):
'''Get a thumbnail image of filename, <max_bytes'''
original_size = os.stat(filename)[stat.ST_SIZE]
print "Original file size: %.1f KB" % (original_size/1024.)
image = Image.open(filename)
image.load()
print "Original image size: %dx%d pixels" % image.size
min_bytes = int(0.9 * max_bytes)
largest_side = max(image.size)
smallest_side = 16
for attempt in range(MAX_TRIES):
try_side = (largest_side + smallest_side) / 2
print "Attempt #%d of %d" % (attempt+1, MAX_TRIES)
print "Side must be within [%d:%d], now trying %d" % (
smallest_side, largest_side, try_side)
thumb = image.copy()
thumb.thumbnail((try_side,try_side), Image.ANTIALIAS)
afile = cStringIO.StringIO()
thumb.save(afile, "PNG")
resulting_size = len(afile.getvalue())
afile.close()
print "Reduced file size: %.1f KB" % (resulting_size/1024.)
print "Reduced image size: %dx%d pixels" % thumb.size
if min_bytes <= resulting_size <= max_bytes:
print "Success!"
return thumb
elif resulting_size > max_bytes:
print "Too large (>%d), reducing more" % max_bytes
largest_side = try_side
else:
print "Too small (<%d), reducing less" % min_bytes
smallest_side = try_side
print "too many attempts, returning what I've got!"
return thumb
def main():
thumb = getThumbnail("pyth.png")
print "Reduced image size: %dx%d pixels" % thumb.size
print "Saving to thumb.png"
thumb.save("thumb.png")
thumb_size = os.stat("thumb.png")[stat.ST_SIZE]
print "Reduced file size: %.1f KB" % (thumb_size/1024.)
print "Done, bye!"
if __name__ == '__main__':
main()