def generate_thumbnail(doc_file_id): """ Celery task that tries to generate a thumbnail for a :class:`.DocumentFile`. If it succeed, this function modifies the :attr:`.DocumentFile.thumbnail` attribute. The stored value follow the following pattern :samp:`{doc_file_id}.png`. :param doc_file_id: id of the :class:`.DocumentFile`. """ doc_file = DocumentFile.objects.get(id=doc_file_id) ext = os.path.splitext(doc_file.filename)[1].lower() ext2 = get_ext(doc_file.filename) name = "%s.png" % (doc_file_id) thumbnail_path = thumbnailfs.path(name) generated = resize = False thumbnailers = ThumbnailersManager.get_all_thumbnailers(ext)[:] if ext2 != ext: thumbnailers.extend(ThumbnailersManager.get_all_thumbnailers(ext2)) for thumbnailer in thumbnailers: try: resize = thumbnailer(doc_file.file.path, doc_file.filename, thumbnail_path) except Exception, e: # let another thumbnailer do the job if os.path.exists(thumbnail_path): # this file may be corrupted os.remove(thumbnail_path) else: if os.path.exists(thumbnail_path): doc_file.thumbnail = os.path.basename(thumbnail_path) doc_file.no_index = True doc_file.save(update_fields=("thumbnail",)) generated = True break
def generate_thumbnail(doc_file_id): """ Celery task that tries to generate a thumbnail for a :class:`.DocumentFile`. If it succeed, this function modifies the :attr:`.DocumentFile.thumbnail` attribute. The stored value follow the following pattern :samp:`{doc_file_id}.png`. :param doc_file_id: id of the :class:`.DocumentFile`. """ doc_file = DocumentFile.objects.get(id=doc_file_id) ext = os.path.splitext(doc_file.filename)[1].lower() ext2 = get_ext(doc_file.filename) name = "%s.png" % (doc_file_id) thumbnail_path = thumbnailfs.path(name) generated = resize = False thumbnailers = ThumbnailersManager.get_all_thumbnailers(ext)[:] if ext2 != ext: thumbnailers.extend(ThumbnailersManager.get_all_thumbnailers(ext2)) for thumbnailer in thumbnailers: try: resize = thumbnailer(doc_file.file.path, doc_file.filename, thumbnail_path) except Exception, e: # let another thumbnailer do the job if os.path.exists(thumbnail_path): # this file may be corrupted os.remove(thumbnail_path) else: if os.path.exists(thumbnail_path): doc_file.thumbnail = os.path.basename(thumbnail_path) doc_file.no_index = True doc_file.save(update_fields=("thumbnail", )) generated = True break
c1 = c2 c2 = cad.read(1) if c2 == '': # end of file, raises an exception so that the thumbnailer fails raise Exception() cad.seek(-2, 1) done = False while not done: seek() try: im = Image.open(SeekedFile(cad)) except IOError: # not a JPEG, goes forward and looks up for another # magic number cad.seek(3, 1) else: # looks good, save the image im.thumbnail(ThumbnailersManager.THUMBNAIL_SIZE, Image.ANTIALIAS) im.save(output_path) done = True return False CATIA_FILES = ("catpart", "catproduct", "catdrawing") PRO_ENGINEER_FILES = ("asm", "prt") FILES = CATIA_FILES + PRO_ENGINEER_FILES for ext in FILES: ThumbnailersManager.register("." + ext, jfif_thumbnailer)
".uyvy", ".vda", ".vicar", ".vid", ".viff", ".vst", ".wbmp", ".wmf", ".wmv", ".wmz", ".wpg", ".x", ".x3f", ".xbm", ".xc", ".xcf", ".xpm", ".xps", ".xv", ".xwd", ".y", ".ycbcr", ".ycbcra", ".yuv", ) # some formats may not be available, but it is simpler/faster to let # imagemagick fail than checking available format. for ext in FORMATS: ThumbnailersManager.register(ext, magick_thumbnailer)
import Image from base import ThumbnailersManager def pil_thumbnailer(input_path, original_filename, output_path): """ Thumbnailer that uses PIL to generate a thumbnail from an image. """ im = Image.open(input_path) im.thumbnail(ThumbnailersManager.THUMBNAIL_SIZE, Image.ANTIALIAS) im.save(output_path) return False Image.init() for ext, name in Image.EXTENSION.iteritems(): if name in Image.OPEN: ThumbnailersManager.register(ext, pil_thumbnailer)
#: Supported formats (if all ImageMagick decoders are installed) FORMATS = (".3fr", ".a", ".ai", ".art", ".arw", ".avi", ".avs", ".b", ".bgr", ".bgra", ".bmp", ".brg", ".c", ".cal", ".cals", ".caption", ".cin", ".cmyk", ".cmyka", ".cr2", ".crw", ".cur", ".cut", ".dcm", ".dcr", ".dcx", ".dds", ".dfont", ".djvu", ".dng", ".dot", ".dpx", ".epdf", ".epi", ".eps", ".epsf", ".epsi", ".ept", ".ept2", ".ept3", ".erf", ".exr", ".fax", ".fits", ".fractal", ".fts", ".g", ".g3", ".gbr", ".gif", ".gif87", ".gradient", ".gray", ".grb", ".group4", ".hald", ".hrz", ".icb", ".ico", ".icon", ".inline", ".ipl", ".j2c", ".jng", ".jp2", ".jpc", ".jpeg", ".jpg", ".jpx", ".k", ".k25", ".kdc", ".label", ".m", ".m2v", ".m4v", ".map", ".mat", ".miff", ".mng", ".mono", ".mov", ".mp4", ".mpc", ".mpeg", ".mpg", ".mrw", ".msl", ".msvg", ".mtv", ".mvg", ".nef", ".null", ".o", ".orf", ".otb", ".otf", ".pal", ".palm", ".pam", ".pattern", ".pbm", ".pcd", ".pcds", ".pcl", ".pct", ".pcx", ".pdb", ".pdf", ".pdfa", ".pef", ".pes", ".pfa", ".pfb", ".pfm", ".pgm", ".pgx", ".picon", ".pict", ".pix", ".pjpeg", ".plasma", ".png", ".png24", ".png32", ".png8", ".pnm", ".ppm", ".ps", ".psb", ".psd", ".ptif", ".pwp", ".r", ".raf", ".ras", ".rbg", ".rgb", ".rgba", ".rgbo", ".rla", ".rle", ".scr", ".sct", ".sfw", ".sgi", ".sr2", ".srf", ".stegano", ".sun", ".svg", ".svgz", ".text", ".tga", ".tiff", ".tile", ".tim", ".ttc", ".ttf", ".txt", ".uyvy", ".vda", ".vicar", ".vid", ".viff", ".vst", ".wbmp", ".wmf", ".wmv", ".wmz", ".wpg", ".x", ".x3f", ".xbm", ".xc", ".xcf", ".xpm", ".xps", ".xv", ".xwd", ".y", ".ycbcr", ".ycbcra", ".yuv") # some formats may not be available, but it is simpler/faster to let # imagemagick fail than checking available format. for ext in FORMATS: ThumbnailersManager.register(ext, magick_thumbnailer)
" Seek to the possible start of a JPEG file" c1, c2 = cad.read(1), cad.read(2) while c1 + c2 != '\xff\xd8': c1 = c2 c2 = cad.read(1) if c2 == '': # end of file, raises an exception so that the thumbnailer fails raise Exception() cad.seek(-2, 1) done = False while not done: seek() try: im = Image.open(SeekedFile(cad)) except IOError: # not a JPEG, goes forward and looks up for another # magic number cad.seek(3, 1) else: # looks good, save the image im.thumbnail(ThumbnailersManager.THUMBNAIL_SIZE, Image.ANTIALIAS) im.save(output_path) done = True return False CATIA_FILES = ("catpart", "catproduct", "catdrawing") PRO_ENGINEER_FILES = ("asm", "prt") FILES = CATIA_FILES + PRO_ENGINEER_FILES for ext in FILES: ThumbnailersManager.register("." + ext, jfif_thumbnailer)
data = cad.read(1024) # end of file, raises an exception so that the thumbnailer fails if not data: raise Exception() pos = data.find(PNG_MAGIC_NUMBER) cad.seek(t + pos) done = False while not done: seek() try: im = Image.open(SeekedFile(cad)) except IOError: # not a PNG, goes forward and looks up for another # magic number cad.seek(5, 1) else: # looks good, save the image im.thumbnail(ThumbnailersManager.THUMBNAIL_SIZE, Image.ANTIALIAS) im.save(output_path) done = True return False FILES = ( "skp", # google sketch up ) for ext in FILES: ThumbnailersManager.register("." + ext, png_thumbnailer)
pos = data.find(PNG_MAGIC_NUMBER) while pos == -1: data = cad.read(1024) # end of file, raises an exception so that the thumbnailer fails if not data: raise Exception() pos = data.find(PNG_MAGIC_NUMBER) cad.seek(t + pos) done = False while not done: seek() try: im = Image.open(SeekedFile(cad)) except IOError: # not a PNG, goes forward and looks up for another # magic number cad.seek(5, 1) else: # looks good, save the image im.thumbnail(ThumbnailersManager.THUMBNAIL_SIZE, Image.ANTIALIAS) im.save(output_path) done = True return False FILES = ("skp", # google sketch up ) for ext in FILES: ThumbnailersManager.register("." + ext, png_thumbnailer)
""" import re import subprocess from base import ThumbnailersManager def sw_thumbnailer(input_path, original_filename, output_path): """ Thumbnailer that extracts a thumlbnail of a SolidWorks file. """ out = subprocess.Popen(["gsf", "list", input_path], stdout=subprocess.PIPE).communicate()[0] previews = re.findall("[\w/-]*PreviewPNG", out) if not previews: raise ValueError("No preview found") done = False for preview in previews: with open(output_path, "wb") as png_file: args = ["gsf", "cat", input_path, preview] call = subprocess.call(args, stdout=png_file) done = call == 0 if done: break return True #: Supported formats FORMATS = (".sldprt", ".sldasm", ".slddrw", ".slddrt") for ext in FORMATS: ThumbnailersManager.register(ext, sw_thumbnailer)