Beispiel #1
0
def get_thumbnail_image(filename, size='normal', autoSave = True):
    source_image_name = filename
    thumbnail_image_name = get_thumbnail_image_name(filename,size)
    thumbnail_size = get_thumbnail_image_size(size)
    thumbnail_crop = get_thumbnail_image_crop(size)
    # 检查缩略图是否存在 
    storage = get_storage()
    thumbs_storage = get_thumbs_storage()

    exists = thumbs_storage.exists(thumbnail_image_name)
    if exists:
        #print "storage exists file~~",thumbnail_image_name
        return thumbnail_image_name

    try:
        exists = storage.exists(source_image_name)
        if not exists:
            print "storage not exists source file >_< ",source_image_name
            return thumbnail_image_name
        source_file = storage.open(filename)
        # 实例PIl对象
        im = Image.open(source_file.file)

        format = Image.EXTENSION.get(thumbnail_image_name, 'PNG')
        if im.info.has_key("duration") and 0:
            # 动态gif图
            original_duration = im.info['duration']
            #frames = [frame.copy() for i, frame in enumerate(iter_frames(im))]    
            #frames = [scale_and_crop(frame, thumbnail_size) for i, frame in enumerate(iter_frames(im))]  
            frames = []
            for i, frame in enumerate(iter_frames(im)):
                frame = colorspace(frame)
                frame = autocrop(frame)
                frame = scale_and_crop(frame, thumbnail_size)
                frames.append(frame)
            #frames.reverse()
            i=0
            for frame in frames:
                i+=1
                #frame.thumbnail((72,72))
            output = StringIO()
            img_data = images2gif.writeGif(output, frames, duration=original_duration/1000.0, dither=0)
            thumbnail_file = thumbs_storage.save(thumbnail_image_name,img_data)
        else:
            im = colorspace(im)
            im = autocrop(im)
            im = scale_and_crop(im, thumbnail_size, thumbnail_crop)
            output = StringIO()
            im.save(output, format)

            is_transparent = is_transparent_image(im)
            if is_transparent:
                img_data = output.getvalue()
            else:
                img_data = im.tostring('jpeg', 'RGB')
            output.close()
            thumbnail_file = thumbs_storage.save(thumbnail_image_name,img_data)

    except Exception, e:
        raise
Beispiel #2
0
 def writeGifToFile(self, fp, images, durations, loops, xys, disposes):
     """ writeGifToFile(fp, images, durations, loops, xys, disposes)
     
     Given a set of images writes the bytes to the specified stream.
     
     """
     
     # Obtain palette for all images and count each occurance
     palettes, occur = [], []
     for im in images:
         palettes.append( getheader(im)[1] )
     for palette in palettes:
         occur.append( palettes.count( palette ) )
     
     # Select most-used palette as the global one (or first in case no max)
     globalPalette = palettes[ occur.index(max(occur)) ]
     
     # Init
     frames = 0
     firstFrame = True
     
     
     for im, palette in zip(images, palettes):
     
         if firstFrame:
             # Write header
     
             # Gather info
             header = self.getheaderAnim(im)
             appext = self.getAppExt(loops)
     
             # Write
             fp.write(header)
             fp.write(globalPalette)
             fp.write(appext)
     
             # Next frame is not the first
             firstFrame = False
     
         if True:
             # Write palette and image data
     
             # Gather info
             data = getdata(im)
             imdes, data = data[0], data[1:]
             graphext = self.getGraphicsControlExt(durations[frames],
                                                     disposes[frames])
             # Make image descriptor suitable for using 256 local color palette
             lid = self.getImageDescriptor(im, xys[frames])
     
             # Write local header
             if (palette != globalPalette) or (disposes[frames] != 2):
                 # Use local color palette
                 fp.write(graphext)
                 fp.write(lid) # write suitable image descriptor
                 fp.write(palette) # write local color table
                 fp.write('\x08') # LZW minimum size code
             else:
                 # Use global color palette
                 fp.write(graphext)
                 fp.write(imdes) # write suitable image descriptor
     
             # Write image data
             for d in data:
                 fp.write(d)
     
         # Prepare for next round
         frames = frames + 1
     img_data = fp.getvalue()
     return img_data
     
     return frames
     storage = get_storage()
     img_data = fp.getvalue()
     thumbnail_file = storage.save("1.gif",img_data)
     fp.write(";")  # end gif
     print fp
     print type(fp)