Python PIL.Image.eval() Examples

The following are 4 code examples of PIL.Image.eval(). You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may also want to check out all available functions/classes of the module PIL.Image , or try the search function .
Example #1
Source File: qrinvite.py    From Fox-V3 with GNU Affero General Public License v3.0 5 votes vote down vote up
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 
Example #2
Source File: images2gif_py3.py    From Legofy with MIT License 4 votes vote down vote up
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 
Example #3
Source File: images2gif_py2.py    From Legofy with MIT License 4 votes vote down vote up
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 
Example #4
Source File: image2gif.py    From 3DSkit with GNU General Public License v3.0 4 votes vote down vote up
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