本文整理汇总了Python中PIL.Image.ADAPTIVE属性的典型用法代码示例。如果您正苦于以下问题:Python Image.ADAPTIVE属性的具体用法?Python Image.ADAPTIVE怎么用?Python Image.ADAPTIVE使用的例子?那么恭喜您, 这里精选的属性代码示例或许可以为您提供帮助。您也可以进一步了解该属性所在类PIL.Image
的用法示例。
在下文中一共展示了Image.ADAPTIVE属性的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。
示例1: extract_image
# 需要导入模块: from PIL import Image [as 别名]
# 或者: from PIL.Image import ADAPTIVE [as 别名]
def extract_image(net, image_file):
batch_size = 1
transformer = set_transformer(net)
if image_file.split('.')[-1] == 'gif':
img = Image.open(image_file).convert("P",palette=Image.ADAPTIVE, colors=256)
newfile = ''.join(image_file.split('.')[:-1])+'.png'
for i, frame in enumerate(iter_frames(img)):
frame.save(newfile,**frame.info)
image_file = newfile
img = cv2.imread(image_file)
img = img.astype('float') / 255
net.blobs['data'].data[:] = transformer.preprocess('data', img)
net.forward()
blobs_out_pool5 = net.blobs['pool5'].data[0,:,0,0]
return blobs_out_pool5
示例2: test_trns_l
# 需要导入模块: from PIL import Image [as 别名]
# 或者: from PIL.Image import ADAPTIVE [as 别名]
def test_trns_l(self):
im = hopper('L')
im.info['transparency'] = 128
f = self.tempfile('temp.png')
im_rgb = im.convert('RGB')
self.assertEqual(im_rgb.info['transparency'],
(128, 128, 128)) # undone
im_rgb.save(f)
im_p = im.convert('P')
self.assertIn('transparency', im_p.info)
im_p.save(f)
im_p = self.assert_warning(
UserWarning,
im.convert, 'P', palette=Image.ADAPTIVE)
self.assertNotIn('transparency', im_p.info)
im_p.save(f)
示例3: test_trns_RGB
# 需要导入模块: from PIL import Image [as 别名]
# 或者: from PIL.Image import ADAPTIVE [as 别名]
def test_trns_RGB(self):
im = hopper('RGB')
im.info['transparency'] = im.getpixel((0, 0))
f = self.tempfile('temp.png')
im_l = im.convert('L')
self.assertEqual(im_l.info['transparency'],
im_l.getpixel((0, 0))) # undone
im_l.save(f)
im_p = im.convert('P')
self.assertIn('transparency', im_p.info)
im_p.save(f)
im_rgba = im.convert('RGBA')
self.assertNotIn('transparency', im_rgba.info)
im_rgba.save(f)
im_p = self.assert_warning(
UserWarning,
im.convert, 'P', palette=Image.ADAPTIVE)
self.assertNotIn('transparency', im_p.info)
im_p.save(f)
示例4: to_cbdt_gif
# 需要导入模块: from PIL import Image [as 别名]
# 或者: from PIL.Image import ADAPTIVE [as 别名]
def to_cbdt_gif(self, dst):
font_a_images = read_cbdt(self._font_a.ttfont)
font_b_images = read_cbdt(self._font_b.ttfont)
for element in self._data:
key_before = element["glyph before"]
key_after = element["glyph after"]
image_1 = font_a_images[key_before]
image_1_gif = Image.new('RGBA', image_1.size, (255, 255, 255))
image_1_gif.paste(image_1, image_1)
image_1_gif = image_1_gif.convert('RGB').convert('P', palette=Image.ADAPTIVE)
image_2 = font_b_images[key_after]
image_2_gif = Image.new('RGBA', image_2.size, (255, 255, 255))
image_2_gif.paste(image_2, image_2)
image_2_gif = image_2_gif.convert('RGB').convert('P', palette=Image.ADAPTIVE)
img_path = os.path.join(dst, f"{key_before}.gif")
image_1_gif.save(img_path,
save_all=True,
append_images=[image_2_gif],
duration=1000,
loop=0
)
示例5: get_col_reduced_palette_image
# 需要导入模块: from PIL import Image [as 别名]
# 或者: from PIL.Image import ADAPTIVE [as 别名]
def get_col_reduced_palette_image(img):
"""Returns image reduced to in Excel allowed number of colors."""
cust_col_num_range = (8, 64)
col_cnt = cust_col_num_range[1] - cust_col_num_range[0]
pal_img = img.convert('P', palette=Image.ADAPTIVE, colors=col_cnt)
pal_pixels = pal_img.load()
def add_col_offset(x_pos, y_pos):
"""Add minimum color number to a pixel in palette image."""
pal_pixels[x_pos, y_pos] += cust_col_num_range[0]
map2d(pal_img.size, add_col_offset)
return pal_img
示例6: test_palette_handling
# 需要导入模块: from PIL import Image [as 别名]
# 或者: from PIL.Image import ADAPTIVE [as 别名]
def test_palette_handling(self):
# see https://github.com/python-pillow/Pillow/issues/513
im = Image.open(TEST_GIF)
im = im.convert('RGB')
im = im.resize((100, 100), Image.LANCZOS)
im2 = im.convert('P', palette=Image.ADAPTIVE, colors=256)
f = self.tempfile('temp.gif')
im2.save(f, optimize=True)
reloaded = Image.open(f)
self.assert_image_similar(im, reloaded.convert('RGB'), 10)
示例7: _convert2pilp
# 需要导入模块: from PIL import Image [as 别名]
# 或者: from PIL.Image import ADAPTIVE [as 别名]
def _convert2pilp(im):
Image = _getImage()
return im.convert("P", dither=Image.NONE, palette=Image.ADAPTIVE)
示例8: drawToPILP
# 需要导入模块: from PIL import Image [as 别名]
# 或者: from PIL.Image import ADAPTIVE [as 别名]
def drawToPILP(d, dpi=72, bg=0xffffff, configPIL=None, showBoundary=rl_config._unset_):
Image = _getImage()
im = drawToPIL(d, dpi=dpi, bg=bg, configPIL=configPIL, showBoundary=showBoundary)
return im.convert("P", dither=Image.NONE, palette=Image.ADAPTIVE)
示例9: save_raster_png
# 需要导入模块: from PIL import Image [as 别名]
# 或者: from PIL.Image import ADAPTIVE [as 别名]
def save_raster_png(self, data, filename):
if 'semantics' in filename:
from util.util import labels_to_colors
image_save = Image.fromarray(np.squeeze(labels_to_colors(data, self.opt.color_palette).astype(np.int8)), mode='RGB').convert('P', palette=Image.ADAPTIVE, colors=256)
image_save.save(filename)
示例10: convert_webp_to_png
# 需要导入模块: from PIL import Image [as 别名]
# 或者: from PIL.Image import ADAPTIVE [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
示例11: reduce_colors
# 需要导入模块: from PIL import Image [as 别名]
# 或者: from PIL.Image import ADAPTIVE [as 别名]
def reduce_colors (img_bgr, n_colors):
img_rgb = (img_bgr[...,::-1] * 255.0).astype(np.uint8)
img_rgb_pil = Image.fromarray(img_rgb)
img_rgb_pil_p = img_rgb_pil.convert('P', palette=Image.ADAPTIVE, colors=n_colors)
img_rgb_p = img_rgb_pil_p.convert('RGB')
img_bgr = cv2.cvtColor( np.array(img_rgb_p, dtype=np.float32) / 255.0, cv2.COLOR_RGB2BGR )
return img_bgr
示例12: convertImagesToPIL
# 需要导入模块: from PIL import Image [as 别名]
# 或者: from PIL.Image import ADAPTIVE [as 别名]
def convertImagesToPIL(self, images, dither, nq=0):
""" 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')
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())
else:
im = nqInstance.quantize(im) # Use to quantize the image itself
images2.append(im)
else:
# Adaptive PIL algorithm
AD = Image.ADAPTIVE
for im in images:
im = im.convert('P', palette=AD, dither=dither)
images2.append(im)
# Done
return images2
示例13: convertImagesToPIL
# 需要导入模块: from PIL import Image [as 别名]
# 或者: from PIL.Image import ADAPTIVE [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
示例14: writeGif
# 需要导入模块: from PIL import Image [as 别名]
# 或者: from PIL.Image import ADAPTIVE [as 别名]
def writeGif(filename, images, duration=0.1, loops=0, dither=1):
""" writeGif(filename, images, duration=0.1, loops=0, dither=1)
Write an animated gif from the specified images.
images should be a list of numpy arrays of PIL images.
Numpy images of type float should have pixels between 0 and 1.
Numpy images of other types are expected to have values between 0 and 255.
"""
if PIL is None:
raise RuntimeError("Need PIL to write animated gif files.")
AD = Image.ADAPTIVE
images2 = []
# convert to PIL
for im in images:
if isinstance(im,Image.Image):
images2.append( im.convert('P', palette=AD, dither=dither) )
elif np and isinstance(im, np.ndarray):
if im.dtype == np.uint8:
pass
elif im.dtype in [np.float32, np.float64]:
im = (im*255).astype(np.uint8)
else:
im = im.astype(np.uint8)
# convert
if len(im.shape)==3 and im.shape[2]==3:
im = Image.fromarray(im,'RGB').convert('P', palette=AD, dither=dither)
elif len(im.shape)==2:
im = Image.fromarray(im,'L').convert('P', palette=AD, dither=dither)
else:
raise ValueError("Array has invalid shape to be an image.")
images2.append(im)
else:
raise ValueError("Unknown image type.")
# check duration
if hasattr(duration, '__len__'):
if len(duration) == len(images2):
durations = [d for d in duration]
else:
raise ValueError("len(duration) doesn't match amount of images.")
else:
durations = [duration for im in images2]
# open file
fp = open(filename, 'wb')
# write
try:
n = _writeGifToFile(fp, images2, durations, loops)
print n, 'frames written'
finally:
fp.close()
示例15: do_reduce_colors
# 需要导入模块: from PIL import Image [as 别名]
# 或者: from PIL.Image import ADAPTIVE [as 别名]
def do_reduce_colors(img: ImageType, max_colors: int) -> Tuple[ImageType, int, int]:
""" Reduce the number of colors of an Image object
It takes a PIL image object and tries to reduce the total number of colors,
converting it to an indexed color (mode P) image. If the input image is in
mode 1, it cannot be further reduced, so it's returned back with no
changes.
:param img: a PIL image in color (modes P, RGBA, RGB, CMYK, YCbCr, LAB or HSV)
:param max_colors: an integer indicating the maximum number of colors allowed.
:return: a PIL image in mode P (or mode 1, as stated above), an integer
indicating the original number of colors (0 if source is not a
mode P or mode 1 image) and an integer stating the resulting
number of colors.
"""
orig_mode = img.mode
if orig_mode == "1":
return img, 2, 2
colors = img.getcolors()
if colors:
orig_colors = len(colors)
else:
orig_colors = 0
# Intermediate conversion steps when needed
if orig_mode in ["CMYK", "YCbCr", "LAB", "HSV"]:
img = img.convert("RGB")
elif orig_mode == "LA":
img = img.convert("RGBA")
# Actual color reduction happening here
if orig_mode in ["RGB", "L"]:
palette = Image.ADAPTIVE
elif orig_mode == "RGBA":
palette = Image.ADAPTIVE
transparent = Image.new("RGBA", img.size, (0, 0, 0, 0))
# blend with transparent image using own alpha
img = Image.composite(img, transparent, img)
elif orig_mode == "P":
palette = img.getpalette()
img = img.convert("RGBA")
w, h = img.size
alpha_layer = Image.new("L", img.size)
for x in range(w):
for y in range(h):
r, g, b, a = img.getpixel((x, y))
alpha_layer.putpixel((x, y), a)
img.putalpha(alpha_layer)
else:
return img, 0, 0
img = img.convert("P", palette=palette, colors=max_colors)
return img, orig_colors, len(img.getcolors())