jueves, julio 03, 2008

get the thumbnail of a file with python

with this function you can obtain the the path where is stored (returns None if the thumbnail doesn't exists) the thumbnail of the given uri.


import md5
import os.path

def thumbnail_path_from_uri(uri, size='normal'):
"""Construct the path for the thumbnail of the given uri

Arguments:
- `uri`: the uri that points to the file that is looking for the thumbnail.
- `size`: the size of the thumbnail (normal or large)
"""


assert isinstance(uri, basestring), \
TypeError("The uri must be a str")

assert isinstance(size, basestring) and (size == 'large' or size=='normal'), \
TypeError("The size for thumbnail can be normal or large")

hash = md5.new()
hash.update(uri)
path = os.path.join(os.path.expanduser('~'), ".thumbnails", size, str("%s.png" % hash.hexdigest()))

if os.path.exists(path):
return path
else:
return None

No hay comentarios.: