我们从Python开源项目中,提取了以下27个代码示例,用于说明如何使用PIL.Image.blend()。
def drawSimpleSegment(self): #Drawing module im_Width, im_Height = self.pred_size prediction_image = Image.new("RGB", (im_Width, im_Height) ,(0,0,0)) prediction_imageDraw = ImageDraw.Draw(prediction_image) #BASE all image segmentation for i in range(im_Width): for j in range(im_Height): #get matrix element class(0-149) px_Class = self.predicted_classes[j][i] #assign color from .mat list put_Px_Color = tuple(self.class_colors['colors'][px_Class]) #drawing prediction_imageDraw.point((i,j), fill=put_Px_Color) #Resize to original size and save self.coef, self.h_pad, self.w_pad = self.calculateResize() FullHdOutImage = self.resizeToOutput(prediction_image, self.coef, self.h_pad, self.w_pad) FullHdOutImage = Image.blend(FullHdOutImage, self.im, 0.5) return FullHdOutImage
def enhance(self, factor): """ Returns an enhanced image. :param factor: A floating point value controlling the enhancement. Factor 1.0 always returns a copy of the original image, lower factors mean less color (brightness, contrast, etc), and higher values more. There are no restrictions on this value. :rtype: :py:class:`~PIL.Image.Image` """ return Image.blend(self.degenerate, self.image, factor)
def blend(image1, image2, alpha): """Blend images using constant transparency weight. Alias for :py:meth:`PIL.Image.Image.blend`. :rtype: :py:class:`~PIL.Image.Image` """ return Image.blend(image1, image2, alpha)
def do_blend(self): """usage: blend <image:pic1> <image:pic2> <float:alpha> Replace two images and an alpha with the blended image. """ image1 = self.do_pop() image2 = self.do_pop() alpha = float(self.do_pop()) self.push(Image.blend(image1, image2, alpha))
def make_img_overlay(img, predicted_img): w = img.shape[0] h = img.shape[1] color_mask = np.zeros((w, h, 3), dtype=np.uint8) color_mask[:,:,0] = predicted_img*PIXEL_DEPTH img8 = img_float_to_uint8(img) background = Image.fromarray(img8, 'RGB').convert("RGBA") overlay = Image.fromarray(color_mask, 'RGB').convert("RGBA") new_img = Image.blend(background, overlay, 0.2) return new_img ## Execution
def manipulate_frame(self, frame_image, faces, index): fieri = Image.open(self.__class__.get_os_path('overlays/fieri.png')) original_size = frame_image.size if (index == 0): self.ex = int(((random.random() * 0.6) + 0.2) * frame_image.size[0]) self.ey = int(((random.random() * 0.6) + 0.2) * frame_image.size[1]) self.fieri_width = frame_image.size[0] * self.initial_size else: scale = self.scale_factor * index self.fieri_width = frame_image.size[0] * self.initial_size * scale frame_image = frame_image.resize((int(original_size[0]*scale), int(original_size[1]*scale)), Image.BICUBIC) ( rex, rey ) = ( self.ex*scale, self.ey*scale ) crop_coords = ( int(rex - original_size[0]/2), int(rey - original_size[1]/2), int(rex + original_size[0]/2), int(rey + original_size[1]/2) ); frame_image = frame_image.crop( crop_coords ).copy() re_width = max(int(self.fieri_width ), 1) re_height = max(int(self.fieri_width * 1.2), 1) fieri = fieri.resize((re_width, re_height), Image.BICUBIC) base = frame_image.copy() if (index > 0): frame_image.paste(fieri, ( int(frame_image.size[0]/2 - re_width/2), int(frame_image.size[1]/2- re_height/2) ), fieri) else: frame_image.paste(fieri, ( int(self.ex - re_width/2), int(self.ey - re_height/2) ), fieri) return Image.blend(base, frame_image, float(index+1) / float(self.total_frames))
def get_tile(char, fg_color, bg_color, opaque): ch_x = char % 0x10 ch_y = int(char / 0x10) tile_bg = Image.new("RGBA", (8, 14), color="#" + BG_COLORS[bg_color % 0x8]) tile_fg = FG_GRAPHICS[fg_color].crop((ch_x * 8, ch_y * 14, ch_x * 8 + 8, ch_y * 14 + 14)) result = Image.alpha_composite(tile_bg, tile_fg) if not opaque: result = Image.blend(tile_bg, result, 0.7) return result
def overlayImage(img1,img2,alpha): try: return Image.blend(img1, img2, float(alpha)/100) except ValueError: return Image.blend(img1.convert('RGB'), img2.convert('RGB'), float(alpha)/100)
def _create_ship_image(self, score, avatar1, avatar2): ava_im1 = Image.open(avatar1).convert('RGBA') ava_im2 = Image.open(avatar2).convert('RGBA') # Assume the two images are square size = min(ava_im1.size, ava_im2.size) offset = round(_scale(0, 100, size[0], 0, score)) ava_im1.thumbnail(size) ava_im2.thumbnail(size) # paste img1 on top of img2 newimg1 = Image.new('RGBA', size=size, color=(0, 0, 0, 0)) newimg1.paste(ava_im2, (-offset, 0)) newimg1.paste(ava_im1, (offset, 0)) # paste img2 on top of img1 newimg2 = Image.new('RGBA', size=size, color=(0, 0, 0, 0)) newimg2.paste(ava_im1, (offset, 0)) newimg2.paste(ava_im2, (-offset, 0)) # blend with alpha=0.5 im = Image.blend(newimg1, newimg2, alpha=0.6) mask = Image.open(self._mask).convert('L') mask = mask.resize(ava_im1.size, resample=Image.BILINEAR) im.putalpha(mask) f = io.BytesIO() im.save(f, 'png') f.seek(0) return discord.File(f, filename='test.png')
def make_img_overlay(img, predicted_img, true_img=None): w = img.shape[0] h = img.shape[1] color_mask = np.zeros((w, h, 3), dtype=np.uint8) color_mask[:, :, 0] = predicted_img * PIXEL_DEPTH if true_img is None: color_mask[:, :, 1] = true_img * PIXEL_DEPTH img8 = img_float_to_uint8(img) background = Image.fromarray(img8, 'RGB').convert("RGBA") overlay = Image.fromarray(color_mask, 'RGB').convert("RGBA") new_img = Image.blend(background, overlay, 0.2) return new_img
def image(self): r""" Tuple with a CAPTCHA text and a Image object. Images are generated on the fly, using given text source, TTF font and other parameters passable through __init__. All letters in used text are morphed. Also a line is morphed and pased onto CAPTCHA text. Additionaly, if self.noise > 1/255, a "snowy" image is merged with CAPTCHA image with a 50/50 ratio. Property returns a pair containing a string with text in returned image and image itself. :returns: ``tuple`` (CAPTCHA text, Image object) """ text = self.text w, h = self.font.getsize(text) margin_x = round(self.margin_x * w / self.w) margin_y = round(self.margin_y * h / self.h) image = Image.new('RGB', (w + 2*margin_x, h + 2*margin_y), (255, 255, 255)) # Text self._writeText(image, text, pos=(margin_x, margin_y)) # Line self._drawLine(image) # White noise noise = self._whiteNoise(image.size) if noise is not None: image = Image.blend(image, noise, 0.5) # Resize image = image.resize(self.size, resample=self.resample) return (text, image)
def _create_ship_image(self, score, avatar1, avatar2): ava_im1 = Image.open(avatar1).convert('RGBA') ava_im2 = Image.open(avatar2).convert('RGBA') size = min(ava_im1.size, ava_im2.size) offset = round(_scale(0, 100, size[0], 0, score)) ava_im1.thumbnail(size) ava_im2.thumbnail(size) newimg1 = Image.new('RGBA', size=size, color=(0, 0, 0, 0)) newimg1.paste(ava_im2, (-offset, 0)) newimg1.paste(ava_im1, (offset, 0)) newimg2 = Image.new('RGBA', size=size, color=(0, 0, 0, 0)) newimg2.paste(ava_im1, (offset, 0)) newimg2.paste(ava_im2, (-offset, 0)) im = Image.blend(newimg1, newimg2, alpha=0.6) mask = Image.open(self._mask).convert('L') mask = mask.resize(ava_im1.size, resample=Image.BILINEAR) im.putalpha(mask) f = io.BytesIO() im.save(f, 'png') f.seek(0) return discord.File(f, filename='test.png')
def make_img_overlay(img, predicted_img): """ Draw red patches on the satellite image. @param img : The original image. @param predicted_img : The label. """ w = img.shape[0] h = img.shape[1] color_mask = np.zeros((w, h, 3), dtype=np.uint8) color_mask[:, :, 0] = predicted_img * PIXEL_DEPTH img8 = img_float_to_uint8(img) background = Image.fromarray(img8, 'RGB').convert("RGBA") overlay = Image.fromarray(color_mask, 'RGB').convert("RGBA") new_img = Image.blend(background, overlay, 0.2) return new_img
def main(): try: opts, args = getopt.getopt(sys.argv[1:], "hx:y:a:") except getopt.GetoptError as err: print(err) usage() sys.exit(2) if ("-h", "") in opts: usage() sys.exit(0) try: infile = args[0] im = Image.open(infile) print(im.size, im.mode) except IndexError as err: print(err) usage() sys.exit(-1) except IOError as err: print(err) print("Error: cannot open file") sys.exit(-1) im = im.convert("RGBA") width = 64 height = 64 alpha = 0.5 for o, a in opts: if o == "-x": width = int(a) if o == "-y": height = int(a) elif o == "-a": alpha = float(a) if not (0 < width <= 256 and 0 < height <= 256 and 0.0 <= alpha <= 1.0): print("Error: out of range") sys.exit(-1) print("resizing...") sz = min(width, height) pixel = im.resize((sz, sz), Image.LANCZOS) im = im.resize((width, height), Image.LANCZOS) layer0 = im.resize((width * sz, height * sz), Image.NEAREST) layer1 = Image.new(im.mode, layer0.size) for x in range(width): for y in range(height): layer1.paste(pixel, (x * sz, y * sz)) print("merging...") output = Image.blend(layer0, layer1, alpha) outfile = os.path.splitext(infile)[0] + "_picpic.png" output.save(outfile, "PNG") print("done.")
def vis_seg(img_names, cls_names, output_dir, gt_dir): """ This function plot segmentation results to specific directory Args: img_names: list """ assert os.path.exists(output_dir) # a list of dictionary inst_dir = os.path.join(output_dir, 'SegInst') cls_dir = os.path.join(output_dir, 'SegCls') res_dir = os.path.join(output_dir, 'SegRes') if not os.path.isdir(inst_dir): os.mkdir(inst_dir) if not os.path.isdir(cls_dir): os.mkdir(cls_dir) if not os.path.isdir(res_dir): os.mkdir(res_dir) res_list = _prepare_dict(img_names, cls_names, output_dir) for img_ind, image_name in enumerate(img_names): target_inst_file = os.path.join(inst_dir, image_name + '.jpg') target_cls_file = os.path.join(cls_dir, image_name + '.jpg') print image_name gt_image = gt_dir + '/img/' + image_name + '.jpg' img_data = cv2.imread(gt_image) img_width = img_data.shape[1] img_height = img_data.shape[0] pred_dict = res_list[img_ind] inst_img, cls_img = _convert_pred_to_image(img_width, img_height, pred_dict) color_map = _get_voc_color_map() inst_out_img = np.zeros((img_height, img_width, 3)) cls_out_img = np.zeros((img_height, img_width, 3)) for i in xrange(img_height): for j in xrange(img_width): inst_out_img[i][j] = color_map[inst_img[i][j]][::-1] cls_out_img[i][j] = color_map[cls_img[i][j]][::-1] cv2.imwrite(target_inst_file, inst_out_img) cv2.imwrite(target_cls_file, cls_out_img) background = Image.open(gt_image) mask = Image.open(target_cls_file) background = background.convert('RGBA') mask = mask.convert('RGBA') superimpose_image = Image.blend(background, mask, 0.8) name = os.path.join(res_dir, image_name + '.png') superimpose_image.save(name, 'PNG')