本文整理汇总了Python中PIL.Image.eval方法的典型用法代码示例。如果您正苦于以下问题:Python Image.eval方法的具体用法?Python Image.eval怎么用?Python Image.eval使用的例子?那么, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类PIL.Image
的用法示例。
在下文中一共展示了Image.eval方法的3个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。
示例1: convert_webp_to_png
# 需要导入模块: from PIL import Image [as 别名]
# 或者: from PIL.Image import eval [as 别名]
def convert_webp_to_png(path):
im = Image.open(path)
im.load()
alpha = im.split()[-1]
im = im.convert("RGB").convert("P", palette=Image.ADAPTIVE, colors=255)
mask = Image.eval(alpha, lambda a: 255 if a <= 128 else 0)
im.paste(255, mask)
new_path = path.replace(".webp", ".png")
im.save(new_path, transparency=255)
return new_path
示例2: convertImagesToPIL
# 需要导入模块: from PIL import Image [as 别名]
# 或者: from PIL.Image import eval [as 别名]
def convertImagesToPIL(self, images, dither, nq=0,images_info=None):
""" convertImagesToPIL(images, nq=0)
Convert images to Paletted PIL images, which can then be
written to a single animaged GIF.
"""
# Convert to PIL images
images2 = []
for im in images:
if isinstance(im, Image.Image):
images2.append(im)
elif np and isinstance(im, np.ndarray):
if im.ndim==3 and im.shape[2]==3:
im = Image.fromarray(im,'RGB')
elif im.ndim==3 and im.shape[2]==4:
# im = Image.fromarray(im[:,:,:3],'RGB')
self.transparency = True
im = Image.fromarray(im[:,:,:4],'RGBA')
elif im.ndim==2:
im = Image.fromarray(im,'L')
images2.append(im)
# Convert to paletted PIL images
images, images2 = images2, []
if nq >= 1:
# NeuQuant algorithm
for im in images:
im = im.convert("RGBA") # NQ assumes RGBA
nqInstance = NeuQuant(im, int(nq)) # Learn colors from image
if dither:
im = im.convert("RGB").quantize(palette=nqInstance.paletteImage(),colors=255)
else:
im = nqInstance.quantize(im,colors=255) # Use to quantize the image itself
self.transparency = True # since NQ assumes transparency
if self.transparency:
alpha = im.split()[3]
mask = Image.eval(alpha, lambda a: 255 if a <=128 else 0)
im.paste(255,mask=mask)
images2.append(im)
else:
# Adaptive PIL algorithm
AD = Image.ADAPTIVE
# for index,im in enumerate(images):
for i in range(len(images)):
im = images[i].convert('RGB').convert('P', palette=AD, dither=dither,colors=255)
if self.transparency:
alpha = images[i].split()[3]
mask = Image.eval(alpha, lambda a: 255 if a <=128 else 0)
im.paste(255,mask=mask)
images2.append(im)
# Done
return images2
示例3: convertImagesToPIL
# 需要导入模块: from PIL import Image [as 别名]
# 或者: from PIL.Image import eval [as 别名]
def convertImagesToPIL(self, images, dither, nq=0, images_info=None):
""" convertImagesToPIL(images, nq=0)
Convert images to Paletted PIL images, which can then be
written to a single animaged GIF.
"""
# Convert to PIL images
images2 = []
for im in images:
if isinstance(im, Image.Image):
images2.append(im)
elif np and isinstance(im, np.ndarray):
if im.ndim == 3 and im.shape[2] == 3:
im = Image.fromarray(im, 'RGB')
elif im.ndim == 3 and im.shape[2] == 4:
# im = Image.fromarray(im[:,:,:3],'RGB')
self.transparency = True
im = Image.fromarray(im[:, :, :4], 'RGBA')
elif im.ndim == 2:
im = Image.fromarray(im, 'L')
images2.append(im)
# Convert to paletted PIL images
images, images2 = images2, []
if nq >= 1:
# NeuQuant algorithm
for im in images:
im = im.convert("RGBA") # NQ assumes RGBA
nqInstance = NeuQuant(im, int(nq)) # Learn colors from image
if dither:
im = im.convert("RGB").quantize(palette=nqInstance.paletteImage(), colors=255)
else:
im = nqInstance.quantize(im, colors=255) # Use to quantize the image itself
self.transparency = True # since NQ assumes transparency
if self.transparency:
alpha = im.split()[3]
mask = Image.eval(alpha, lambda a: 255 if a <= 128 else 0)
im.paste(255, mask=mask)
images2.append(im)
else:
# Adaptive PIL algorithm
AD = Image.ADAPTIVE
# for index,im in enumerate(images):
for i in range(len(images)):
im = images[i].convert('RGB').convert('P', palette=AD, dither=dither, colors=255)
if self.transparency:
alpha = images[i].split()[3]
mask = Image.eval(alpha, lambda a: 255 if a <= 128 else 0)
im.paste(255, mask=mask)
images2.append(im)
# Done
return images2