This was tricky because not all browsers show Tiff images so the trick is to generate the thumbnails as jpg.
thumb_image_format = None
if mimeType == "image/tiff" :
thumb_image_format = "JPEG"
ret_value = utils.create_thumbnail_pil(inputPath, outputPath, thumb_image_format)
def create_thumbnail_pil(self, infile, outfile, thumb_image_format=None):
import Image
size = 100, 100
try:
im = Image.open(infile)
new_image = im.copy()
if new_image.mode == "CMYK":
self.logger.info('converting CYMK to RGB for %s' , outfile)
new_image = new_image.convert("RGB")
new_image.thumbnail(size, Image.ANTIALIAS)
if thumb_image_format == None:
thumb_image_format = im.format
new_image.save(outfile, thumb_image_format)
return "OK: %s saved successfully" % outfile
except (IOError, ValueError):
self.logger.exception("Error creating thumbnail")
#remove 0 bytes file
if os.path.exists(outfile):
os.remove(outfile)
return "ERR: Error creating thumbnail for %s" % infile
thumb_image_format = None
if mimeType == "image/tiff" :
thumb_image_format = "JPEG"
ret_value = utils.create_thumbnail_pil(inputPath, outputPath, thumb_image_format)
def create_thumbnail_pil(self, infile, outfile, thumb_image_format=None):
import Image
size = 100, 100
try:
im = Image.open(infile)
new_image = im.copy()
if new_image.mode == "CMYK":
self.logger.info('converting CYMK to RGB for %s' , outfile)
new_image = new_image.convert("RGB")
new_image.thumbnail(size, Image.ANTIALIAS)
if thumb_image_format == None:
thumb_image_format = im.format
new_image.save(outfile, thumb_image_format)
return "OK: %s saved successfully" % outfile
except (IOError, ValueError):
self.logger.exception("Error creating thumbnail")
#remove 0 bytes file
if os.path.exists(outfile):
os.remove(outfile)
return "ERR: Error creating thumbnail for %s" % infile
Comments
Post a Comment