gpt4 book ai didi

python - 使用 PIL 调整 png 图像大小会失去透明度

转载 作者:太空狗 更新时间:2023-10-30 00:56:49 26 4
gpt4 key购买 nike

我使用这种方法调整 png 图像的大小:

Method for converting PNGs to premultiplied alpha

但是这张图还是失去了透明度:

import Image, numpy

def resize(filename, img,height, width):
if filename.endswith(".png"):
img = img.convert('RGBA')
premult = numpy.fromstring(img.tostring(), dtype=numpy.uint8)
alphaLayer = premult[3::4] / 255.0
premult[::4] *= alphaLayer
premult[1::4] *= alphaLayer
premult[2::4] *= alphaLayer
img = Image.fromstring("RGBA", img.size, premult.tostring())
img = img.resize((height,width), Image.ANTIALIAS)
return img

来自

http://i.stack.imgur.com/oPV5q.pnghttp://i.stack.imgur.com/jzmT9.png

最佳答案

这是一个更复杂的调整大小(保持透明度)的函数:它允许您仅使用新的宽度值、新的宽度和高度值或引用文件来获得新的大小。您也可以更改重采样方法:

## PngResizeTransparency.py
#
## Resize PNG image by keeping the transparency
## thanks to https://stackoverflow.com/users/1453719/nicolas-barbey
#
## Use:
## - using a reference file to get new sizes:
# PNG_ResizeKeepTransparency(SourceFile, ResizedFile, RefFile ='YourRefFile.png')
## - using only the resized width:
# PNG_ResizeKeepTransparency(SourceFile, ResizedFile, new_width)
## - using resized width and hight:
# PNG_ResizeKeepTransparency(SourceFile, ResizedFile, new_width, new_height)
## - using resample mode: add param resample="NEAREST"/"BILINEAR"/"BICUBIC"/"ANTIALIAS"

from PIL import Image

def PNG_ResizeKeepTransparency(SourceFile, ResizedFile, new_width=0, new_height=0, resample="ANTIALIAS", RefFile =''):
# needs PIL
# Inputs:
# - SourceFile = initial PNG file (including the path)
# - ResizedFile = resized PNG file (including the path)
# - new_width = resized width in pixels; if you need % plz include it here: [your%] *initial width
# - new_height = resized hight in pixels ; default = 0 = it will be calculated using new_width
# - resample = "NEAREST", "BILINEAR", "BICUBIC" and "ANTIALIAS"; default = "ANTIALIAS"
# - RefFile = reference file to get the size for resize; default = ''

img = Image.open(SourceFile) # open PNG image path and name
img = img.convert("RGBA") # convert to RGBA channels
width, height = img.size # get initial size

# if there is a reference file to get the new size
if RefFile != '':
imgRef = Image.open(RefFile)
new_width, new_height = imgRef.size
else:
# if we use only the new_width to resize in proportion the new_height
# if you want % of resize please use it into new_width (?% * initial width)
if new_height == 0:
new_height = new_width*width/height

# split image by channels (bands) and resize by channels
img.load()
bands = img.split()
# resample mode
if resample=="NEAREST":
resample = Image.NEAREST
else:
if resample=="BILINEAR":
resample = Image.BILINEAR
else:
if resample=="BICUBIC":
resample = Image.BICUBIC
else:
if resample=="ANTIALIAS":
resample = Image.ANTIALIAS
bands = [b.resize((new_width, new_height), resample) for b in bands]
# merge the channels after individual resize
img = Image.merge('RGBA', bands)
# save the image
img.save(ResizedFile)
return

#######################################################

if __name__ == "__main__":
sFile = './autumn-png-leaf.png'
# resize using new width value (new height is calculated by keeping image aspect)
PNG_ResizeKeepTransparency(sFile, sFile[:-4]+'_resized.png', 400)
# resize using a reference file to get the new image dimension
PNG_ResizeKeepTransparency(sFile, sFile[:-4]+'_resized.png', RefFile = 'autumn-png-leaf_starry-night-van-gogh_fchollet_10.png')

关于python - 使用 PIL 调整 png 图像大小会失去透明度,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/14634014/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com