How to combine multiple PNGs into one big PNG file?

后端 未结 9 2049
天涯浪人
天涯浪人 2020-11-28 08:26

I have approx. 6000 PNG files (256*256 pixels) and want to combine them into a big PNG holding all of them programmatically.

What\'s the best/fastest way to do that?

9条回答
  •  予麋鹿
    予麋鹿 (楼主)
    2020-11-28 09:21

    simple python script for joining tiles into one big image:

    import Image
    
    TILESIZE = 256
    ZOOM = 15
    def merge_images( xmin, xmax, ymin, ymax, output) :
        out = Image.new( 'RGB', ((xmax-xmin+1) * TILESIZE, (ymax-ymin+1) * TILESIZE) ) 
    
        imx = 0;
        for x in range(xmin, xmax+1) :
            imy = 0
            for y in range(ymin, ymax+1) :
                tile = Image.open( "%s_%s_%s.png" % (ZOOM, x, y) )
                out.paste( tile, (imx, imy) )
                imy += TILESIZE
            imx += TILESIZE
    
        out.save( output )
    

    run:

    merge_images(18188, 18207, 11097, 11111, "output.png")
    

    works for files named like %ZOOM_%XCORD_%YCORD.png , for example 15_18188_11097.png

提交回复
热议问题