Python PIL.ImageEnhance.Sharpness() Examples

The following are 30 code examples of PIL.ImageEnhance.Sharpness(). You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may also want to check out all available functions/classes of the module PIL.ImageEnhance , or try the search function .
Example #1
Source File: multimedia.py    From chepy with GNU General Public License v3.0 6 votes vote down vote up
def image_sharpness(self, factor: int, extension: str = "png"):
        """Change image sharpness
        
        Args:
            factor (int): Factor to increase the sharpness by
            extension (str, optional): File extension of loaded image. Defaults to "png"
        
        Returns:
            Chepy: The Chepy object. 
        """
        image = Image.open(self._load_as_file())
        image = self._force_rgb(image)
        fh = io.BytesIO()
        enhanced = ImageEnhance.Sharpness(image).enhance(factor)
        enhanced.save(fh, extension)
        self.state = fh.getvalue()
        return self 
Example #2
Source File: deepfry.py    From FlameCogs with MIT License 6 votes vote down vote up
def _fry(img):
		e = ImageEnhance.Sharpness(img)
		img = e.enhance(100)
		e = ImageEnhance.Contrast(img)
		img = e.enhance(100)
		e = ImageEnhance.Brightness(img)
		img = e.enhance(.27)
		r, b, g = img.split()
		e = ImageEnhance.Brightness(r)
		r = e.enhance(4)
		e = ImageEnhance.Brightness(g)
		g = e.enhance(1.75)
		e = ImageEnhance.Brightness(b)
		b = e.enhance(.6)
		img = Image.merge('RGB', (r, g, b))
		e = ImageEnhance.Brightness(img)
		img = e.enhance(1.5)
		temp = BytesIO()
		temp.name = 'deepfried.png'
		img.save(temp)
		temp.seek(0)
		return temp 
Example #3
Source File: neural_style_transfer.py    From Style_Migration_For_Artistic_Font_With_CNN with MIT License 6 votes vote down vote up
def save_img(fname, image, image_enhance=False):  # 图像可以增强
    image = Image.fromarray(image)
    if image_enhance:
        # 亮度增强
        enh_bri = ImageEnhance.Brightness(image)
        brightness = 1.2
        image = enh_bri.enhance(brightness)

        # 色度增强
        enh_col = ImageEnhance.Color(image)
        color = 1.2
        image = enh_col.enhance(color)

        # 锐度增强
        enh_sha = ImageEnhance.Sharpness(image)
        sharpness = 1.2
        image = enh_sha.enhance(sharpness)
    imsave(fname, image)
    return 
Example #4
Source File: data_utils.py    From keras-YOLOv3-model-set with MIT License 6 votes vote down vote up
def random_sharpness(image, jitter=.5):
    """
    Random adjust sharpness for image

    # Arguments
        image: origin image for sharpness change
            PIL Image object containing image data
        jitter: jitter range for random sharpness,
            scalar to control the random sharpness level.

    # Returns
        new_image: adjusted PIL Image object.
    """
    enh_sha = ImageEnhance.Sharpness(image)
    sharpness = rand(jitter, 1/jitter)
    new_image = enh_sha.enhance(sharpness)

    return new_image 
Example #5
Source File: generator.py    From VerifAI with BSD 3-Clause "New" or "Revised" License 6 votes vote down vote up
def modifyImageBscc(imageData, brightness, sharpness, contrast, color):
    """Update with brightness, sharpness, contrast and color."""

    brightnessMod = ImageEnhance.Brightness(imageData)
    imageData = brightnessMod.enhance(brightness)

    sharpnessMod = ImageEnhance.Sharpness(imageData)
    imageData = sharpnessMod.enhance(sharpness)

    contrastMod = ImageEnhance.Contrast(imageData)
    imageData = contrastMod.enhance(contrast)

    colorMod = ImageEnhance.Color(imageData)
    imageData = colorMod.enhance(color)

    return imageData 
Example #6
Source File: test_models.py    From nider with MIT License 5 votes vote down vote up
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) 
Example #7
Source File: image_transforms.py    From KERN with MIT License 5 votes vote down vote up
def __call__(self, img):
        factor = 1.0 + np.random.randn(1)/5
        # print("sharpness {}".format(factor))
        enhancer = ImageEnhance.Sharpness(img)
        return enhancer.enhance(factor) 
Example #8
Source File: spatial_transforms.py    From GAN_Review with MIT License 5 votes vote down vote up
def __call__(self, image):
        if self.rnd:
            enhancer = ImageEnhance.Sharpness(image)
            image = enhancer.enhance(self.factor)
        return image 
Example #9
Source File: operations.py    From Auto-PyTorch with Apache License 2.0 5 votes vote down vote up
def __call__(self, image):
        if random.uniform(0, 1) > self.prob:
            return image
        else:
            magnitude_range = np.linspace(0.1, 1.9, 10)
            factor = magnitude_range[self.magnitude]
            enhancer = ImageEnhance.Sharpness(image)
            return enhancer.enhance(factor) 
Example #10
Source File: operations.py    From Auto-PyTorch with Apache License 2.0 5 votes vote down vote up
def __init__(self, prob, magnitude):
        super(Sharpness, self).__init__(prob, magnitude) 
Example #11
Source File: color.py    From keras-CenterNet with Apache License 2.0 5 votes vote down vote up
def sharpness(image, prob=0.5, min=0, max=2, factor=None):
    random_prob = np.random.uniform()
    if random_prob > prob:
        return image
    if factor is None:
        factor = np.random.uniform(min, max)
    image = Image.fromarray(image[..., ::-1])
    enhancer = ImageEnhance.Sharpness(image)
    image = enhancer.enhance(factor=factor)
    return np.array(image)[..., ::-1] 
Example #12
Source File: deepfryer.py    From X-tra-Telegram with Apache License 2.0 5 votes vote down vote up
def deepfry(img: Image) -> Image:
    colours = (
        (randint(50, 200), randint(40, 170), randint(40, 190)),
        (randint(190, 255), randint(170, 240), randint(180, 250))
    )

    img = img.copy().convert("RGB")

    # Crush image to hell and back
    img = img.convert("RGB")
    width, height = img.width, img.height
    img = img.resize((int(width ** uniform(0.8, 0.9)), int(height ** uniform(0.8, 0.9))), resample=Image.LANCZOS)
    img = img.resize((int(width ** uniform(0.85, 0.95)), int(height ** uniform(0.85, 0.95))), resample=Image.BILINEAR)
    img = img.resize((int(width ** uniform(0.89, 0.98)), int(height ** uniform(0.89, 0.98))), resample=Image.BICUBIC)
    img = img.resize((width, height), resample=Image.BICUBIC)
    img = ImageOps.posterize(img, randint(3, 7))

    # Generate colour overlay
    overlay = img.split()[0]
    overlay = ImageEnhance.Contrast(overlay).enhance(uniform(1.0, 2.0))
    overlay = ImageEnhance.Brightness(overlay).enhance(uniform(1.0, 2.0))

    overlay = ImageOps.colorize(overlay, colours[0], colours[1])

    # Overlay red and yellow onto main image and sharpen the hell out of it
    img = Image.blend(img, overlay, uniform(0.1, 0.4))
    img = ImageEnhance.Sharpness(img).enhance(randint(5, 300))

    return img 
Example #13
Source File: macintoshplus.py    From Trusty-cogs with MIT License 5 votes vote down vote up
def sharpness(im, k=3):
    enhancer = ImageEnhance.Sharpness(im)
    return enhancer.enhance(k) 
Example #14
Source File: auto_augment.py    From isic2019 with MIT License 5 votes vote down vote up
def sharpness(img, magnitude):
    magnitudes = np.linspace(0.1, 1.9, 11)
    img = ImageEnhance.Sharpness(img).enhance(random.uniform(magnitudes[magnitude], magnitudes[magnitude+1]))
    return img 
Example #15
Source File: color.py    From EfficientDet with Apache License 2.0 5 votes vote down vote up
def sharpness(image, prob=0.5, min=0, max=2, factor=None):
    random_prob = np.random.uniform()
    if random_prob > prob:
        return image
    if factor is None:
        # 0 模糊一点, 1 原图, 2 清晰一点
        factor = np.random.uniform(min, max)
    image = Image.fromarray(image[..., ::-1])
    enhancer = ImageEnhance.Sharpness(image)
    image = enhancer.enhance(factor=factor)
    return np.array(image)[..., ::-1] 
Example #16
Source File: image_tfs.py    From tanda with MIT License 5 votes vote down vote up
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)) 
Example #17
Source File: AET_Memory_Dataset.py    From EnAET with MIT License 5 votes vote down vote up
def operate_CCBS(self,img1_transform):
        oper_param=np.random.uniform(low=0.2,high=1.8,size=4).astype('float32')
        image1 = ImageEnhance.Color(img1_transform).enhance(oper_param[0])
        image1 = ImageEnhance.Contrast(image1).enhance(oper_param[1])
        image1 = ImageEnhance.Brightness(image1).enhance(oper_param[2])
        image1 = ImageEnhance.Sharpness(image1).enhance(oper_param[3])
        return image1,oper_param 
Example #18
Source File: bitmap_factory.py    From fluxclient with GNU Affero General Public License v3.0 5 votes vote down vote up
def _get_workspace(self):
        if self._workspace:
            return self._workspace

        workspace = self._gen_empty_workspace()

        for img in self._images:

            #====Enhance test
            brightness = ImageEnhance.Brightness(img.pil_image)
            bright_img = brightness.enhance(2.0)
            bright_img.save("bright2.jpg")
            sharpness = ImageEnhance.Sharpness(img.pil_image)
            sharp_img = sharpness.enhance(10.0)
            sharp_img.save("sharp.jpg")
            #======

            resized_img = self._resize_img(img)
            #resized_img.save("resized.jpg", "JPEG")

            rotate = img.rotation * 180 / pi
            rotated_img = self._rotate_img(resized_img, rotate)
            #rotated_img.save("rotated.jpg", "JPEG")

            corner = self._cal_corner(img, rotated_img)
            workspace.paste(rotated_img, box=corner)
            mirror_image = ImageOps.mirror(rotated_img)
            #workspace.paste(mirror_image, box=corner)



        #========mirror test
        #mirror = ImageOps.mirror(workspace)
        #mirror.save("mirror.jpg", "JPEG")
        #============
        workspace.save("workspace.jpg", "JPEG")
        #mirror.save("workspace.jpg", "JPEG")
        self._workspace = workspace
        return workspace 
Example #19
Source File: eo1_ali_enhance.py    From FloodMapsWorkshop with Apache License 2.0 5 votes vote down vote up
def method6(im):
	r,g,b,a = im.split()
	r = ImageOps.autocontrast(r, cutoff = 2)		
	g = ImageOps.autocontrast(g, cutoff = 2)		
	b = ImageOps.autocontrast(b, cutoff = 2)		
	im3 = Image.merge('RGBA', (r,g,b,a))
	
	enh3 	= ImageEnhance.Sharpness(im3)
	im4 	= enh3.enhance(2.0)
	
	im4.save("test_m6.tif") 
Example #20
Source File: AET_All_Dataloader.py    From EnAET with MIT License 5 votes vote down vote up
def operate_CCBS(self,img1_transform):
        oper_param=np.random.uniform(low=0.2,high=1.8,size=4).astype('float32')
        image1 = ImageEnhance.Color(img1_transform).enhance(oper_param[0])
        image1 = ImageEnhance.Contrast(image1).enhance(oper_param[1])
        image1 = ImageEnhance.Brightness(image1).enhance(oper_param[2])
        image1 = ImageEnhance.Sharpness(image1).enhance(oper_param[3])
        return image1,oper_param 
Example #21
Source File: data_transforms.py    From dla with BSD 3-Clause "New" or "Revised" License 5 votes vote down vote up
def __call__(self, image, *args):
        alpha = 1.0 + np.random.uniform(-self.var, self.var)
        image = ImageEnhance.Sharpness(image).enhance(alpha)
        return (image, *args) 
Example #22
Source File: Transform.py    From VideoSuperResolution with MIT License 5 votes vote down vote up
def call(self, img):
    sharp = min(max(0, self.value), 2)
    return ImageEnhance.Sharpness(img).enhance(sharp) 
Example #23
Source File: macintoshplus.py    From NotSoBot with MIT License 5 votes vote down vote up
def sharpness(im, k=3):
	enhancer = ImageEnhance.Sharpness(im)
	return enhancer.enhance(k) 
Example #24
Source File: augmentations.py    From augmix with Apache License 2.0 5 votes vote down vote up
def sharpness(pil_img, level):
    level = float_parameter(sample_level(level), 1.8) + 0.1
    return ImageEnhance.Sharpness(pil_img).enhance(level) 
Example #25
Source File: deepfry.py    From BotHub with Apache License 2.0 5 votes vote down vote up
def deepfry(img: Image) -> Image:
    colours = (
        (randint(50, 200), randint(40, 170), randint(40, 190)),
        (randint(190, 255), randint(170, 240), randint(180, 250))
    )

    img = img.copy().convert("RGB")

    # Crush image to hell and back
    img = img.convert("RGB")
    width, height = img.width, img.height
    img = img.resize((int(width ** uniform(0.8, 0.9)), int(height ** uniform(0.8, 0.9))), resample=Image.LANCZOS)
    img = img.resize((int(width ** uniform(0.85, 0.95)), int(height ** uniform(0.85, 0.95))), resample=Image.BILINEAR)
    img = img.resize((int(width ** uniform(0.89, 0.98)), int(height ** uniform(0.89, 0.98))), resample=Image.BICUBIC)
    img = img.resize((width, height), resample=Image.BICUBIC)
    img = ImageOps.posterize(img, randint(3, 7))

    # Generate colour overlay
    overlay = img.split()[0]
    overlay = ImageEnhance.Contrast(overlay).enhance(uniform(1.0, 2.0))
    overlay = ImageEnhance.Brightness(overlay).enhance(uniform(1.0, 2.0))

    overlay = ImageOps.colorize(overlay, colours[0], colours[1])

    # Overlay red and yellow onto main image and sharpen the hell out of it
    img = Image.blend(img, overlay, uniform(0.1, 0.4))
    img = ImageEnhance.Sharpness(img).enhance(randint(5, 300))

    return img 
Example #26
Source File: auto_augment.py    From keras-auto-augment with MIT License 5 votes vote down vote up
def sharpness(img, magnitude):
    magnitudes = np.linspace(0.1, 1.9, 11)

    img = Image.fromarray(img)
    img = ImageEnhance.Sharpness(img).enhance(random.uniform(magnitudes[magnitude], magnitudes[magnitude+1]))
    img = np.array(img)
    return img 
Example #27
Source File: trans.py    From ocr.pytorch with MIT License 5 votes vote down vote up
def tranfun(self, image):
        image = getpilimage(image)
        sha = ImageEnhance.Sharpness(image)
        return sha.enhance(random.uniform(self.lower, self.upper)) 
Example #28
Source File: mydataset.py    From ocr.pytorch with MIT License 5 votes vote down vote up
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 )  # 调整图像锐度 
Example #29
Source File: textart.py    From minqlx-plugins with GNU General Public License v3.0 5 votes vote down vote up
def image_to_unicode(self, image, font_data, width=None, height=None):
        img = Image.open(image)
        if width and not height:
            ratio = width/img.size[0]
            img = img.resize((width, round(img.size[1] * ratio * 0.5)), Image.BILINEAR)
        elif not width and height:
            ratio = width/img.size[1]
            img = img.resize((round(img.size[0] * ratio), round(height * 0.5)), Image.BILINEAR)
        else:
            img = img.resize((width, round(height * 0.5)), Image.BILINEAR)
        img = img.convert("L")

        # Enhance!
        #contrast = ImageEnhance.Contrast(img)
        #img = contrast.enhance(0.7)
        #sharpen = ImageEnhance.Sharpness(img)
        #img = sharpen.enhance(1.5)

        # Process data.
        keys = sorted(list(font_data.keys()))

        out = ""
        for y in range(img.size[1]):
            for x in range(img.size[0]):
                lum = img.getpixel((x, y))
                index = bisect.bisect(keys, lum) - 1
                out += chr(random.choice(font_data[keys[index]]))
            out += "\n"

        return out 
Example #30
Source File: face_sketch_data.py    From Face-Sketch-Wild with MIT License 5 votes vote down vote up
def __call__(self, sample):
        img = sample[0]
        sharp_factor = np.random.uniform(max(0, 1 - self.sharp), 1 + self.sharp)
        enhancer = ImageEnhance.Sharpness(img)
        img = enhancer.enhance(sharp_factor)

        transform = self.get_params(self.brightness, self.contrast,
                                    self.saturation, self.hue)
        img = transform(img)
        sample[0] = img

        return sample