A program for handling image resizing found that the resized PNG images were significantly different from the original ones. In the original image, a gradient area and partial transparency were lost.
After searching for a long time, it was discovered that the issue originated from the alpha channel.
Now, this piece of code can achieve the exact output of the original PNG.