def blur_mask_of_image(image, channel, radius_my, x1, y1, x2, y2): """ Функция, размывающая маску за даным каналом и радиусом """ image_width, image_height = image.size x1_1 = int(image_width * x1 / 100) y1_1 = int(image_height * y1 / 100) x2_2 = int(image_width * x2 / 100) y2_2 = int(image_height * y2 / 100) mask = (x1_1, y1_1, x2_2, y2_2) image_part_to_blur = image.crop(mask) if channel == "RGB": image_part_to_blur = image_part_to_blur.filter( GaussianBlur(radius=radius_my)) else: r, g, b = image_part_to_blur.split() if channel == "R": r = r.filter(GaussianBlur(radius=radius_my)) if channel == "G": g = g.filter(GaussianBlur(radius=radius_my)) if channel == "B": b = b.filter(GaussianBlur(radius=radius_my)) image_part_to_blur = Image.merge("RGB", (r, g, b)) image.paste(image_part_to_blur, mask) return image
def __call__(self, img, bboxes): if np.random.rand() > self.prob: return img, bboxes factor = self.level_to_arg() img = Image.fromarray(img) img = img.filter(GaussianBlur(radius=factor)) return np.array(img), bboxes
def blur(image_path: str, radius=200, size: tuple = None, save_dir: str = None, use_cache=True) -> Path: img_path = Path(image_path) # skip PNGs if img_path.suffix == '.png': return img_path modified = int(img_path.stat().st_mtime) save_path = img_path.with_suffix(f'.v{modified}.blurred.jpg') if save_dir: save_path = Path(save_dir) / save_path.name if use_cache and save_path.exists(): return save_path with Image.open(img_path) as img, \ open(save_path, mode='w') as destination: if not size: size = max(img.size) img.thumbnail(size, resample=Image.NEAREST) blurred = img.filter(GaussianBlur(radius=radius)) blurred.save(destination, 'JPEG') return save_path
def DownScale_n_GaussianBlur(img, size_image, scale=2, gauss_radius=1.5): from PIL.ImageFilter import GaussianBlur size_im = np.array(size_image) img = img.resize((size_im // scale)) img = img.resize((size_im), Image.BICUBIC) img = img.filter(GaussianBlur(radius=gauss_radius)) return img
def __call__(self, img, target, roi): """ img (PIL Image): Image to be rotated. target (PIL Image): (optional) Target to be rotated Returns: PIL Image: Rotated image(s). """ kernel_radius = self.get_params(self.radius) from PIL.ImageFilter import GaussianBlur return img.filter(GaussianBlur(radius=kernel_radius)), target, roi
def __init__(self, root: str, data_augmentation: bool = True, normalize: bool = False, size: int = 79302016, blur_oe: bool = False, blur_std: float = 1.0, seed: int = 0): super().__init__(root) self.image_size = (3, 32, 32) self.n_classes = 1 # only class 1: outlier since 80 Million Tiny Images is used for outlier exposure self.shuffle = False self.size = size # TinyImages preprocessing: feature scaling to [0, 1] and data augmentation if specified transform = [transforms.ToTensor(), transforms.ToPILImage()] if data_augmentation: transform += [ transforms.ColorJitter(brightness=0.01, contrast=0.01, saturation=0.01, hue=0.01), transforms.RandomHorizontalFlip(p=0.5), transforms.RandomCrop(32, padding=4) ] else: transform += [transforms.CenterCrop(32)] if blur_oe: transform += [ transforms.Lambda( lambda x: x.filter(GaussianBlur(radius=blur_std))) ] transform += [transforms.ToTensor()] if data_augmentation: transform += [ transforms.Lambda(lambda x: x + 0.001 * torch.randn_like(x)) ] if normalize: # CIFAR-10 mean and std transform += [ transforms.Normalize((0.491373, 0.482353, 0.446667), (0.247059, 0.243529, 0.261569)) ] transform = transforms.Compose(transform) # Get dataset self.train_set = TinyImages(root=self.root, size=self.size, transform=transform, download=True, seed=seed) self.test_set = None
def save_ima(nb): global img, Name_file_entry img1 = img if save_kak == 0: img1.save(Name_file_entry.get() + ".png", 'png') elif save_kak == 1: img1 = img1.filter(GaussianBlur(radius=10)) img1.save(Name_file_entry.get() + ".png", 'png') elif save_kak == 2: img1 = img1.filter(ModeFilter(size=9)) img1.save(Name_file_entry.get() + ".png", 'png') elif save_kak == 3: img1 = img1.filter(CONTOUR()) img1.save(Name_file_entry.get() + ".png", 'png') elif save_kak == 4: img1 = img1.filter(FIND_EDGES()) img1.save(Name_file_entry.get() + ".png", 'png') elif save_kak == 5: img1 = img1.convert('L') img1.save(Name_file_entry.get() + ".png", 'png') elif save_kak == 6: img1 = img1.convert('RGB') img1 = ImageOps.invert(img1) img1.save(Name_file_entry.get() + ".png", 'png') img1 = ImageOps.invert(img1) elif save_kak == 7: img1 = img1.filter(DETAIL()) img1.save(Name_file_entry.get() + ".png", 'png') elif save_kak == 8: img1 = img1.filter(SHARPEN()) img1.save(Name_file_entry.get() + ".png", 'png') elif save_kak == 9: img1 = img1.filter(EMBOSS()) img1.save(Name_file_entry.get() + ".png", 'png') elif save_kak == 10: img1 = img1.filter(SMOOTH_MORE()) img1.save(Name_file_entry.get() + ".png", 'png') blur_button["bg"] = "gray" Drawn_button['bg'] = "gray" Contour_button['bg'] = "gray" FIND_EDGES_button['bg'] = "gray" GrayScale_button['bg'] = "gray" negative_button['bg'] = "gray" SHARPEN_button['bg'] = "gray" DETAIL_button['bg'] = "gray" EMBOSS_button['bg'] = "gray" window.withdraw()
def __init__(self, root: str, data_augmentation: bool = True, normalize: bool = False, size: int = 14155519, blur_oe: bool = False, blur_std: float = 1.0, seed: int = 0): super().__init__(root) self.image_size = (3, 224, 224) self.n_classes = 1 # only class 1: outlier since ImageNet22K is used for outlier exposure self.shuffle = False self.size = size # ImageNet preprocessing: feature scaling to [0, 1], data normalization, and data augmentation transform = [transforms.Resize(256)] if data_augmentation: transform += [ transforms.ColorJitter(brightness=0.01, contrast=0.01, saturation=0.01, hue=0.01), transforms.RandomHorizontalFlip(p=0.5), transforms.RandomCrop(224) ] else: transform += [transforms.CenterCrop(224)] if blur_oe: transform += [ transforms.Lambda( lambda x: x.filter(GaussianBlur(radius=blur_std))) ] transform += [transforms.ToTensor()] if data_augmentation: transform += [ transforms.Lambda(lambda x: x + 0.001 * torch.randn_like(x)) ] if normalize: transform += [ transforms.Normalize((0.485, 0.456, 0.406), (0.229, 0.224, 0.225)) ] transform = transforms.Compose(transform) # Get dataset self.train_set = MyImageNet22K(root=self.root + '/fall11_whole_extracted', size=self.size, transform=transform, seed=seed) self.test_set = None
def gaussImagePIL(): global window, canvas, paper, filename, inImage, outImage, inH, inW, outH, outW r = askinteger("가우시안 블러", "값 (0 ~ 100)", minvalue=0, maxvalue=360) outImage = inImage.copy() outImage = outImage.filter(GaussianBlur(radius=r)) outW = outImage.width outH = outImage.height displayImagePIL()
def __getitem__(self, index): if self.mode == 1: # mode =1 为预测时使用,会从所有WSI文件中返回全部的region的图像 slideIDX = self.slideIDX[index] (k, j) = self.grid[index] # img = self.slides[slideIDX].read_region(coord,self.level,(self.patch_size[slideIDX],\ # self.patch_size[slideIDX])).convert('RGB') target = self.targets[slideIDX] img = Image.open( os.path.join( self.image_save_dir, self.batch[slideIDX], str(target), self.slidenames[slideIDX] + '_' + str(k) + '_' + str(j) + '.jpg')) # if img.size != (224,224): # img = img.resize((224,224),Image.BILINEAR) if self.transform is not None: img = self.transform(img) return img, target elif self.mode == 2: # mode =2 为训练时使用,只会根据指定的index(经过上一轮MIL过程得出) \ # 从全部WSI文件中筛选对应的坐标列表,返回相应的训练图像和label batch, slideIDX, (k, j), target, h_value = self.t_data[index] img = Image.open( os.path.join( self.image_save_dir, batch, str(self.targets[slideIDX]), self.slidenames[slideIDX] + '_' + str(k) + '_' + str(j) + '.jpg')) # if h_value > 0: # hue_factor = random.uniform(h_value,0.1) # elif h_value == 0: # hue_factor = random.uniform(0,0) # elif h_value < 0: # hue_factor = random.uniform(-0.1,h_value) #naive_train不再使用上述逻辑 if h_value != 0: img = functional.adjust_hue(img, h_value) # 一般情况下可以将h_value !=0默认为训练模式,只有在训练模式下才进行H通道变换的颜色增强方法 # 如果在maketraindata方法设置采样复制,那么就会针对h_value的值进行不同方向的hue_factor生成,\ # 从而达到复制的样本和原来的样本有不一样的增强的效果 # 如果是在训练模式下,再用一定的概率,新增模糊加强的功能: if np.random.binomial(1, 0.5): radius = random.uniform(0.5, 1.1) img = img.filter(GaussianBlur(radius)) # if img.size != (224,224): # img = img.resize((224,224),Image.BILINEAR) if self.transform is not None: img = self.transform(img) return img, target
def __setBlurEffect(self): """ 设置磨砂效果 """ if not self.blurPicPath: return # 裁剪下需要磨砂的部分 img = Image.open(self.blurPicPath).convert('RGB').resize( (200, 200)) # type:Image.Image img = img.crop((self.cropX, self.cropY, self.width() + self.cropX, self.height() + self.cropY)) img = img.filter(GaussianBlur(self.blurRadius) ).point(lambda x: int(x * 0.7)) self.blurPic = img.toqpixmap() self.update()
def blur_sq(origim, alreadysqim): width, height = origim.size alreadysqim = alreadysqim.filter(GaussianBlur(radius=7)) if isLandscape(width, height) == True: delta = int(1080 / (width/height)) im_topaste = origim.resize((1080, delta)) (left, upper) = (0, int((1080-delta)/2)) else: delta = int(1080 / (height/width)) im_topaste = origim.resize((delta, 1080)) (left, upper) = (int((1080-delta)/2), 0) alreadysqim.paste(im_topaste, (left, upper)) return alreadysqim
def fast_paint(image: Image, brushes: List[int] = [2, 4, 8, 16, 32], blur_factor: int = 2, threshold: int = 100) -> Image: width, height = image.size canvas = Image.new('RGB', image.size, color=(255, 255, 255)) draw_canvas = Draw(canvas) canvas_data = np.array(canvas) # Run through each brush size from large to small for brush_size in sorted(brushes, reverse=True): size_2 = brush_size // 2 blurred_image = image.filter(GaussianBlur(blur_factor * brush_size)) blurred_image_data = np.array(blurred_image) to_brush_positions = [] # Run through each image with squares of brush_size width for x in range(size_2, width - size_2, brush_size): for y in range(size_2, height - size_2, brush_size): canvas_data[x - size_2:x + size_2, y - size_2:y + size_2] # Calculate the error between points in area points_in_area = [(i, j) for i in range(x - size_2, x + size_2) for j in range(y - size_2, y + size_2)] distances = [(distance_rgb(image(point), blurred_image.getpixel(point)), point) for point in points_in_area] error = sum(score for score, _ in distances) print(error) if error > threshold: _, max_point = max(distances) to_brush_positions.append({ 'point': max_point, 'color': image.getpixel(max_point) }) # Paint circles of shuffled brush positions and colors shuffle(to_brush_positions) for to_paint in to_brush_positions: x, y = to_paint['point'] left_up = (x - size_2, y - size_2) right_down = (x + size_2, y + size_2) draw_canvas.ellipse([left_up, right_down], fill=to_paint['color']) return canvas
def setPlaylistCover(self, picPath: str): """ 设置封面 """ self.playlistCoverPath = picPath if not picPath: return # 封面磨砂 img = Image.open(picPath).resize((288, 288)).crop( (0, 46, 288, 242)) # type:Image.Image self.__blurPix = img.filter(GaussianBlur(50)).toqpixmap() self.playlistCoverPix = QPixmap(picPath).scaled( 135, 135, Qt.KeepAspectRatioByExpanding, Qt.SmoothTransformation) # type:QPixmap # 获取主色调 self.dominantRgb = self.__dominantColor.getDominantColor( picPath, tuple) self.update()
def remove_specks_or_blur(X): """This attempts to reduce the noise introduced in the later pages that are harder to read. It does this by attempting to make any black pixel white that does not have enough black neighbour pixels. Alternatively, it applies blurring to the image. Params: X - feature vectors stored as rows in a matrix """ speck_removal_convolution = ( 1, 1, 1, 1, 1, 1, 1, 1, 1 ) convolution = ImageFilter.Kernel( size=(3,3), kernel=speck_removal_convolution, scale=9, offset=0 ) # Apply to each letter individually for i in range(len(X)): # Reshape the feature vector to image dimensions and convert the object # to a Pil Image rectangular_image_array = np.reshape(X[i,:], NEW_DIMENSIONS_2D) image = Image.fromarray(rectangular_image_array) image = image.convert("L") if APPLY_BLURRING: image = image.filter(GaussianBlur(radius=2)) #image = image.filter(ImageFilter.BLUR) elif APPLY_SPECK_REMOVAL: image = image.filter(convolution) # Convert the Pil Image back to a numpy feature vector image_array = np.array(image) image_array = np.reshape(image_array, (NEW_DIMENSIONS,)) X[i,:] = image_array return X
def _transform(image, difficulty): transformations = ['transpose'] if difficulty in ('normal', 'hard'): transformations.append('blur') if difficulty == 'hard': transformations.append('color') transformation = choice(transformations) if transformation == 'transpose': direction = choice(( Image.FLIP_LEFT_RIGHT, Image.FLIP_TOP_BOTTOM, Image.TRANSPOSE, Image.ROTATE_90, Image.ROTATE_180, Image.ROTATE_270)) return image.transpose(direction) elif transformation == 'blur': return image.filter(GaussianBlur(8)) else: # transformation == color color_trf_method = choice(( grayscale, invert, lambda img: posterize(img, 4))) return color_trf_method(image)
def fill(image: Image) -> Image: ori_image = image.copy() image = image.resize(size=(mm_to_px(STICKER_W + 5), mm_to_px(STICKER_H + 5))) image = image.filter(GaussianBlur(radius=16)) offset_x = int((mm_to_px(STICKER_W + 5) - ori_image.size[0]) / 2) offset_y = int((mm_to_px(STICKER_H + 5) - ori_image.size[1]) / 2) image.paste( ori_image, ( offset_x, offset_y, offset_x + ori_image.size[0], offset_y + ori_image.size[1], ), ) return image
def setBlurAlbum(self, imagePath: str, imageSize: tuple = (210, 210), blurRadius=30): """ 更新磨砂专辑封面 """ self.__imagePath = imagePath self.__imageSize = imageSize self.__blurRadius = blurRadius if not imagePath: return # 读入专辑封面 albumCover = Image.open(imagePath).resize( imageSize) # type:Image.Image # 创建一个新图像 blurAlbumCover = Image.new( 'RGBA', (imageSize[0]+2*blurRadius, imageSize[1]+2*blurRadius), (255, 255, 255, 0)) blurAlbumCover.paste(albumCover, (blurRadius, blurRadius)) # 对图像进行高斯模糊 blurAlbumCover = blurAlbumCover.filter(GaussianBlur(blurRadius/2)) self.__blurImage = ImageQt(blurAlbumCover) # 调整窗口大小 self.resize(*blurAlbumCover.size) # 绘制磨砂图 self.update()
def setBlurPic(self, imagePath: str, blurRadius: int = 30): """ 设置磨砂图片 """ self.__picPath = imagePath self.__blurRadius = blurRadius if not imagePath: return # 读入专辑封面 playlistCover = Image.open(imagePath).resize((288, 288)).crop( (0, 92, 288, 288)) # type:Image.Image # 创建一个新图像 blurImage = Image.new('RGBA', (288 + 2 * blurRadius, 196 + 2 * blurRadius), (255, 255, 255, 0)) blurImage.paste(playlistCover, (blurRadius, blurRadius)) # 对图像进行高斯模糊 blurImage = blurImage.filter(GaussianBlur(blurRadius / 2)) self.__blurPix = blurImage.toqpixmap() # 调整窗口大小 self.resize(*blurImage.size) # 绘制磨砂图 self.update()
def blur_im(blur): global img2, Image_main, save_kak img_blur = img2 dimg = img_blur.filter(GaussianBlur(radius=10)) blur_button["bg"] = "black" Drawn_button['bg'] = "gray" Contour_button['bg'] = "gray" FIND_EDGES_button['bg'] = "gray" GrayScale_button['bg'] = "gray" negative_button['bg'] = "gray" SHARPEN_button['bg'] = "gray" DETAIL_button['bg'] = "gray" EMBOSS_button['bg'] = "gray" SMOOTH_MORE_button["bg"] = "gray" imgTK1 = ImageTk.PhotoImage(dimg) Image_main["image"] = imgTK1 Image_main.image = imgTK1 save_kak = 1
def sketch_filter(img, blur_radius=0.8, brightness_ratio=2, contrast_ratio=2): """ generate sketch images by applying filters to the photograph images. tested range of parameters: blur_radius - 0.8~1.5 brightness_offset - 1.5~2.0 contrast_offset - 1.5~3.0 """ to_tensor = transforms.ToTensor() to_pil = transforms.ToPILImage() img_tensor_orig = to_tensor(img) img_blur = img.filter(GaussianBlur(blur_radius)) img_tensor_blur = to_tensor(img_blur) img_mean = (1 + img_tensor_orig - img_tensor_blur) / 2 img = to_pil(img_mean) img = ImageEnhance.Brightness(img).enhance(brightness_ratio) img = ImageEnhance.Contrast(img).enhance(contrast_ratio) return img
def __init__(self, sigma=1): self.sigma = sigma self.filter = GaussianBlur(radius=sigma)
def blur(image, radius=2): bfilter = GaussianBlur(radius=radius) return image.filter(bfilter)
def transform_with_single_float( image: PILImageClass, params: SingleFloatParameter) -> PILImageClass: transformed_image = image.filter(GaussianBlur(params.float_value)) transformed_image.format = image.format return transformed_image
def get_render(self, subject, vid_name, num_views, fid, yid=0, pid=0, random_sample=False): ''' Return the render data :param subject: subject name :param num_views: how many views to return :param view_id: the first view_id. If None, select a random one. :return: 'img': [num_views, C, W, H] images 'calib': [num_views, 4, 4] calibration matrix 'extrinsic': [num_views, 4, 4] extrinsic matrix 'mask': [num_views, 1, W, H] masks ''' pitch = self.pitch_list[pid] # The ids are an even distribution of num_views around view_id view_ids = [self.yaw_list[(yid + len(self.yaw_list) // num_views * offset) % len(self.yaw_list)] for offset in range(num_views)] if random_sample: view_ids = np.random.choice(self.yaw_list, num_views, replace=False) calib_list = [] render_list = [] mask_list = [] extrinsic_list = [] for vid in view_ids: frames_left = int(int(1 - self.temporal_length)/2) frames_right = int(int(self.temporal_length + 1)/2) for frame_num in range(frames_left, frames_right): cur_fid = min(max(0, fid + 5 * frame_num), 95) param_path = os.path.join(self.PARAM, subject, vid_name, 'frame%d_%d_%d_%02d.npy' % (cur_fid, vid, pitch, 0)) render_path = os.path.join(self.RENDER, subject, vid_name, 'frame%d_%d_%d_%02d.jpg' % (cur_fid, vid, pitch, 0)) mask_path = os.path.join(self.MASK, subject, vid_name, 'frame%d_%d_%d_%02d.png' % (cur_fid, vid, pitch, 0)) # loading calibration data param = np.load(param_path, allow_pickle=True) # pixel unit / world unit ortho_ratio = param.item().get('ortho_ratio') # world unit / model unit scale = param.item().get('scale') # camera center world coordinate center = param.item().get('center') # model rotation R = param.item().get('R') translate = -np.matmul(R, center).reshape(3, 1) extrinsic = np.concatenate([R, translate], axis=1) extrinsic = np.concatenate([extrinsic, np.array([0, 0, 0, 1]).reshape(1, 4)], 0) # Match camera space to image pixel space scale_intrinsic = np.identity(4) scale_intrinsic[0, 0] = scale / ortho_ratio scale_intrinsic[1, 1] = -scale / ortho_ratio scale_intrinsic[2, 2] = scale / ortho_ratio # Match image pixel space to image uv space uv_intrinsic = np.identity(4) uv_intrinsic[0, 0] = 1.0 / float(self.opt.loadSize // 2) uv_intrinsic[1, 1] = 1.0 / float(self.opt.loadSize // 2) uv_intrinsic[2, 2] = 1.0 / float(self.opt.loadSize // 2) # Transform under image pixel space trans_intrinsic = np.identity(4) mask = Image.open(mask_path).convert('L') render = Image.open(render_path).convert('RGB') if self.is_train: # Pad images pad_size = int(0.1 * self.load_size) render = ImageOps.expand(render, pad_size, fill=0) mask = ImageOps.expand(mask, pad_size, fill=0) w, h = render.size th, tw = self.load_size, self.load_size # random flip if self.opt.random_flip and np.random.rand() > 0.5: scale_intrinsic[0, 0] *= -1 render = transforms.RandomHorizontalFlip(p=1.0)(render) mask = transforms.RandomHorizontalFlip(p=1.0)(mask) # random scale if self.opt.random_scale: rand_scale = random.uniform(0.9, 1.1) w = int(rand_scale * w) h = int(rand_scale * h) render = render.resize((w, h), Image.BILINEAR) mask = mask.resize((w, h), Image.NEAREST) scale_intrinsic *= rand_scale scale_intrinsic[3, 3] = 1 # random translate in the pixel space if self.opt.random_trans: dx = random.randint(-int(round((w - tw) / 10.)), int(round((w - tw) / 10.))) dy = random.randint(-int(round((h - th) / 10.)), int(round((h - th) / 10.))) else: dx = 0 dy = 0 trans_intrinsic[0, 3] = -dx / float(self.opt.loadSize // 2) trans_intrinsic[1, 3] = -dy / float(self.opt.loadSize // 2) x1 = int(round((w - tw) / 2.)) + dx y1 = int(round((h - th) / 2.)) + dy render = render.crop((x1, y1, x1 + tw, y1 + th)) mask = mask.crop((x1, y1, x1 + tw, y1 + th)) #render = self.aug_trans(render) # random blur if self.opt.aug_blur > 0.00001: blur = GaussianBlur(np.random.uniform(0, self.opt.aug_blur)) render = render.filter(blur) intrinsic = np.matmul(trans_intrinsic, np.matmul(uv_intrinsic, scale_intrinsic)) calib = torch.Tensor(np.matmul(intrinsic, extrinsic)).float() extrinsic = torch.Tensor(extrinsic).float() mask = transforms.Resize(self.load_size)(mask) mask = transforms.ToTensor()(mask).float() render = self.to_tensor(render) render = mask.expand_as(render) * render render_list.append(render) if frame_num == 0: calib_list.append(calib) extrinsic_list.append(extrinsic) mask_list.append(mask) return { 'img': torch.stack(render_list, dim=0), 'calib': torch.stack(calib_list, dim=0), 'extrinsic': torch.stack(extrinsic_list, dim=0), 'mask': torch.stack(mask_list, dim=0) }
def __init__(self, model, filter_radius=7): assert isinstance(model, ApplicationModel) self.model = model self.im_filter = GaussianBlur(radius=filter_radius)
def __getitem__(self, index): if self.mode == 1: batch, slideIDX, (k, j), target, _, loc = self.t_data[index] if self.size in (448, 224): # img = Image.open(os.path.join(self.image_save_dir,batch,str(self.targets[slideIDX]),self.slidenames[slideIDX] + '_' + str(k) + '_' + str(j)+'.jpg')) img = io.imread( os.path.join( self.image_save_dir, self.slidenames[slideIDX], self.slidenames[slideIDX] + '_(' + str(k) + ',' + str(j) + ').jpg')) # io.imread img = cut_img(img, self.size, loc, False) img = Image.fromarray(img) if img.size != (224, 224): img = img.resize((224, 224), Image.BILINEAR) else: img = Image.open( os.path.join( self.image_save_dir, self.slidenames[slideIDX], self.slidenames[slideIDX] + '_(' + str(k) + ',' + str(j) + ').jpg')) # img = img.resize((224,224),Image.BILINEAR) if self.transform is not None: img = self.transform(img) return img, target elif self.mode == 2: # mode =2 为训练时使用,只会根据指定的index(经过上一轮MIL过程得出) \ # 从全部WSI文件中筛选对应的坐标列表,返回相应的训练图像和label batch, slideIDX, (k, j), target, h_value, loc = self.t_data[index] # img = Image.open(os.path.join(self.image_save_dir,batch,str(self.targets[slideIDX]),self.slidenames[slideIDX] + '_' + str(k) + '_' + str(j)+'.jpg')) if self.size <= 448: img = io.imread( os.path.join( self.image_save_dir, self.slidenames[slideIDX], self.slidenames[slideIDX] + '_(' + str(k) + ',' + str(j) + ').jpg')) img = cut_img(img, self.size, loc, True) img = Image.fromarray(img) if img.size != (224, 224): img = img.resize((224, 224), Image.BILINEAR) else: img = Image.open( os.path.join( self.image_save_dir, self.slidenames[slideIDX], self.slidenames[slideIDX] + '_(' + str(k) + ',' + str(j) + ').jpg')) # img = img.resize((224,224),Image.BILINEAR) #对H通道的像素值进行线性变换实现基于H通道的数据增强 # if h_value > 0: # hue_factor = random.uniform(h_value,0.1) # elif h_value == 0: # hue_factor = random.uniform(0,0) # elif h_value < 0: # hue_factor = random.uniform(-0.1,h_value) # #对原图进行一定程度的高斯模糊处理实现模糊的数据增强 if np.random.binomial(1, 0.5): radius = random.uniform(0.5, 1.1) img = img.filter(GaussianBlur(radius)) # # if hue_factor !=0: # img = functional.adjust_hue(img,hue_factor) # 只有在训练模式下才进行H通道变换的颜色增强方法 # 如果在maketraindata方法设置采样复制,那么就会针对h_value的值进行不同方向的hue_factor生成,\ # 从而达到复制的样本和原来的样本有不一样的增强的效果 if self.transform is not None: img = self.transform(img) return img, target
def blur(self, img): if random.random() < self.p: radius = random.randint(self.radius_min, self.radius_max) img = img.filter(GaussianBlur(radius=radius)) return img
import numpy as np from PIL import Image from PIL.ImageFilter import GaussianBlur, MedianFilter, UnsharpMask im = Image.open('dipxe_text.tif') out = im.convert("L") out.show() out_array = np.array(out) blured_image = out.filter(GaussianBlur(0.5)) # blured_image.show() blured_image_array = np.array(blured_image) unsharp_mask = out_array - blured_image_array unsharp_mask_show = Image.fromarray(unsharp_mask) # unsharp_mask_show.show() unsharp_mask_image = out_array + unsharp_mask unsharp_masked_image = Image.fromarray(unsharp_mask_image) unsharp_masked_image.show() direct_unsharp_mask = out.filter(UnsharpMask(2,150,3)) direct_unsharp_mask.show() # high_boost_filter_image = out_array + 10*unsharp_mask # high_boost_filtered_image = Image.fromarray(high_boost_filter_image) # high_boost_filtered_image.show()
def image_process(f, input_dir, output_dir, args): processed = False try: img = Img(f) except IOError: print(colored("IOError;", 'red')) if args.mv: print("Moving to mv dir") nonimages_mv(f, input_dir / NONIMAGES_DIR_NAME) return print(f.name) print(img.size) # What if png file actually an apng? if f.name.endswith( '.png') and img.img.get_format_mimetype() == 'image/apng': print(colored('APNG;', 'red')) if args.mv: print("Moving to mv dir") nonimages_mv(f, input_dir / NONIMAGES_DIR_NAME) return # copy non-png files to output dir if they have small filesize filesize_min_to_process = size2bytes(args.fsize_min) if os.path.getsize(f) < filesize_min_to_process: if args.orignocopy: return print(colored("Size too low\nCopying to out dir", 'blue')) try: shutil.copy2(f, output_dir) except shutil.SameFileError: pass return # optional images bluring for smoothing jpg artifacts if args.blur_radius: img.img = img.img.convert('RGB') img.img = img.img.filter(GaussianBlur(radius=args.blur_radius)) processed = True if args.sharpen: img.img = img.img.filter( UnsharpMask(radius=args.sharpen, percent=150, threshold=0)) # img.img = img.img.filter(Kernel((3, 3), [0 , -1.14, 0 , # -1.14, 5.56 , -1.14, # 0 , -1.14, 0 ], scale=args.sharpen )) processed = True # resize images (has option to ask for each) if they are bigger than args.size # args.size == 0 disables resizing if args.size != '0': if args.size[-1] == 'x': # set resize size by smallest side size_target_min = int(args.size[:-1]) if int(min(img.size)) > size_target_min: if (not args.ask) or input(colored('resize? y/n ', 'yellow')).lower() == 'y': size_target = calc_minsize_target(img.img.size, size_target_min) print(colored('making image smaller', 'yellow')) img.img.thumbnail(size_target, Image.LANCZOS) processed = True else: if int(max(img.size)) > int( args.size): # else set resize size by biggest side size_target = int(args.size) if (not args.ask) or input(colored('resize? y/n ', 'yellow')).lower() == 'y': size_target = size_target, size_target print(colored('making image smaller', 'yellow')) # Lanczos filter is slow, but keeps details and edges. BICUBIC as alternative? img.img.thumbnail(size_target, Image.LANCZOS) processed = True additional_args = { "quality": args.convert_quality, "lossless": args.lossless, "origcopy": not args.orignocopy, "processed": processed, "slow_enc": args.slow } # optional convert to format if args.convert_format: img_save(img, output_dir, args.convert_format, **additional_args, compare=False) return if f.name.endswith('.png'): if args.kpng: ext = 'png' img_save(img, output_dir, ext, **additional_args) elif image_has_transparency(img.img): print(colored('Image has transparency', 'yellow')) ext = 'jxl' if HAVE_JXL else 'webp' img_save(img, output_dir, ext, **additional_args) else: img_save_lossy(img, output_dir, args.nowebp, additional_args) elif f.name.endswith('.jpg'): img_save_lossy(img, output_dir, args.nowebp, additional_args) else: print(colored(str(img.img.format).lower(), 'blue')) print(colored("Copying to out dir", 'blue')) try: shutil.copy2(f, output_dir) except shutil.SameFileError: pass