Return to Snippet

Revision: 48822
at July 11, 2011 05:47 by miohtama


Initial Code
def process_floor_image(infile, outfile, copy_pics):
    """
    Copy the orignal image and make also 1024 x 1024 sliced tiles out of it. 
        
    Because our green robot friend sucks.
    
    http://stackoverflow.com/questions/6632140/android-pixel-quality-reduction-in-images-loaded-in-webview    
    
    @return Image size in [width, height]
    """
    
    
    im = Image.open(infile)
    
    w = im.size[0]
    h = im.size[1]
    dimensions = [ w, h ]
    
    if copy_pics:
        
        # Copy master
        path = os.path.dirname(outfile)
        base = os.path.basename(outfile)
        
        try:
            os.makedirs(path)
        except OSError:
            pass
        
        print "Copying image:" + outfile
        shutil.copyfile(infile, outfile)
                    
        # slice the image to 1000 x 1000 tiles
        slice_size = 1000
        for y in range(0, h, slice_size):
            for x in range(0, w, slice_size):
                fname = os.path.join(path, "tile-%d-%d-%s" % (x, y, base)) 
                print "Creating tile:" + fname
                
                mx = min(x+slice_size, w)
                my = min(y+slice_size, h)
                
                buffer = Image.new("RGB", [slice_size, slice_size], (255, 255, 255))                                
                tile = im.crop((x, y, mx, my))
                buffer.paste(tile, (0, 0))                                
                buffer.save(fname, "PNG")
        
    return dimensions

Initial URL


Initial Description
Create smaller tiles out of a larger bitmap image

Initial Title
Slicing images with Python and PIL

Initial Tags
image

Initial Language
Python