I use this method to resize png images:
Method for converting PNGs to premultiplied alpha
But this image still loses transparency:
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
from
to
The problem is unrelated to the linked question, instead you have to patch PIL such that it reads the tRNS
PNG chunk correctly. PIL assumes a single value for this chunk, but this image shown has a transparency description for each value in the palette. After that is handled, then it is simple to solve the problem: convert the image to the 'LA'
mode and resize:
import sys
from PIL import Image
img = Image.open(sys.argv[1])
pal = img.getpalette()
width, height = img.size
actual_transp = img.info['actual_transparency'] # XXX This will fail.
result = Image.new('LA', img.size)
im = img.load()
res = result.load()
for x in range(width):
for y in range(height):
t = actual_transp[im[x, y]]
color = pal[im[x, y]]
res[x, y] = (color, t)
result.resize((64, 64), Image.ANTIALIAS).save(sys.argv[2])
So we go from this , to this:
The PIL patch for this specific situation is very simple actually. Open your PIL/PngImagePlugin.py
, go to the function chunk_tRNS
, enter the if
statement that checks for im_mode == "P"
and the subsequent check for i >= 0
, then add the line self.im_info["actual_transparency"] = map(ord, s)
.
This is a more complex function to resize (keeping the transparency): it allows you to use only new width value, both new width and height values or a reference file to get the new size. You can change the resample method too:
## 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')
I have fixed this problem, the nice hack from mmgp is no longer necessary. PIL will now read and apply the transparency properly.
https://github.com/d-schmidt/Pillow/commit/5baa1ac1b8d41fcedce7b12ed1c4a8e87b4851bc
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.