Scale images with PIL preserving transparency and color?

前端 未结 4 1716
花落未央
花落未央 2021-02-08 07:27

Say you want to scale a transparent image but do not yet know the color(s) of the background you will composite it onto later. Unfortunately PIL seems to incorporate the color v

4条回答
  •  悲哀的现实
    2021-02-08 07:53

    sorry for answering myself but this is the only working solution that I know of. It sets the color values of fully transparent pixels to the average of the surrounding non fully transparent pixels to minimize impact of fully transparent pixel colors while resizing. There are special cases where the proper result will not be achieved.

    It is very ugly and slow. I'd be happy to accept your answer if you can come up with something better.

    # might be possible to speed this up by only processing necessary pixels
    #  using scipy dilate, numpy where
    
    import PIL.Image
    
    filename = "trans.png"  # http://qrc-designer.com/stuff/trans.png
    size = (25,25)
    
    import numpy as np
    
    im = PIL.Image.open(filename)
    
    npImRgba = np.asarray(im, dtype=np.uint8)
    npImRgba2 = np.asarray(im, dtype=np.uint8)
    npImRgba2.flags.writeable = True
    lenY = npImRgba.shape[0]
    lenX = npImRgba.shape[1]
    for y in range(npImRgba.shape[0]):
        for x in range(npImRgba.shape[1]):
            if npImRgba[y, x, 3] != 0:  # only change completely transparent pixels
                continue        
            colSum = np.zeros((3), dtype=np.uint16)
            i = 0
            for oy in [-1, 0, 1]:
                for ox in [-1, 0, 1]:
                    if not oy and not ox:
                        continue
                    iy = y + oy
                    if iy < 0:
                        continue
                    if iy >= lenY:
                        continue
                    ix = x + ox
                    if ix < 0:
                        continue
                    if ix >= lenX:
                        continue
                    col = npImRgba[iy, ix]
                    if not col[3]:
                        continue
                    colSum += col[:3]
                    i += 1
            npImRgba2[y, x, :3] = colSum / i
    
    im = PIL.Image.fromarray(npImRgba2)
    im = im.transform(size, PIL.Image.EXTENT, (0,0) + im.size, PIL.Image.LINEAR)
    im.save("slime_"+filename)
    

    result: enter image description here

提交回复
热议问题