Python PIL.ImageOps.colorize() Examples

The following are 12 code examples of PIL.ImageOps.colorize(). 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.ImageOps , or try the search function .
Example #1
Source File: __init__.py    From pibooth with MIT License 6 votes vote down vote up
def colorize_pil_image(pil_image, color, bg_color=None):
    """Convert a picto in white to the corresponding color.

    :param pil_image: PIL image to be colorized
    :type pil_image: :py:class:`PIL.Image`
    :param color: RGB color to convert the picto
    :type color: tuple
    :param bg_color: RGB color to use for the picto's background
    :type bg_color: tuple
    """
    if not bg_color:
        bg_color = (abs(color[0] - 255), abs(color[1] - 255), abs(color[2] - 255))
    _, _, _, alpha = pil_image.split()
    gray_pil_image = pil_image.convert('L')
    new_pil_image = ImageOps.colorize(gray_pil_image, black=bg_color, white=color)
    new_pil_image.putalpha(alpha)
    return new_pil_image 
Example #2
Source File: drawing.py    From swmmio with MIT License 6 votes vote down vote up
def annotate_streets(df, img, text_col):
    # confirm font file location
    if not os.path.exists(FONT_PATH):
        print('Error loading default font. Check your FONT_PATH')
        return None

    unique_sts = df[text_col].unique()
    for street in unique_sts:
        draw_coords = df.loc[df.ST_NAME == street, 'draw_coords'].tolist()[0]
        coords = df.loc[df.ST_NAME == street, 'coords'].tolist()[0]
        font = ImageFont.truetype(FONT_PATH, int(25))
        imgTxt = Image.new('L', font.getsize(street))
        drawTxt = ImageDraw.Draw(imgTxt)
        drawTxt.text((0, 0), street, font=font, fill=(10, 10, 12))
        angle = angle_bw_points(coords[0], coords[1])
        texrot = imgTxt.rotate(angle, expand=1)
        mpt = midpoint(draw_coords[0], draw_coords[1])
        img.paste(ImageOps.colorize(texrot, (0, 0, 0), (10, 10, 12)), mpt, texrot) 
Example #3
Source File: test_imageops.py    From python3_ios with BSD 3-Clause "New" or "Revised" License 5 votes vote down vote up
def test_colorize_2color(self):
        # Test the colorizing function with 2-color functionality

        # Open test image (256px by 10px, black to white)
        im = Image.open("Tests/images/bw_gradient.png")
        im = im.convert("L")

        # Create image with original 2-color functionality
        im_test = ImageOps.colorize(im, 'red', 'green')

        # Test output image (2-color)
        left = (0, 1)
        middle = (127, 1)
        right = (255, 1)
        self.assert_tuple_approx_equal(im_test.getpixel(left),
                                       (255, 0, 0),
                                       threshold=1,
                                       msg='black test pixel incorrect')
        self.assert_tuple_approx_equal(im_test.getpixel(middle),
                                       (127, 63, 0),
                                       threshold=1,
                                       msg='mid test pixel incorrect')
        self.assert_tuple_approx_equal(im_test.getpixel(right),
                                       (0, 127, 0),
                                       threshold=1,
                                       msg='white test pixel incorrect') 
Example #4
Source File: test_imageops.py    From python3_ios with BSD 3-Clause "New" or "Revised" License 5 votes vote down vote up
def test_colorize_2color_offset(self):
        # Test the colorizing function with 2-color functionality and offset

        # Open test image (256px by 10px, black to white)
        im = Image.open("Tests/images/bw_gradient.png")
        im = im.convert("L")

        # Create image with original 2-color functionality with offsets
        im_test = ImageOps.colorize(im,
                                    black='red',
                                    white='green',
                                    blackpoint=50,
                                    whitepoint=100)

        # Test output image (2-color) with offsets
        left = (25, 1)
        middle = (75, 1)
        right = (125, 1)
        self.assert_tuple_approx_equal(im_test.getpixel(left),
                                       (255, 0, 0),
                                       threshold=1,
                                       msg='black test pixel incorrect')
        self.assert_tuple_approx_equal(im_test.getpixel(middle),
                                       (127, 63, 0),
                                       threshold=1,
                                       msg='mid test pixel incorrect')
        self.assert_tuple_approx_equal(im_test.getpixel(right),
                                       (0, 127, 0),
                                       threshold=1,
                                       msg='white test pixel incorrect') 
Example #5
Source File: __init__.py    From margipose with Apache License 2.0 5 votes vote down vote up
def random_texture():
    files = list(iglob('resources/textures/*.png'))
    file = files[np.random.randint(0, len(files))]
    texture = Image.open(file).convert('L')
    texture = ImageOps.colorize(
        texture,
        'black',
        (np.random.randint(50, 256), np.random.randint(50, 256), np.random.randint(50, 256))
    )
    return texture 
Example #6
Source File: icon_factory.py    From gd.py with MIT License 5 votes vote down vote up
def apply_color(image: ImageType, color: Tuple[int, int, int]) -> ImageType:
    # [r, g, b, a][3] -> a
    alpha = image.split()[3]

    colored = ImageOps.colorize(ImageOps.grayscale(image), white=color, black="black")
    colored.putalpha(alpha)

    return colored 
Example #7
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 #8
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 #9
Source File: lambda_function.py    From lambda-apigateway-twilio-tutorial with Apache License 2.0 5 votes vote down vote up
def sample_filter(im):
    '''
    A simple filter to be applied to the image
    '''
    black = "#000099"
    white= "#99CCFF"
    filter_image = ImageOps.colorize(ImageOps.grayscale(im), black, white)
    return filter_image 
Example #10
Source File: Utils.py    From script.toolbox with GNU General Public License v2.0 5 votes vote down vote up
def image_recolorize(src, black="#000099", white="#99CCFF"):
    # img = image_recolorize(img, black="#000000", white="#FFFFFF")
    """
    Returns a recolorized version of the initial image using a two-tone
    approach. The color in the black argument is used to replace black pixels
    and the color in the white argument is used to replace white pixels.

    The defaults set the image to a blue hued image.
    """
    return ImageOps.colorize(ImageOps.grayscale(src), black, white) 
Example #11
Source File: test_imageops.py    From python3_ios with BSD 3-Clause "New" or "Revised" License 4 votes vote down vote up
def test_sanity(self):

        ImageOps.autocontrast(hopper("L"))
        ImageOps.autocontrast(hopper("RGB"))

        ImageOps.autocontrast(hopper("L"), cutoff=10)
        ImageOps.autocontrast(hopper("L"), ignore=[0, 255])

        ImageOps.colorize(hopper("L"), (0, 0, 0), (255, 255, 255))
        ImageOps.colorize(hopper("L"), "black", "white")

        ImageOps.pad(hopper("L"), (128, 128))
        ImageOps.pad(hopper("RGB"), (128, 128))

        ImageOps.crop(hopper("L"), 1)
        ImageOps.crop(hopper("RGB"), 1)

        ImageOps.deform(hopper("L"), self.deformer)
        ImageOps.deform(hopper("RGB"), self.deformer)

        ImageOps.equalize(hopper("L"))
        ImageOps.equalize(hopper("RGB"))

        ImageOps.expand(hopper("L"), 1)
        ImageOps.expand(hopper("RGB"), 1)
        ImageOps.expand(hopper("L"), 2, "blue")
        ImageOps.expand(hopper("RGB"), 2, "blue")

        ImageOps.fit(hopper("L"), (128, 128))
        ImageOps.fit(hopper("RGB"), (128, 128))

        ImageOps.flip(hopper("L"))
        ImageOps.flip(hopper("RGB"))

        ImageOps.grayscale(hopper("L"))
        ImageOps.grayscale(hopper("RGB"))

        ImageOps.invert(hopper("L"))
        ImageOps.invert(hopper("RGB"))

        ImageOps.mirror(hopper("L"))
        ImageOps.mirror(hopper("RGB"))

        ImageOps.posterize(hopper("L"), 4)
        ImageOps.posterize(hopper("RGB"), 4)

        ImageOps.solarize(hopper("L"))
        ImageOps.solarize(hopper("RGB"))

        ImageOps.exif_transpose(hopper("L"))
        ImageOps.exif_transpose(hopper("RGB")) 
Example #12
Source File: test_imageops.py    From python3_ios with BSD 3-Clause "New" or "Revised" License 4 votes vote down vote up
def test_colorize_3color_offset(self):
        # Test the colorizing function with 3-color functionality and offset

        # Open test image (256px by 10px, black to white)
        im = Image.open("Tests/images/bw_gradient.png")
        im = im.convert("L")

        # Create image with new three color functionality with offsets
        im_test = ImageOps.colorize(im,
                                    black='red',
                                    white='green',
                                    mid='blue',
                                    blackpoint=50,
                                    whitepoint=200,
                                    midpoint=100)

        # Test output image (3-color) with offsets
        left = (25, 1)
        left_middle = (75, 1)
        middle = (100, 1)
        right_middle = (150, 1)
        right = (225, 1)
        self.assert_tuple_approx_equal(im_test.getpixel(left),
                                       (255, 0, 0),
                                       threshold=1,
                                       msg='black test pixel incorrect')
        self.assert_tuple_approx_equal(im_test.getpixel(left_middle),
                                       (127, 0, 127),
                                       threshold=1,
                                       msg='low-mid test pixel incorrect')
        self.assert_tuple_approx_equal(im_test.getpixel(middle),
                                       (0, 0, 255),
                                       threshold=1,
                                       msg='mid incorrect')
        self.assert_tuple_approx_equal(im_test.getpixel(right_middle),
                                       (0, 63, 127),
                                       threshold=1,
                                       msg='high-mid test pixel incorrect')
        self.assert_tuple_approx_equal(im_test.getpixel(right),
                                       (0, 127, 0),
                                       threshold=1,
                                       msg='white test pixel incorrect')