def _apply_filters(self, img): ''' ''' if self.color.get(): filt = ImageFilter.UnsharpMask(radius=25, percent=200, threshold=30) img = img.filter(filt) if self.edge.get(): #img = img.convert("LA") #filt = ImageFilter.SHARPEN() #img = img.filter(filt) #enh = ImageEnhance.Brightness(img) #img = enh.enhance(0.25) enh = ImageEnhance.Contrast(img) image = enh.enhance(1.5) #enh = ImageEnhance.Brightness(img) #img = enh.enhance(0.75) filt = ImageFilter.UnsharpMask(radius=50, percent=150, threshold=30) img = img.filter(filt) filt = ImageFilter.EDGE_ENHANCE_MORE() img = img.filter(filt) filt = ImageFilter.CONTOUR() img = img.filter(filt) return img
def preprocess(img_arr, mask_arr): img = Image.fromarray(img_arr) img = ImageEnhance.Brightness(img).enhance(0.95) mask = Image.fromarray(mask_arr) img.paste(mask, (0, 0), mask) # apply circular mask img = ImageEnhance.Color(img).enhance(2) img.save("tmp/_0.jpg") roi = np.array(img) roi[:, :, [0, 2]] = 0 # extract green channel by setting all others to 0 img = Image.fromarray(roi) img.save("tmp/_1.jpg") img = img.convert('L') img.save("tmp/_2.jpg") img = ImageEnhance.Contrast(img).enhance(1.1) img = ImageEnhance.Brightness(img).enhance(1.4) img = img.filter(ImageFilter.MedianFilter(size=7)) img.save("tmp/_3.jpg") img = img.filter(ImageFilter.GaussianBlur(radius=3)) img.save("tmp/_4.jpg") img = ImageEnhance.Contrast(img).enhance(1.07) img = ImageEnhance.Brightness(img).enhance(1.05) img.save("tmp/_5.jpg") img = img.filter( ImageFilter.UnsharpMask(radius=4.5, percent=200, threshold=2)) img.save("tmp/_6.jpg") img_arr = np.array(img) darken_details(img_arr, 19) img = Image.fromarray(img_arr) img.save("tmp/_7.jpg") img = ImageEnhance.Brightness(img).enhance(4) img = ImageEnhance.Contrast(img).enhance(3) img.save("tmp/_8.jpg") img = img.filter(ImageFilter.GaussianBlur(radius=1.9)) img.save("tmp/_9.jpg") img = img.filter( ImageFilter.UnsharpMask(radius=3.8, percent=110, threshold=2)) img.save("tmp/_10.jpg") img = ImageEnhance.Brightness(img).enhance(3) img = ImageEnhance.Contrast(img).enhance(2) img.save("tmp/_11.jpg") img.paste(mask, (0, 0), mask) img = img.convert('L') img.save("tmp/_12.jpg") img_arr = np.array(img) # darken_details(img_arr, 20) # Image.fromarray(img_arr).save("tmp/_13.jpg") return img_arr
def Sharp(path): img = Image.open(path) img_sharp = img.filter( ImageFilter.UnsharpMask(radius=2, percent=150, threshold=3)) img_sharp.save('a1.png') pat = 'a1.png' display(pat)
def unsharp_mask(img, radius=2, percent=150, threshold=3): if not _is_pil_image(img): raise TypeError("img should be PIL Image. Got {}".format(type(img))) return img.filter( ImageFilter.UnsharpMask(radius=radius, percent=percent, threshold=threshold))
def __process_mask__(self, orig_image, mask): """ Applies a mask, removes the gray stroke near the borders of the image without a background (Needs refinement). :param orig_image: Original PIL image :param mask: Mask PIL Image :return: Finished image """ # TODO Move the following algorithm from GIMP to this function. # Algorithm: # 1) Alpha channel in the selection # 2) Invert the selection # 3) Feather the selection (5px) # 4) Delete the selection image = self.__apply_mask__(orig_image, mask) # Image Border Improvement Algorithm # TODO Replace it with the algorithm described above mask = 255 - self.np.array(mask) # Invert mask mask_unsh = Image.fromarray(mask).filter( ImageFilter.UnsharpMask(5, 120, 3)) image_unsh = self.__apply_mask__(image, mask_unsh) new_mask = self.__extact_alpha_channel__(image_unsh) new_mask = Image.fromarray( self.__remove_too_transparent_borders__( 255 - self.np.array(new_mask), 0)) image = self.__apply_mask__(image, new_mask) image = self.np.array(image) - self.np.array( image_unsh ) # The similarity of the "grain extraction" mode in GIMP # image = self.color_correction(image) # TODO Make RGB color correction around the edges image = Image.fromarray(image) return image
def FiksFile(): global mainImg last = len(mainImg) - 1 if angle.get() != 0: print(2) mainImg.append(mainImg[last].rotate(angle.get())) angle.set(0) if sizeVertCrop.get() != 0 or sizeHorCrop.get() != 0: temp = buildCropForm(mainImg[last]) if form4Crop.get() <= 2: temp = temp.crop(drawFromCentr1(temp.size, sizeVertCrop.get())) else: temp = temp.crop( drawFromCentr2(temp.size, sizeHorCrop.get(), sizeVertCrop.get())) mainImg.append(temp) sizeHorCrop.set(0) sizeVertCrop.set(0) if sizeBlur.get() != 0: print(4) mainImg.append(mainImg[last].filter( ImageFilter.GaussianBlur(sizeBlur.get()))) sizeBlur.set(0) if sharpRad.get() != 0 or sharpPerc.get() != 0 or sharpLim.get() != 0: print(5) mainImg.append(mainImg[last].filter( ImageFilter.UnsharpMask(sharpRad.get(), sharpPerc.get(), sharpLim.get()))) sharpRad.set(0) sharpPerc.set(0) sharpLim.set(0) InsertImg()
def convertPixmap(pixmap, brightness, contrast, saturation, sharpness): img = QtGui.QImage(pixmap) buffer = QtCore.QBuffer() buffer.open(QtCore.QIODevice.ReadWrite) img.save(buffer, "PNG") strio = cStringIO.StringIO() strio.write(buffer.data()) buffer.close() strio.seek(0) im = Image.open(strio) im = ImageEnhance.Brightness(im).enhance(brightness) #default 1 im = ImageEnhance.Contrast(im).enhance(contrast) im = ImageEnhance.Sharpness(im).enhance(sharpness) im = im.filter(ImageFilter.UnsharpMask(radius=0, percent=100, threshold=0)) # default 0 100 0 converter = ImageEnhance.Color(im) im = converter.enhance(saturation) #im1.save('/tmp/test.png') qt = ImageQt.ImageQt(im) imgout = QtGui.QImage(qt) rect = imgout.rect() #pixmapout=QtGui.QPixmap(imgout) #-> GARBLED IMAGES-----------------------v pixmapout = QtGui.QPixmap( imgout.copy(rect)) #Darn.. why. has to do with memory management? ################## This also worked: no garbled images when save/load was here: #tempfile='/tmp/TempCDEPanelResource'+str(self.tmpfilecounter)+'.png' #pixmapout.save(tempfile) #pixmapout=QtGui.QPixmap(tempfile) return pixmapout
def predict(img): r"""Predict the age of the given facial image. @Args: image: image matrix @Returns: The predicted age """ model = LogisticRegression(250 * 250, 121) if os.path.exists(os.path.join(CURR_DIR, 'param')): model.load_state_dict(torch.load(os.path.join(CURR_DIR, 'param'))) else: print("No existent weight found. Train the network before using it.") raise FileNotFoundError img = np.array(img) img = Image.fromarray(img, 'RGB') image = img.convert('L') image = image.filter( ImageFilter.UnsharpMask(radius=2, percent=150, threshold=3)) image = np.array(image).reshape(1, 250 * 250) image_tensor = Variable(torch.Tensor(image)) output = model(image_tensor) _, predicted = torch.max(output.data, 1) return predicted.item()
def main(): parser = get_argparser() args = parser.parse_args() dirpath, _, filenames = os.walk( os.path.expanduser(args.image_folder)).next() image_paths = [os.path.join(dirpath, image) for image in filenames] images = get_images(image_paths) smallest_height = min(image.height for image in images) resized_images = resize_images(images, smallest_height) padding = int(args.padding * smallest_height) # Composite width = width of all images, plus padding on either side. width = sum(image.width for image in resized_images) + ( padding * (len(resized_images) + 1)) composite = Image.new("RGB", (width, smallest_height + 2 * padding), color=(255, 255, 255)) paste_images(resized_images, composite, padding) composite.filter(ImageFilter.UnsharpMask(0.4, 150)) if args.ofile: composite.save(os.path.expanduser(args.ofile)) else: composite.show()
def test_sanity(): def apply_filter(filter_to_apply): for mode in ["L", "RGB", "CMYK"]: im = hopper(mode) out = im.filter(filter_to_apply) assert out.mode == im.mode assert out.size == im.size apply_filter(ImageFilter.BLUR) apply_filter(ImageFilter.CONTOUR) apply_filter(ImageFilter.DETAIL) apply_filter(ImageFilter.EDGE_ENHANCE) apply_filter(ImageFilter.EDGE_ENHANCE_MORE) apply_filter(ImageFilter.EMBOSS) apply_filter(ImageFilter.FIND_EDGES) apply_filter(ImageFilter.SMOOTH) apply_filter(ImageFilter.SMOOTH_MORE) apply_filter(ImageFilter.SHARPEN) apply_filter(ImageFilter.MaxFilter) apply_filter(ImageFilter.MedianFilter) apply_filter(ImageFilter.MinFilter) apply_filter(ImageFilter.ModeFilter) apply_filter(ImageFilter.GaussianBlur) apply_filter(ImageFilter.GaussianBlur(5)) apply_filter(ImageFilter.BoxBlur(5)) apply_filter(ImageFilter.UnsharpMask) apply_filter(ImageFilter.UnsharpMask(10)) with pytest.raises(TypeError): apply_filter("hello")
def unsharp(): if request.method == "POST": delete_images() image_src = 'static/uploads/img.png' im = Image.open(image_src) trio = request.get_data(as_text=True) # Datele primite sunt separate in radius, strength, threshold si stocate toate intr-un tuple trio = trio.split('&') trio_radius = trio.pop(0) trio_radius = trio_radius.split('=') trio_radius = int(trio_radius.pop()) trio_strength = trio.pop(0) trio_strength = trio_strength.split('=') trio_strength = int(trio_strength.pop()) trio_threshold = trio.pop(0) trio_threshold = trio_threshold.split('=') trio_threshold = int(trio_threshold.pop()) im_unsharp = im.filter( ImageFilter.UnsharpMask(trio_radius, trio_strength, trio_threshold)) imgname = 'img_unsharp_' + str(trio_radius) + str(trio_strength) + str( trio_threshold) + '.png' im_unsharp.save('static/uploads/' + imgname) image_url_unsharp = url_for('static', filename="uploads/" + imgname) return jsonify({'image_url_unsharp': image_url_unsharp})
def test_usm_accuracy(test_images): snakes = test_images["snakes"] src = snakes.convert("RGB") i = src.filter(ImageFilter.UnsharpMask(5, 1024, 0)) # Image should not be changed because it have only 0 and 255 levels. assert i.tobytes() == src.tobytes()
def process1(): #샤프하게 global im, tk_img out = im.filter(ImageFilter.UnsharpMask(radius=2, percent=150, threshold=3)) tk_img = ImageTk.PhotoImage(out) canvas.create_image(360, 240, image=tk_img) window.update()
def execute_on(self, *args, **kwargs): super(TransformationUnsharpMask, self).execute_on(*args, **kwargs) return self.image.filter( ImageFilter.UnsharpMask(radius=self.radius, percent=self.percent, threshold=self.threshold))
def test_sanity(self): def filter(filter): im = hopper("L") out = im.filter(filter) self.assertEqual(out.mode, im.mode) self.assertEqual(out.size, im.size) filter(ImageFilter.BLUR) filter(ImageFilter.CONTOUR) filter(ImageFilter.DETAIL) filter(ImageFilter.EDGE_ENHANCE) filter(ImageFilter.EDGE_ENHANCE_MORE) filter(ImageFilter.EMBOSS) filter(ImageFilter.FIND_EDGES) filter(ImageFilter.SMOOTH) filter(ImageFilter.SMOOTH_MORE) filter(ImageFilter.SHARPEN) filter(ImageFilter.MaxFilter) filter(ImageFilter.MedianFilter) filter(ImageFilter.MinFilter) filter(ImageFilter.ModeFilter) filter(ImageFilter.Kernel((3, 3), list(range(9)))) filter(ImageFilter.GaussianBlur) filter(ImageFilter.GaussianBlur(5)) filter(ImageFilter.UnsharpMask) filter(ImageFilter.UnsharpMask(10)) self.assertRaises(TypeError, lambda: filter("hello"))
def unsharpmask(input_image): #function that makes a color image blurs the image, unsharpens and controls brightness old_image = Image.open(input_image) new_image = old_image.filter( ImageFilter.UnsharpMask(radius=10, percent=500, threshold=3)) #radius = blur amount, percent = unsharpen in percent, threshold = brightness new_image.save('UnsharpMask_Image.JPEG')
def _crop(self, parsed_args, original): width, height = original.size scale = random.uniform(0.2, 3) # Calculate the crop points crop_width = int(parsed_args.width * scale) crop_height = int(parsed_args.height * scale) left = random.randint(0, width - crop_width) top = random.randint(0, height - crop_height) right = left + crop_width bottom = top + crop_height # Do the crop result = original.crop((left, top, right, bottom)) result = result.resize((parsed_args.width, parsed_args.height)) # Greyscale it result = ImageOps.grayscale(result) # Blur result = result.filter( ImageFilter.GaussianBlur(float(parsed_args.blur))) result = result.filter(ImageFilter.UnsharpMask(float( parsed_args.blur))) result = ImageOps.autocontrast(result) return (result)
def test_sanity(self): def filter(filter): for mode in ["L", "RGB", "CMYK"]: im = hopper(mode) out = im.filter(filter) self.assertEqual(out.mode, im.mode) self.assertEqual(out.size, im.size) filter(ImageFilter.BLUR) filter(ImageFilter.CONTOUR) filter(ImageFilter.DETAIL) filter(ImageFilter.EDGE_ENHANCE) filter(ImageFilter.EDGE_ENHANCE_MORE) filter(ImageFilter.EMBOSS) filter(ImageFilter.FIND_EDGES) filter(ImageFilter.SMOOTH) filter(ImageFilter.SMOOTH_MORE) filter(ImageFilter.SHARPEN) filter(ImageFilter.MaxFilter) filter(ImageFilter.MedianFilter) filter(ImageFilter.MinFilter) filter(ImageFilter.ModeFilter) filter(ImageFilter.GaussianBlur) filter(ImageFilter.GaussianBlur(5)) filter(ImageFilter.BoxBlur(5)) filter(ImageFilter.UnsharpMask) filter(ImageFilter.UnsharpMask(10)) self.assertRaises(TypeError, filter, "hello")
def filter_unsharp_mask(request): image_data = Image.open("photo.jpg") fliter_data = image_data.filter(ImageFilter.UnsharpMask()) msstream=BytesIO() fliter_data.save(msstream,"jpeg") fliter_data.close() return HttpResponse(msstream.getvalue(),content_type="image/jpeg")
def sharpen(self): gerPic = Image.open(self.newPic) sharpenValue = self.horizontalSlider_2.value() self.newPic = gerPic.filter(ImageFilter.UnsharpMask(radius=sharpenValue,percent=300,threshold=3)) self.newPic.save(self.filename) self.newPic = self.filename self.pic = QtGui.QPixmap(self.filename) self.graphicsView.setPixmap(self.pic)
def show_value_1(blur_radius): print('Unsharp Blur Radius: ', blur_radius) custom_filter = ImageFilter.UnsharpMask(radius=float(blur_radius)) img = im1.filter(custom_filter) photo = ImageTk.PhotoImage(img) l['image'] = photo l.photo = photo
def sharp(pic): # black and white sharp = IMG.open(pic) #blur = blur.filter(ImageFilter.GaussianBlur(radius=50)) # add blur, radius = 50 sharp = sharp.filter( ImageFilter.UnsharpMask(radius=8, percent=150, threshold=3)) # add blur, radius = 50 sharp.save('Sharp_' + str(pic) + '.jpg') # save as jpg return
def auto_unsharpmask_exec(self, value_changed): if value_changed: unsharp_img = self.image.filter( ImageFilter.UnsharpMask(self.unsharp_mod_values[0], self.unsharp_mod_values[1], self.unsharp_mod_values[2])) self.image = unsharp_img self.loadImageFromPIX(self.image)
def MadeSharp(self): global mainImg if len(mainImg) == 0: return last = len(mainImg) - 1 newImg = mainImg[last].filter( ImageFilter.UnsharpMask(sharpRad.get(), sharpPerc.get(), sharpLim.get())) InsertSpecImg(newImg)
def unsharp_mask(self, **kwargs): params = kwargs['params'] radius = float(params['radius']) percent = int(params['percent']) threshold = int(params['threshold']) self.final = self.img.filter( ImageFilter.UnsharpMask(radius, percent, threshold)) self.filter = 'unsharp_mask'
def run_sharp(self): value = self.sharp_slider.value() im = Image.open(self.sharp_filename) im = im.filter(ImageFilter.UnsharpMask(2, value, 3)) im.save(self.new_sharp_filename) self.pixmap = QPixmap(self.new_sharp_filename).scaled( self.sharp_image.size(), Qt.KeepAspectRatio) self.sharp_image.setPixmap(self.pixmap)
def unsharp_image(file_name): cv_image = read_image(file_name) image = Image.fromarray(cv_image) pil_im = image.filter( ImageFilter.UnsharpMask(radius=15, percent=350, threshold=3)) # pil_im = image.filter(ImageFilter.UnsharpMask(radius=7, percent=150, threshold=2)) sharpened_image = np.array(pil_im) return sharpened_image
def filter_insta(image): contrast = image.ImageEnhance.Contrast().enhance(1.15) color = ImageEnhance.Color(contrast).enhance(1.2) bright = ImageEnhance.Brightness(color).enhance(0.95) unsharp = bright.filter( ImageFilter.UnsharpMask(radius=2, percent=150, threshold=3)) warm = convert_temp(unsharp, 5000) return warm
def gen_usm(image: Image, percent: int, radius: int, threshold: int, iterations: int) -> Image: result: Image = image for _ in range(iterations): result = result.filter( ImageFilter.UnsharpMask(percent=percent, radius=radius, threshold=threshold)) return result
def unsharp_mask(src: np.ndarray, p) -> np.ndarray: if np.random.uniform() < p: tmp = Image.fromarray(src) percent = random.randint(10, 90) threshold = random.randint(0, 5) mask = ImageFilter.UnsharpMask(percent=percent, threshold=threshold) dst = np.array(tmp.filter(mask), dtype=np.uint8) return dst return src