我们从Python开源项目中,提取了以下17个代码示例,用于说明如何使用PIL.ImageOps.expand()。
def pad_to_target(img, target_height, target_width, label=0): # Pad image with zeros to the specified height and width if needed # This op does nothing if the image already has size bigger than target_height and target_width. w, h = img.size left = top = right = bottom = 0 doit = False if target_width > w: delta = target_width - w left = delta // 2 right = delta - left doit = True if target_height > h: delta = target_height - h top = delta // 2 bottom = delta - top doit = True if doit: img = ImageOps.expand(img, border=(left, top, right, bottom), fill=label) assert img.size[0] >= target_width assert img.size[1] >= target_height return img
def __call__(self, img, mask): if self.padding > 0: img = ImageOps.expand(img, border=self.padding, fill=0) mask = ImageOps.expand(mask, border=self.padding, fill=0) assert img.size == mask.size w, h = img.size th, tw = self.size if w == tw and h == th: return img, mask if w < tw or h < th: return img.resize((tw, th), Image.BILINEAR), mask.resize((tw, th), Image.NEAREST) x1 = random.randint(0, w - tw) y1 = random.randint(0, h - th) return img.crop((x1, y1, x1 + tw, y1 + th)), mask.crop((x1, y1, x1 + tw, y1 + th))
def photos_paste(self, pipe, prev_point, point): show_camera = False while len(self.photos) != 0: (d, photo) = self.photos[0] if d < prev_point.time: self.photos.pop(0) continue if d > point.time: return show_camera print "????:", photo image = Image.open(photo).convert("RGBA") image = image.resize(self.get_fit_size(image.size), Image.ANTIALIAS) x_expand = (self.cf.video_width - image.size[0]) / 2 y_expand = (self.cf.video_height - image.size[1]) / 2 image = ImageOps.expand(image, (x_expand,y_expand,x_expand,y_expand), fill="white") for i in range(self.cf.video_fps * self.cf.photos_show_secs): image.save(pipe.stdin, 'PNG') self.photos.pop(0) show_camera = True return show_camera
def __call__(self, imgA, imgB): imgs = [imgA, imgB] output = [] x1 = -1 y1 = -1 for img in imgs: if self.padding > 0: img = ImageOps.expand(img, border=self.padding, fill=0) w, h = img.size th, tw = self.size if w == tw and h == th: output.append(img) continue if x1 == -1 and y1 == -1: x1 = random.randint(0, w - tw) y1 = random.randint(0, h - th) output.append(img.crop((x1, y1, x1 + tw, y1 + th))) return output[0], output[1]
def load_img_seq(path, resize_size=None, color=None,num_frames=12): """Load image from path Args: path (str): image path resize_size (x,y): if not None, image size to resize to color (r,g,b,a): if not None, border color num_frames (int): Number of frames horizontally concatenated in img from path Returns: img(PIL image): processed image """ #print(path) img_orig = Image.open(path) width, height = img_orig.size img = img_orig.crop(((num_frames-2)*width/num_frames,0,(num_frames-1)*width/num_frames, height)) if resize_size: img = img.resize((resize_size[1], resize_size[0])) if color: img = ImageOps.expand(img,border=2,fill=color) return img
def __call__(self, img): """ Args: img (PIL.Image): Image to be cropped. Returns: PIL.Image: Cropped image. """ if self.padding > 0: img = ImageOps.expand(img, border=self.padding, fill=0) w, h = img.size th, tw = self.size if w == tw and h == th: return img x1 = random.randint(0, w - tw) y1 = random.randint(0, h - th) return img.crop((x1, y1, x1 + tw, y1 + th))
def render_image(codes, scale=3, ratio=3, padding=20, fg_color="#000", bg_color="#FFF"): width, height = barcode_size(codes) # Translate hex code colors to RGB tuples bg_color = parse_color(bg_color) fg_color = parse_color(fg_color) # Construct the image image = Image.new("RGB", (width, height), bg_color) # Draw the pixle grid px = image.load() for x, y, visible in modules(codes): px[x, y] = fg_color if visible else bg_color # Scale and add padding image = image.resize((scale * width, scale * height * ratio)) image = ImageOps.expand(image, padding, bg_color) return image
def expand_img(imdir,outdir,pct,color): im = Image.open(imdir) width, height = im.size out_filename = outdir ImageOps.expand(im,int(width*pct), int(color)).save(out_filename, 'JPEG', quality = 100)
def __call__(self, imgA, imgB): imgs = [imgA, imgB] output = [] for img in imgs: output.append(ImageOps.expand(img, border=self.padding, fill=self.fill)) return output[0], output[1]
def pil_adprocess(ad): ad = ImageOps.expand(ad, border=5, fill='black') bg = Image.new("RGB", (640, 480), "white") bg_w, bg_h = bg.size img_w, img_h = ad.size # where to put the image on the background offset = ((bg_w - img_w) / 2, (bg_h - img_h) / 2) bg.paste(ad, offset) return bg
def __call__(self, input): input['img'] = ImageOps.expand(input['img'], border=self.padding, fill=self.fill) return input
def __call__(self, input): if self.padding > 0: input['img'] = ImageOps.expand(img, border=self.padding, fill=0) w, h = input['img'].size th, tw = self.size if w == tw and h == th: return input x1 = random.randint(0, w - tw) y1 = random.randint(0, h - th) input['img'] = input['img'].crop((x1, y1, x1 + tw, y1 + th)) return input
def __call__(self, img): """ Args: img (PIL.Image): Image to be padded. Returns: PIL.Image: Padded image. """ return ImageOps.expand(img, border=self.padding, fill=self.fill)
def crop_images(image_folder, border): for file_name in os.listdir(image_folder): image_path = os.path.join(image_folder, file_name) image = Image.open(image_path) cropped_image = ImageOps.expand(image, border=border, fill='white') cropped_image.save(image_path)
def cleanImage(imagePath): image = Image.open(imagePath) image = image.point(lambda x: 0 if x<143 else 255) borderImage = ImageOps.expand(image,border=20,fill='white') borderImage.save(imagePath)
def draw_gesture(img_src): gesture_path = os.path.join(os.path.dirname(__file__), "gesture.png") # ?????????? ???????? response = requests.get(img_src) response.raise_for_status() image_bytes = io.BytesIO(response.content) # ????????? image = Image.open(image_bytes) gesture = Image.open(gesture_path) if image.mode != 'RGBA': image = image.convert('RGBA') if gesture.mode != 'RGBA': gesture = gesture.convert('RGBA') # ????????? ?????? gesture ??? image image_width, image_height = image.size gest_width, gest_height = gesture.size k = 2 / 3 if (image_width / image_height) < (gest_width / gest_height): new_width = k * image_width new_height = new_width / gest_width * gest_height else: new_height = k * image_height new_width = new_height / gest_height * gest_width gesture = gesture.resize((int(new_width), int(new_height))) gest_width, gest_height = gesture.size diff0 = image_width - gest_width diff1 = image_height - gest_height gesture = ImageOps.expand(gesture, border=(diff0, diff1, 0, 0), fill=0) # ??????????? gesture ? image gestured_image = io.BytesIO() Image.alpha_composite(image, gesture).save(gestured_image, format='PNG') # ???? ?????, ??? ??? ???????? ????????? gestured_image.seek(0) return gestured_image