예제 #1
0
def brightness(image, amount=50):
    """Adjust brightness from black to white
    - amount: -1(black) 0 (unchanged) 1(white)
    - repeat: how many times it should be repeated"""
    if amount == 0:
        return image

    image = imtools.convert_safe_mode(image)

    if amount < 0:
        #fade to black
        im = imtools.blend(
                image,
                Image.new(image.mode, image.size, 0),
                -amount / 100.0)
    else:
        #fade to white
        im = imtools.blend(
                image,
                Image.new(image.mode, image.size,
                    ImageColor.getcolor('white', image.mode)),
                amount / 100.0)
    #fix image transparency mask
    if imtools.has_alpha(image):
        im.putalpha(imtools.get_alpha(image))
    return im
예제 #2
0
def grayscale(image, amount=100):
    grayscaled = ImageOps.grayscale(image)
    if amount < 100:
        grayscaled = imtools.blend(image, grayscaled, amount / 100.0)
    if image.mode == "RGBA":
        grayscaled.putalpha(imtools.get_alpha(image))
    return grayscaled
예제 #3
0
def grayscale(image, amount=100):
    grayscaled = ImageOps.grayscale(image)
    if amount < 100:
        grayscaled = imtools.blend(image, grayscaled, amount / 100.0)
    if image.mode == 'RGBA':
        grayscaled.putalpha(imtools.get_alpha(image))
    return grayscaled
예제 #4
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
예제 #5
0
def median(image, radius, amount=100):
    """Apply a filter
    - amount: 0-1"""
    image = imtools.convert_safe_mode(image)
    medianed = image.filter(ImageFilter.MedianFilter(radius))
    if amount < 100:
        return imtools.blend(image, medianed, amount / 100.0)
    return medianed
예제 #6
0
def invert(image, amount=100):
    image = imtools.convert_safe_mode(image)
    inverted = ImageChops.invert(image)
    if amount < 100:
        inverted = imtools.blend(image, inverted, amount / 100.0)
    if imtools.has_alpha(image):
        inverted.putalpha(imtools.get_alpha(image))
    return inverted
예제 #7
0
def maximum(image, radius, amount=100):
    """Apply a filter
    - amount: 0-1"""
    image = imtools.convert_safe_mode(image)
    maximumed = image.filter(ImageFilter.MaxFilter(radius))
    if amount < 100:
        return imtools.blend(image, maximumed, amount / 100.0)
    return maximumed
예제 #8
0
def common(image, radius, amount=100):
    """Apply a filter
    - amount: 0-1"""
    image = imtools.convert_safe_mode(image)
    commoned = image.filter(ImageFilter.ModeFilter(radius))
    if amount < 100:
        return imtools.blend(image, commoned, amount / 100.0)
    return commoned
예제 #9
0
def common(image, radius, amount=100):
    """Apply a filter
    - amount: 0-1"""
    image = imtools.convert_safe_mode(image)
    commoned = image.filter(ImageFilter.ModeFilter(radius))
    if amount < 100:
        return imtools.blend(image, commoned, amount / 100.0)
    return commoned
예제 #10
0
def rnk(image, radius, rank=50, amount=100):
    """Apply a filter
    - amount: 0-1"""
    rank /= 100.0
    r = int((radius * radius - 1) * rank)
    image = imtools.convert_safe_mode(image)
    ranked = image.filter(ImageFilter.RankFilter(radius, r))
    if amount < 100:
        return imtools.blend(image, ranked, amount / 100)
    return ranked
예제 #11
0
파일: rank.py 프로젝트: Reimilia/Camellia
def rnk(image, radius, rank=50, amount=100):
    """Apply a filter
    - amount: 0-1"""
    rank /= 100.0
    r = int((radius * radius - 1) * rank)
    image = imtools.convert_safe_mode(image)
    ranked = image.filter(ImageFilter.RankFilter(radius, r))
    if amount < 100:
        return imtools.blend(image, ranked, amount / 100)
    return ranked
예제 #12
0
def posterize(image, bits, amount=100):
    """Apply a filter
    - amount: 0-1"""
    image = imtools.convert_safe_mode(image)
    posterized = imtools.remove_alpha(image)
    posterized = ImageOps.posterize(posterized, bits)
    if imtools.has_alpha(image):
        imtools.put_alpha(posterized, imtools.get_alpha(image))
    if amount < 100:
        return imtools.blend(image, posterized, amount / 100.0)
    return posterized
예제 #13
0
def solarize(image, treshold, amount=100):
    """Apply a filter
    - amount: 0-1"""
    image = imtools.convert_safe_mode(image)
    solarized = image.convert('RGB')
    solarized = ImageOps.solarize(solarized, treshold)
    if imtools.has_alpha(image):
        imtools.put_alpha(solarized, imtools.get_alpha(image))
    if amount < 100:
        return imtools.blend(image, solarized, amount / 100.0)
    return solarized
예제 #14
0
def solarize(image, treshold, amount=100):
    """Apply a filter
    - amount: 0-1"""
    image = imtools.convert_safe_mode(image)
    solarized = image.convert('RGB')
    solarized = ImageOps.solarize(solarized, treshold)
    if imtools.has_alpha(image):
        imtools.put_alpha(solarized, imtools.get_alpha(image))
    if amount < 100:
        return imtools.blend(image, solarized, amount / 100.0)
    return solarized
예제 #15
0
def posterize(image, bits, amount=100):
    """Apply a filter
    - amount: 0-1"""
    image = imtools.convert_safe_mode(image)
    posterized = imtools.remove_alpha(image)
    posterized = ImageOps.posterize(posterized, bits)
    if imtools.has_alpha(image):
        imtools.put_alpha(posterized, imtools.get_alpha(image))
    if amount < 100:
        return imtools.blend(image, posterized, amount / 100.0)
    return posterized
예제 #16
0
def equalize(image, amount=100):
    image = imtools.convert_safe_mode(image)
    if imtools.has_alpha(image):
        equalized = imtools.remove_alpha(image)
    else:
        equalized = image
    equalized = ImageOps.equalize(equalized)
    if imtools.has_alpha(image):
        imtools.put_alpha(equalized, imtools.get_alpha(image))
    if amount < 100:
        equalized = imtools.blend(image, equalized, amount / 100.0)
    return equalized
예제 #17
0
def rotate(image, angle, resample_image, expand=0, amount=100,
        background_color='#000000', background_opacity=100):
    resample_image = getattr(Image, resample_image)
    if background_opacity != 100 or background_color != '#000000':
        image = image.convert('RGBA')
    rotated = image.rotate(angle, resample_image, expand)
    rgba = HTMLColorToRGBA(background_color,
                    255 * background_opacity / 100)
    rotated = imtools.fill_background_color(rotated, rgba)
    if amount < 100:
        rotated = imtools.blend(image, rotated, amount / 100.0, rgba)
    return rotated
예제 #18
0
def rotate(image, angle, resample_image, expand=0, amount=100,
        background_color='#000000', background_opacity=100):
    resample_image = getattr(Image, resample_image)
    if background_opacity != 100 or background_color != '#000000':
        image = image.convert('RGBA')
    rotated = image.rotate(angle, resample_image, expand)
    rgba = HTMLColorToRGBA(background_color,
                    255 * background_opacity / 100)
    rotated = imtools.fill_background_color(rotated, rgba)
    if amount < 100:
        rotated = imtools.blend(image, rotated, amount / 100.0, rgba)
    return rotated
예제 #19
0
def equalize(image, amount=100):
    image = imtools.convert_safe_mode(image)
    if imtools.has_alpha(image):
        equalized = imtools.remove_alpha(image)
    else:
        equalized = image
    equalized = ImageOps.equalize(equalized)
    if imtools.has_alpha(image):
        imtools.put_alpha(equalized, imtools.get_alpha(image))
    if amount < 100:
        equalized = imtools.blend(image, equalized, amount / 100.0)
    return equalized
예제 #20
0
def saturation(image, amount=50):
    """Adjust brightness from black to white
    - amount: -1(black) 0 (unchanged) 1(white)
    - repeat: how many times it should be repeated"""
    if amount == 0:
        return image
    amount /= 100.0
    grey = image.convert("L").convert(image.mode)
    if amount < 0:
        #grayscale
        im = imtools.blend(image, grey, -amount)
    else:
        #overcolored = Image - (alpha * Grey) / (1 - alpha)
        alpha = 0.7
        alpha_g = grey.point(lambda x: x * alpha)
        i_minus_alpha_g = ImageChops.subtract(image, alpha_g)
        overcolored = i_minus_alpha_g.point(lambda x: x / (1 - alpha))
        im = imtools.blend(image, overcolored, amount)
    #fix image transparency mask
    if image.mode == 'RGBA':
        im.putalpha(imtools.get_alpha(image))
    return im
예제 #21
0
def saturation(image, amount=50):
    """Adjust brightness from black to white
    - amount: -1(black) 0 (unchanged) 1(white)
    - repeat: how many times it should be repeated"""
    if amount == 0:
        return image
    amount /= 100.0
    grey = image.convert("L").convert(image.mode)
    if amount < 0:
        #grayscale
        im = imtools.blend(image, grey, -amount)
    else:
        #overcolored = Image - (alpha * Grey) / (1 - alpha)
        alpha = 0.7
        alpha_g = grey.point(lambda x: x * alpha)
        i_minus_alpha_g = ImageChops.subtract(image, alpha_g)
        overcolored = i_minus_alpha_g.point(lambda x: x / (1 - alpha))
        im = imtools.blend(image, overcolored, amount)
    #fix image transparency mask
    if image.mode == 'RGBA':
        im.putalpha(imtools.get_alpha(image))
    return im
예제 #22
0
def contrast(image, amount=50):
    """Adjust brightness from black to white
    - amount: -1(black) 0 (unchanged) 1(white)
    - repeat: how many times it should be repeated"""
    if amount == 0:
        return image
    elif amount < 0:
        #low contrast
        mean = reduce(lambda a, b: a + b,
                      image.convert("L").histogram()) / 256.0
        im = imtools.blend(
            image,
            Image.new("L", image.size, mean).convert(image.mode),
            -amount / 100.0)
    else:
        #high contrast
        im = imtools.blend(image, image.point(lambda x: x > 128 and 255),
                           amount / 100.0)
    #fix image transparency mask
    if image.mode == 'RGBA':
        im.putalpha(imtools.get_alpha(image))
    return im
예제 #23
0
def colorize(image, black, white, amount=100):
    """Apply a filter
    - amount: 0-1"""
    if image.mode != 'L':
        im = image.convert('L')
    else:
        im = image
    colorized = ImageOps.colorize(im, black, white)
    if image.mode == 'RGBA':
        colorized = colorized.convert('RGBA')
        colorized.putalpha(imtools.get_alpha(image))
    if amount < 100:
        return imtools.blend(image, colorized, amount / 100.0)
    return colorized
예제 #24
0
def colorize(image, black, white, amount=100):
    """Apply a filter
    - amount: 0-1"""
    if image.mode != 'L':
        im = image.convert('L')
    else:
        im = image
    colorized = ImageOps.colorize(im, black, white)
    if image.mode == 'RGBA':
        colorized = colorized.convert('RGBA')
        colorized.putalpha(imtools.get_alpha(image))
    if amount < 100:
        return imtools.blend(image, colorized, amount / 100.0)
    return colorized
예제 #25
0
def autocontrast(image, amount=100.0, cutoff=0):
    """Apply a filter
    - amount: 0-1
    - repeat: how many times it should be repeated"""
    image = imtools.convert_safe_mode(image)
    if imtools.has_transparency(image):
        im = imtools.remove_alpha(image)
    else:
        im = image
    contrasted = ImageOps.autocontrast(im, cutoff)
    if imtools.has_transparency(image):
        imtools.put_alpha(contrasted, imtools.get_alpha(image))
    if amount < 100:
        return imtools.blend(image, contrasted, amount / 100.0)
    return contrasted
예제 #26
0
def autocontrast(image, amount=100.0, cutoff=0):
    """Apply a filter
    - amount: 0-1
    - repeat: how many times it should be repeated"""
    image = imtools.convert_safe_mode(image)
    if imtools.has_transparency(image):
        im = imtools.remove_alpha(image)
    else:
        im = image
    contrasted = ImageOps.autocontrast(im, cutoff)
    if imtools.has_transparency(image):
        imtools.put_alpha(contrasted, imtools.get_alpha(image))
    if amount < 100:
        return imtools.blend(image, contrasted, amount / 100.0)
    return contrasted
예제 #27
0
def contrast(image, amount=50):
    """Adjust brightness from black to white
    - amount: -1(black) 0 (unchanged) 1(white)
    - repeat: how many times it should be repeated"""
    if amount == 0:
        return image
    elif amount < 0:
        #low contrast
        mean = reduce(lambda a, b:
            a + b, image.convert("L").histogram()) / 256.0
        im = imtools.blend(
                image,
                Image.new("L", image.size, mean).convert(image.mode),
                -amount / 100.0)
    else:
        #high contrast
        im = imtools.blend(
                image,
                image.point(lambda x: x > 128 and 255),
                amount / 100.0)
    #fix image transparency mask
    if image.mode == 'RGBA':
        im.putalpha(imtools.get_alpha(image))
    return im
예제 #28
0
def brightness(image, amount=50):
    """Adjust brightness from black to white
    - amount: -1(black) 0 (unchanged) 1(white)
    - repeat: how many times it should be repeated"""
    if amount == 0:
        return image

    image = imtools.convert_safe_mode(image)

    if amount < 0:
        #fade to black
        im = imtools.blend(image, Image.new(image.mode, image.size, 0),
                           -amount / 100.0)
    else:
        #fade to white
        im = imtools.blend(
            image,
            Image.new(image.mode, image.size,
                      ImageColor.getcolor('white', image.mode)),
            amount / 100.0)
    #fix image transparency mask
    if imtools.has_alpha(image):
        im.putalpha(imtools.get_alpha(image))
    return im
예제 #29
0
def effect(image, filter, amount=100, repeat=1):
    """Apply a filter
    - amount: 0-1
    - repeat: how many times it should be repeated"""
    filter = getattr(ImageFilter, filter)
    image = imtools.convert_safe_mode(image)
    for i in range(repeat):
        filtered = image.filter(filter)
        if imtools.has_alpha(image) and \
           filter in [ImageFilter.CONTOUR, ImageFilter.EMBOSS]:
            filtered.putalpha(imtools.get_alpha(image))
        if amount < 100:
            image = imtools.blend(image, filtered, amount / 100.0)
        else:
            image = filtered
    return image
예제 #30
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
예제 #31
0
def transpose(image, method, amount=100):
    transposed = image.transpose(getattr(Image, method))
    if amount < 100:
        transposed = imtools.blend(image, transposed, amount / 100.0)
    return transposed
예제 #32
0
def transpose(image, method, amount=100):
    transposed = image.transpose(getattr(Image, method))
    if amount < 100:
        transposed = imtools.blend(image, transposed, amount / 100.0)
    return transposed