示例#1
0
def canvas_size(image,
                new_size,
                centering,
                background_color,
                opacity,
                old_size=None):
    #because of layer support photo size can be different from image layer size
    if old_size is None:
        old_size = image.size
    #check if image size has changed
    if old_size == new_size:
        return image
    #displacement
    dx = new_size[0] - old_size[0]
    dy = new_size[1] - old_size[1]
    #alignment
    x = int(centering[0] * dx / 100.0)
    y = int(centering[1] * dy / 100.0)
    #mode
    if image.mode in ['RGBA', 'LA', 'P']:
        if image.mode != 'RGBA':
            image = image.convert('RGBA')
        background_color = HTMLColorToRGBA(background_color, opacity)
    if image.mode in ['RGB', 'RGBA']:
        new_canvas = Image.new(image.mode, new_size, background_color)
    else:
        new_canvas = Image.new('RGB', new_size, background_color)
    if opacity > 0:
        imtools.paste(new_canvas, image, (x, y), mask=image)
    else:
        imtools.paste(new_canvas, image, (x, y), force=True)
    return new_canvas
示例#2
0
def perspective(image, width, height, skew_x, skew_y, offset_x, offset_y, left,
                top, back_color, opacity, resample, crop, transpose):
    image = imtools.convert_safe_mode(image)
    if transpose == 'NONE':
        transpose = None
    else:
        transpose = getattr(Image, transpose)
        image = image.transpose(imtools.get_reverse_transposition(transpose))
    if opacity != 100 or back_color != '#000000':
        image = image.convert('RGBA')
    if width != 0:
        width = 1 / width
    if height != 0:
        height = 1 / height
    offset_x = offset_x * width
    offset_y = offset_y * height
    skew_x = math.tan(r(skew_x))
    skew_y = math.tan(r(skew_y))
    matrix = (width, skew_x, offset_x, skew_y, height, offset_y, left, top)
    perspectived = image.transform(image.size, Image.PERSPECTIVE, matrix,
                                   resample)
    result = imtools.fill_background_color(
        perspectived, HTMLColorToRGBA(back_color, (255 * opacity) / 100))
    if crop:
        result = imtools.auto_crop(result)
    if not (transpose is None):
        result = result.transpose(transpose)
    return result
示例#3
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
示例#4
0
def put_border(image, size, offset, contour_color, fill_color, opacity,
               include_image):
    if opacity < 100:
        fill_color = HTMLColorToRGBA(fill_color, (255 * opacity) / 100)

    if not include_image:
        w, h = image.size
        image = Image.new('RGBA', (w + 2 * offset, h + 2 * offset), fill_color)
    else:
        image = image.convert('RGBA')
        image = ImageOps.expand(image, border=offset, fill=fill_color)
    image = ImageOps.expand(image, border=size, fill=contour_color)
    return image
示例#5
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
示例#6
0
def background(image,
               fill,
               mark,
               color,
               horizontal_offset=None,
               vertical_offset=None,
               horizontal_justification=None,
               vertical_justification=None,
               orientation=None,
               method=None,
               opacity=100):
    if not has_transparency(image):
        return image
    if image.mode == 'P':
        image = image.convert('RGBA')
    if fill == FILL_CHOICES[0]:
        opacity = (255 * opacity) / 100
        return fill_background_color(image, HTMLColorToRGBA(color, opacity))
    elif fill == FILL_CHOICES[1]:
        layer = generate_layer(image.size, mark, method, horizontal_offset,
                               vertical_offset, horizontal_justification,
                               vertical_justification, orientation, opacity)
        paste(layer, image, mask=image)
        return layer
示例#7
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)