Пример #1
0
def warmup(image, midtone, brighten, amount=100):
    """Apply a toning filter. Move the midtones to the desired
    color while preserving blacks and whites with optional mixing
    with original image - amount: 0-100%"""

    mode = image.mode
    info = image.info

    if image.mode != "L":
        im = imtools.convert(image, "L")
    else:
        im = image

    if image.mode != "RGBA" and imtools.has_transparency(image):
        image = imtools.convert(image, "RGBA")

    luma = imtools.convert(imtools.split(im)[0], "F")
    o = []
    m = ImageColor.getrgb(midtone)
    b = brighten / 600.0
    # Calculate channels separately
    for l in range(3):
        o.append(ImageMath.eval("m*(255-i)*i+i", i=luma, m=4 * ((m[l] / 255.0) - 0.5 + b) / 255.0).convert("L"))

    colorized = Image.merge("RGB", tuple(o))

    if imtools.has_alpha(image):
        imtools.put_alpha(colorized, imtools.get_alpha(image))

    if amount < 100:
        colorized = imtools.blend(image, colorized, amount / 100.0)

    return colorized
Пример #2
0
def warmup(image, midtone, brighten, amount=100):
    """Apply a toning filter. Move the midtones to the desired
    color while preserving blacks and whites with optional mixing
    with original image - amount: 0-100%"""

    mode = image.mode
    info = image.info

    if image.mode != 'L':
        im = imtools.convert(image, 'L')
    else:
        im = image

    if image.mode != 'RGBA' and imtools.has_transparency(image):
        image = imtools.convert(image, 'RGBA')

    luma = imtools.convert(imtools.split(im)[0], 'F')
    o = []
    m = ImageColor.getrgb(midtone)
    b = brighten / 600.0
    # Calculate channels separately
    for l in range(3):
        o.append(
            ImageMath.eval("m*(255-i)*i+i",
                           i=luma,
                           m=4 * ((m[l] / 255.0) - 0.5 + b) /
                           255.0).convert('L'))

    colorized = Image.merge('RGB', tuple(o))

    if imtools.has_alpha(image):
        imtools.put_alpha(colorized, imtools.get_alpha(image))

    if amount < 100:
        colorized = imtools.blend(image, colorized, amount / 100.0)

    return colorized
Пример #3
0
def color_to_alpha(image, color_value=None, select_color_by=None):
    image = image.convert('RGBA')
    width, height = image.size
    select = select_color_by
    color = color_value
    if select == OPTIONS[0]:
        color = HTMLColorToRGBA(color, 255)
    elif select == OPTIONS[1]:
        color = image.getpixel((0, 0))
    elif select == OPTIONS[2]:
        color = image.getpixel((width - 1, 0))
    elif select == OPTIONS[3]:
        color = image.getpixel((0, height - 1))
    elif select == OPTIONS[4]:
        color = image.getpixel((width - 1, height - 1))
    else:
        return image

    if color[3] == 0:
        # The selected color is transparent
        return image

    color = map(float, color)
    img_bands = [band.convert("F") for band in imtools.split(image)]

    # Find the maximum difference rate between source and color.
    # I had to use two difference functions because ImageMath.eval
    # only evaluates the expression once.
    alpha = ImageMath.eval("""float(
            max(
                max(
                    max(
                        difference1(red_band, cred_band),
                        difference1(green_band, cgreen_band)
                    ),
                    difference1(blue_band, cblue_band)
                ),
                max(
                    max(
                        difference2(red_band, cred_band),
                        difference2(green_band, cgreen_band)
                    ),
                    difference2(blue_band, cblue_band)
                )
            )
        )""",
                           difference1=difference1,
                           difference2=difference2,
                           red_band=img_bands[0],
                           green_band=img_bands[1],
                           blue_band=img_bands[2],
                           cred_band=color[0],
                           cgreen_band=color[1],
                           cblue_band=color[2])

    # Calculate the new image colors after the removal of the selected color
    new_bands = [
        ImageMath.eval("convert((image - color) / alpha + color, 'L')",
                       image=img_bands[i],
                       color=color[i],
                       alpha=alpha) for i in xrange(3)
    ]

    # Add the new alpha band
    new_bands.append(
        ImageMath.eval("convert(alpha_band * alpha, 'L')",
                       alpha=alpha,
                       alpha_band=img_bands[3]))

    return Image.merge('RGBA', new_bands)
Пример #4
0
def color_to_alpha(image, color_value=None, select_color_by=None):
    image = image.convert('RGBA')
    width, height = image.size
    select = select_color_by
    color = color_value
    if select == OPTIONS[0]:
        color = HTMLColorToRGBA(color, 255)
    elif select == OPTIONS[1]:
        color = image.getpixel((0, 0))
    elif select == OPTIONS[2]:
        color = image.getpixel((width - 1, 0))
    elif select == OPTIONS[3]:
        color = image.getpixel((0, height - 1))
    elif select == OPTIONS[4]:
        color = image.getpixel((width - 1, height - 1))
    else:
        return image

    if color[3] == 0:
        # The selected color is transparent
        return image

    color = map(float, color)
    img_bands = [band.convert("F") for band in imtools.split(image)]

    # Find the maximum difference rate between source and color.
    # I had to use two difference functions because ImageMath.eval
    # only evaluates the expression once.
    alpha = ImageMath.eval(
        """float(
            max(
                max(
                    max(
                        difference1(red_band, cred_band),
                        difference1(green_band, cgreen_band)
                    ),
                    difference1(blue_band, cblue_band)
                ),
                max(
                    max(
                        difference2(red_band, cred_band),
                        difference2(green_band, cgreen_band)
                    ),
                    difference2(blue_band, cblue_band)
                )
            )
        )""",
        difference1=difference1,
        difference2=difference2,
        red_band=img_bands[0],
        green_band=img_bands[1],
        blue_band=img_bands[2],
        cred_band=color[0],
        cgreen_band=color[1],
        cblue_band=color[2])

    # Calculate the new image colors after the removal of the selected color
    new_bands = [
        ImageMath.eval(
            "convert((image - color) / alpha + color, 'L')",
            image=img_bands[i],
            color=color[i],
            alpha=alpha)
        for i in xrange(3)]

    # Add the new alpha band
    new_bands.append(ImageMath.eval(
        "convert(alpha_band * alpha, 'L')",
        alpha=alpha,
        alpha_band=img_bands[3]))

    return Image.merge('RGBA', new_bands)