我们从Python开源项目中,提取了以下10个代码示例,用于说明如何使用PIL.ImageEnhance.Sharpness()。
def avatar_gen_img(self): font_size = int(self.size / 10 * 8) pic_size = self.size an, is_letter = self.avatar_name() font = self.zh_font_file_name if is_letter: font = self.en_font_file_name font_size = int(self.size / 10 * 11) font_file = os.path.abspath(os.path.join(self.font_dir, font)) pygame.init() f = pygame.font.Font(font_file, font_size) is_light=self.is_light_color(self.avatar_background_color()) rtext = f.render(an.upper(), True, (0,0,0) if is_light else (255, 255, 255)) # pygame.image.save(rtext, '%s.png' % an) mode = 'RGBA' astr = pygame.image.tostring(rtext, 'RGBA') circle = Image.new("RGBA", (self.size, self.size)) word = Image.frombytes(mode, f.size(an), astr) word_x = int((pic_size - word.size[0]) / 2) word_y = int(word_x * 0.9) if is_letter: word_y = int((pic_size - word.size[1]) / 2) draw = ImageDraw.Draw(circle) draw.ellipse((0, 0, self.size , self.size ), fill=self.avatar_background_color(), outline=self.avatar_background_color()) draw.point((100, 100), 'red') r, g, b, a = word.split() circle.paste(word, (word_x, word_y), a) sharpness = ImageEnhance.Sharpness(circle) # circle = sharpness.enhance(7.0) # im.show() # circle.show() # print(circle) return circle
def change_sharpness(image, sharpness=1.0): """ Change sharpness of image. >>> image = np.eye(3, dtype='uint8') * 255 >>> change_sharpness(image, 0.5) array([[255, 0, 0], [ 0, 196, 0], [ 0, 0, 255]], dtype=uint8) See http://pillow.readthedocs.io/en/3.1.x/reference/ImageEnhance.html#PIL.ImageEnhance.Sharpness :param numpy array image: Numpy array with range [0,255] and dtype 'uint8'. :param float sharpness: Sharpness [0, ...] :return: Image with changed sharpness :rtype: numpy array with range [0,255] and dtype 'uint8' """ return enhance(image, ie.Sharpness, sharpness)
def do_sharpness(self): """usage: sharpness <image:pic1> Enhance sharpness in the top image. """ from PIL import ImageEnhance factor = float(self.do_pop()) image = self.do_pop() enhancer = ImageEnhance.Sharpness(image) self.push(enhancer.enhance(factor)) # The interpreter loop
def test_draw_on_image_with_enhancements(self, _draw_content_mock, _save, enhance_mock): with create_test_image(): enhance_mock.return_value = PIL_Image.open('test.png') self.img.draw_on_image( image_path=os.path.abspath('test.png'), image_enhancements=((ImageEnhance.Sharpness, 0.5), (ImageEnhance.Brightness, 0.5))) self.assertTrue(enhance_mock.called) self.assertTrue(_draw_content_mock.called)
def sharpness_enhance(self, factor, new_path=None, is_show=False): if self.img is None: img = Image.open(self.path) else: img = self.img img = ImageEnhance.Sharpness(img).enhance(factor) if new_path is not None: img.save(new_path) if is_show: img.show(title='sharpness') return img
def sharpness(func_config): def f(image_config): factor = misc.uniform_sample_from_interval(func_config.min_factor, func_config.max_factor) image_config.image = ImageEnhance.Sharpness(image_config.image).enhance(factor) return f
def call(self, img): if img is None: raise ValueError('img is None') im_n = img.copy() r = random() contrast_low, contrast_high = 0, self.contrast brightness_low, brightness_high = contrast_high, contrast_high + self.brightness sharpness_low, sharpness_high = brightness_high, brightness_high + self.sharpness color_low, color_high = sharpness_high, sharpness_high + self.color if contrast_low <= r < contrast_high: factor_contrast = randint(5, 10)/10 enhancer = ImageEnhance.Contrast(im_n) im_n = enhancer.enhance(factor_contrast) elif brightness_low <= r < brightness_high: factor_brightness = randint(5, 15)/10 enhancer = ImageEnhance.Brightness(im_n) im_n = enhancer.enhance(factor_brightness) elif sharpness_low <= r < sharpness_high: factor_sharpen = randint(0, 20)/10 enhancer = ImageEnhance.Sharpness(im_n) im_n = enhancer.enhance(factor_sharpen) elif color_low <= r < color_high: factor_color = randint(0, 20)/10 enhancer = ImageEnhance.Color(im_n) im_n = enhancer.enhance(factor_color) else: pass return im_n
def randomColor(image): """ ????????? :param image: PIL???image :return: ????????image """ random_factor = np.random.randint(0, 31) / 10. # ???? color_image = ImageEnhance.Color(image).enhance(random_factor) # ???????? random_factor = np.random.randint(10, 21) / 10. # ???? brightness_image = ImageEnhance.Brightness(color_image).enhance(random_factor) # ??????? random_factor = np.random.randint(10, 21) / 10. # ???1? contrast_image = ImageEnhance.Contrast(brightness_image).enhance(random_factor) # ??????? random_factor = np.random.randint(0, 31) / 10. # ???? return ImageEnhance.Sharpness(contrast_image).enhance(random_factor) # ??????
def tesseract(self, img): # keep the data fileName = "tmp_"+int(time.time()+random.randint(1,99999)).__str__()+".jpeg" while os.path.exists( fileName ): fileName = "tmp_"+int(time.time()+random.randint(1,99999)).__str__()+".jpeg" self.tmp_file = fileName with open(self.tmp_file, "w") as oFd: oFd.write(img) # resolve noise try: im = Image.open(self.tmp_file) enhancer = ImageEnhance.Color(im) im = enhancer.enhance(0.0) enhancer = ImageEnhance.Contrast(im) im = enhancer.enhance(3.0) enhancer = ImageEnhance.Brightness(im) im = enhancer.enhance(10.0) enhancer = ImageEnhance.Contrast(im) im = enhancer.enhance(20.0) enhancer = ImageEnhance.Sharpness(im) im = enhancer.enhance(0.0) im.save(self.tmp_file) except Exception as e: pass else: pass # use tesseract imgCode = os.popen("tesseract -l eng -psm 8 {} stdout 2>/dev/null"\ .format(self.tmp_file)).readline()[0:-1] log.debug("Guess Ratio:{}/{}={}%".format(self.guess_hit+1, self.guess_total, \ ((self.guess_hit+1)*100/(self.guess_total)))) os.remove( self.tmp_file ) return imgCode
def TF_enhance_sharpness(x, p=1.0): assert len(x.shape) == 3 h, w, nc = x.shape enhancer = ImageEnhance.Sharpness(np_to_pil(x)) return pil_to_np(enhancer.enhance(p))