コード例 #1
0
ファイル: equalize.py プロジェクト: Reimilia/Camellia
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
コード例 #2
0
ファイル: equalize.py プロジェクト: CamelliaDPG/Camellia
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
コード例 #3
0
ファイル: brightness.py プロジェクト: Pierantonio/phatch
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
コード例 #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
ファイル: invert.py プロジェクト: yorik-freecad-blog/phatch
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
コード例 #6
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
コード例 #7
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
コード例 #8
0
ファイル: posterize.py プロジェクト: CamelliaDPG/Camellia
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
コード例 #9
0
ファイル: solarize.py プロジェクト: Pierantonio/phatch
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
コード例 #10
0
ファイル: pil.py プロジェクト: sean-abbott/phatch
 def convert(self, mode, *args, **keyw):
     """Converts all layers to a different mode."""
     for layer in self.layers.values():
         if layer.image.mode == mode:
             continue
         if mode == 'P' and imtools.has_alpha(layer.image):
             layer.image = imtools.convert(layer.image, mode, *args, **keyw)
             self.info['transparency'] = 255
         elif mode == 'P':
             layer.image = imtools.convert(layer.image, mode, *args, **keyw)
             self.info['transparency'] = None
         else:
             layer.image = imtools.convert(layer.image, mode, *args, **keyw)
コード例 #11
0
 def convert(self, mode, *args, **keyw):
     """Converts all layers to a different mode."""
     for layer in self.layers.values():
         if layer.image.mode == mode:
             continue
         if mode == 'P' and imtools.has_alpha(layer.image):
             layer.image = imtools.convert(layer.image, mode, *args, **keyw)
             self.info['transparency'] = 255
         elif mode == 'P':
             layer.image = imtools.convert(layer.image, mode, *args, **keyw)
             self.info['transparency'] = None
         else:
             layer.image = imtools.convert(layer.image, mode, *args, **keyw)
コード例 #12
0
ファイル: effect.py プロジェクト: yorik-freecad-blog/phatch
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
コード例 #13
0
def reflect(image,
            depth,
            opacity,
            background_color,
            background_opacity,
            scale_method,
            gap=0,
            scale_reflection=False,
            blur_reflection=False,
            cache=None):
    if has_transparency(image):
        image = image.convert('RGBA')
    else:
        image = image.convert('RGB')
    if cache is None:
        cache = {}
    opacity = (255 * opacity) / 100
    background_opacity = (255 * background_opacity) / 100
    scale_method = getattr(Image, scale_method)
    if background_opacity == 255:
        mode = 'RGB'
        color = background_color
    else:
        mode = 'RGBA'
        color = HTMLColorToRGBA(background_color, background_opacity)
    width, height = image.size
    depth = min(height, depth)
    #make reflection
    if has_alpha(image) and background_opacity > 0:
        reflection = Image.new(mode, image.size, color)
        paste(reflection, image, (0, 0), image)
    else:
        reflection = image
    reflection = reflection.transpose(Image.FLIP_TOP_BOTTOM)
    if scale_reflection:
        reflection = reflection.resize((width, depth), scale_method)
    else:
        reflection = reflection.crop((0, 0, width, depth))
    if blur_reflection:
        reflection = reflection.filter(ImageFilter.BLUR)
    mask = gradient_mask((width, depth), opacity, cache)
    #composite
    total_size = (width, height + gap + depth)
    total = Image.new(mode, total_size, color)
    paste(total, image, (0, 0), image)
    paste(total, reflection, (0, height + gap), mask)
    return total
コード例 #14
0
ファイル: reflection.py プロジェクト: Pierantonio/phatch
def reflect(image, depth, opacity, background_color, background_opacity,
        scale_method, gap=0, scale_reflection=False,
        blur_reflection=False, cache=None):
    if has_transparency(image):
        image = image.convert('RGBA')
    else:
        image = image.convert('RGB')
    if cache is None:
        cache = {}
    opacity = (255 * opacity) / 100
    background_opacity = (255 * background_opacity) / 100
    scale_method = getattr(Image, scale_method)
    if background_opacity == 255:
        mode = 'RGB'
        color = background_color
    else:
        mode = 'RGBA'
        color = HTMLColorToRGBA(background_color, background_opacity)
    width, height = image.size
    depth = min(height, depth)
    #make reflection
    if has_alpha(image) and background_opacity > 0:
        reflection = Image.new(mode, image.size, color)
        paste(reflection, image, (0, 0), image)
    else:
        reflection = image
    reflection = reflection.transpose(Image.FLIP_TOP_BOTTOM)
    if scale_reflection:
        reflection = reflection.resize((width, depth), scale_method)
    else:
        reflection = reflection.crop((0, 0, width, depth))
    if blur_reflection:
        reflection = reflection.filter(ImageFilter.BLUR)
    mask = gradient_mask((width, depth), opacity, cache)
    #composite
    total_size = (width, height + gap + depth)
    total = Image.new(mode, total_size, color)
    paste(total, image, (0, 0), image)
    paste(total, reflection, (0, height + gap), mask)
    return total
コード例 #15
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
コード例 #16
0
ファイル: brightness.py プロジェクト: sean-abbott/phatch
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
コード例 #17
0
def make_grid(image,
              grid,
              col_line_width=0,
              row_line_width=0,
              line_color='#FFFFFF',
              line_opacity=0,
              old_size=None,
              scale=True):

    # Check if there is any work to do.
    if grid == (1, 1):
        return image

    # Because of layer support photo size can be different
    # from image layer size
    if old_size is None:
        old_size = image.size

    # Unpack grid
    cols, rows = grid

    # Scaling down?
    if scale:
        # Keep the same number of pixels in the result
        s = sqrt(cols * rows)
        old_size = tuple(map(lambda x: int(x / s), old_size))
        # To scale down we need to make the image processing safe.
        image = imtools.convert_safe_mode(image)\
            .resize(old_size, getattr(Image, 'ANTIALIAS'))

    #displacement
    dx, dy = old_size
    dx += col_line_width
    dy += row_line_width
    new_size = cols * dx - col_line_width, rows * dy - row_line_width

    # The main priority is that the new_canvas has the same mode as the image.

    # Palette images
    if image.mode == 'P':

        if 0 < line_opacity < 255:
            # transparent lines require RGBA
            image = imtools.convert(image, 'RGBA')

        else:
            if 'transparency' in image.info and line_opacity == 0:
                # Make line color transparent for images
                # with transparency.
                line_color_index = image.info['transparency']
                palette = None
            else:
                line_color_index, palette = imtools.fit_color_in_palette(
                    image,
                    ImageColor.getrgb(line_color),
                )
            if line_color_index != -1:
                new_canvas = Image.new('P', new_size, line_color_index)
                imtools.put_palette(new_canvas, image, palette)
            else:
                # Convert to non palette image (RGB or RGBA)
                image = imtools.convert_safe_mode(image)

    # Non palette images
    if image.mode != 'P':

        line_color = ImageColor.getcolor(line_color, image.mode)
        if imtools.has_alpha(image):
            # Make line color transparent for images
            # with an alpha channel.
            line_color = tuple(list(line_color)[:-1] + [line_opacity])
            pass
        new_canvas = Image.new(image.mode, new_size, line_color)

    # Paste grid
    for x in range(cols):
        for y in range(rows):
            pos = (x * dx, y * dy)
            imtools.paste(new_canvas, image, pos, force=True)

    return new_canvas
コード例 #18
0
ファイル: grid.py プロジェクト: Pierantonio/phatch
def make_grid(image, grid, col_line_width=0, row_line_width=0,
        line_color='#FFFFFF', line_opacity=0, old_size=None, scale=True):

    # Check if there is any work to do.
    if grid == (1, 1):
        return image

    # Because of layer support photo size can be different
    # from image layer size
    if old_size is None:
        old_size = image.size

    # Unpack grid
    cols, rows = grid

    # Scaling down?
    if scale:
        # Keep the same number of pixels in the result
        s = sqrt(cols * rows)
        old_size = tuple(map(lambda x: int(x / s), old_size))
        # To scale down we need to make the image processing safe.
        image = imtools.convert_safe_mode(image)\
            .resize(old_size, getattr(Image, 'ANTIALIAS'))

    #displacement
    dx, dy = old_size
    dx += col_line_width
    dy += row_line_width
    new_size = cols * dx - col_line_width, rows * dy - row_line_width

    # The main priority is that the new_canvas has the same mode as the image.

    # Palette images
    if image.mode == 'P':

        if 0 < line_opacity < 255:
            # transparent lines require RGBA
            image = imtools.convert(image, 'RGBA')

        else:
            if 'transparency' in image.info and line_opacity == 0:
                # Make line color transparent for images
                # with transparency.
                line_color_index = image.info['transparency']
                palette = None
            else:
                line_color_index, palette = imtools.fit_color_in_palette(
                    image,
                    ImageColor.getrgb(line_color),
                )
            if line_color_index != -1:
                new_canvas = Image.new('P', new_size, line_color_index)
                imtools.put_palette(new_canvas, image, palette)
            else:
                # Convert to non palette image (RGB or RGBA)
                image = imtools.convert_safe_mode(image)

    # Non palette images
    if image.mode != 'P':

        line_color = ImageColor.getcolor(line_color, image.mode)
        if imtools.has_alpha(image):
            # Make line color transparent for images
            # with an alpha channel.
            line_color = tuple(list(line_color)[:-1] + [line_opacity])
            pass
        new_canvas = Image.new(image.mode, new_size, line_color)

    # Paste grid
    for x in range(cols):
        for y in range(rows):
            pos = (x * dx, y * dy)
            imtools.paste(new_canvas, image, pos, force=True)

    return new_canvas