Ejemplo n.º 1
0
def image_tint(im, tint='#ff0000'):

    src = Image.new('RGB', im.size)
    src.paste(im)

    tr, tg, tb = getrgb(tint)
    tl = getcolor(tint, "L")  # tint color's overall luminosity
    if not tl: tl = 1  # avoid division by zero
    tl = float(tl)  # compute luminosity preserving tint factors
    sr, sg, sb = map(lambda tv: tv / tl,
                     (tr, tg, tb))  # per component adjustments

    # create look-up tables to map luminosity to adjusted tint
    # (using floating-point math only to compute table)
    luts = (list(map(lambda lr: int(lr * sr + 0.5), range(256))) +
            list(map(lambda lg: int(lg * sg + 0.5), range(256))) +
            list(map(lambda lb: int(lb * sb + 0.5), range(256))))
    l = grayscale(src)  # 8-bit luminosity version of whole image
    if Image.getmodebands(src.mode) < 4:
        merge_args = (src.mode, (l, l, l))  # for RGB verion of grayscale
    else:  # include copy of src image's alpha layer
        a = Image.new("L", src.size)
        a.putdata(src.getdata(3))
        merge_args = (src.mode, (l, l, l, a))  # for RGBA verion of grayscale
        luts += range(256)  # for 1:1 mapping of copied alpha values

    return Image.merge(*merge_args).point(luts)
Ejemplo n.º 2
0
def image_tint(src, tint='#fffab5'):
    if Image.isStringType(src):  # file path?
        src = Image.open(src)
    if src.mode not in ['RGB', 'RGBA']:
        raise TypeError('Unsupported source image mode: {}'.format(src.mode))
    src.load()

    tr, tg, tb = getrgb(tint)
    tl = getcolor(tint, "L")  # tint color's overall luminosity
    if not tl: tl = 1  # avoid division by zero
    tl = float(tl)  # compute luminosity preserving tint factors
    sr, sg, sb = map(lambda tv: tv/tl, (tr, tg, tb))  # per component
                                                      # adjustments
    # create look-up tables to map luminosity to adjusted tint
    # (using floating-point math only to compute table)
    luts = (tuple(map(lambda lr: int(lr*sr + 0.5), range(256))) +
            tuple(map(lambda lg: int(lg*sg + 0.5), range(256))) +
            tuple(map(lambda lb: int(lb*sb + 0.5), range(256))))
    l = grayscale(src)  # 8-bit luminosity version of whole image
    if Image.getmodebands(src.mode) < 4:
        merge_args = (src.mode, (l, l, l))  # for RGB verion of grayscale
    else:  # include copy of src image's alpha layer
        a = Image.new("L", src.size)
        a.putdata(src.getdata(3))
        merge_args = (src.mode, (l, l, l, a))  # for RGBA verion of grayscale
        luts += tuple(range(256))  # for 1:1 mapping of copied alpha values
    return Image.merge(*merge_args).point(luts)
Ejemplo n.º 3
0
    def image_tint(self, source_path, tint='#ffffff'):
        '''
        Take an image from a path and convert it to a tinted string for sprite coloration.
        :param source_path: sting path to image
        :param tint: string color code in hex
        :return: string of modified image
        '''

        img_source = Image.open(source_path)                # Get the image from specified path
        tint_red, tint_green, tint_blue = getrgb(tint)      # Get color tint of each color
        tint_lum = getcolor(tint, "L")                      # Tint color luminosity

        if tint_lum == 0:   # Avoid division by 0
            tint_lum = 1

        tint_lum = float(tint_lum)  # Compute luminosity preserving tint factors
        sr, sg, sb = map(lambda tv: tv / tint_lum, (tint_red, tint_green, tint_blue))  # per component adjustments

        # create look-up tables to map luminosity to adjusted tint
        # (using floating-point math only to compute table)
        luts = (list(map(lambda lr: int(lr * sr + 0.5), range(256))) +
                list(map(lambda lg: int(lg * sg + 0.5), range(256))) +
                list(map(lambda lb: int(lb * sb + 0.5), range(256))))

        l = grayscale(img_source)  # 8-bit luminosity version of whole image

        if Image.getmodebands(img_source.mode) < 4:
            merge_args = (img_source.mode, (l, l, l))  # for RGB verion of grayscale
        else:  # include copy of img_source image's alpha layer
            a = Image.new("L", img_source.size)
            a.putdata(img_source.getdata(3))
            merge_args = (img_source.mode, (l, l, l, a))  # for RGBA verion of grayscale
            luts += range(256)  # for 1:1 mapping of copied alpha values

        return Image.merge(*merge_args).point(luts)     # Return string of image
Ejemplo n.º 4
0
def image_tint(path: str, tint: str) -> Image:
    src = Image.open(path)
    if src.mode not in ("RGB", "RGBA"):
        raise TypeError(f"Unsupported source image mode: {src.mode}")
    src.load()

    tr, tg, tb = getrgb(tint)
    tl = getcolor(tint, "L")  # tint color's overall luminosity
    if not tl:
        tl = 1  # avoid division by zero
    tl = float(tl)  # compute luminosity preserving tint factors
    sr, sg, sb = map(lambda tv: tv / tl, (tr, tg, tb))  # per component
    # adjustments
    # create look-up tables to map luminosity to adjusted tint
    # (using floating-point math only to compute table)
    luts = (tuple(map(lambda lr: int(lr * sr + 0.5), range(256))) +
            tuple(map(lambda lg: int(lg * sg + 0.5), range(256))) +
            tuple(map(lambda lb: int(lb * sb + 0.5), range(256))))
    l = grayscale(src)  # 8-bit luminosity version of whole image
    if Image.getmodebands(src.mode) < 4:
        merge_args: tuple = (src.mode, (l, l, l)
                             )  # for RGB verion of grayscale
    else:  # include copy of src image's alpha layer
        a = Image.new("L", src.size)
        a.putdata(src.getdata(3))
        merge_args = (src.mode, (l, l, l, a))  # for RGBA verion of grayscale
        luts += tuple(range(256))  # for 1:1 mapping of copied alpha values

    image = Image.merge(*merge_args).point(luts)
    new_image = Image.new("RGBA", image.size,
                          "WHITE")  # Create a white rgba background
    new_image.paste(image, (0, 0), image)
    return new_image
Ejemplo n.º 5
0
def image_tint(src, tint='#ffffff'):
    if Image.isStringType(src):  # file path?
        src = Image.open(src)
    if src.mode not in ['RGB', 'RGBA']:
        raise TypeError('Unsupported source image mode: {}'.format(src.mode))
    src.load()

    tr, tg, tb = getrgb(tint)
    tl = getcolor(tint, "L")  # tint color's overall luminosity
    if not tl: tl = 1  # avoid division by zero
    tl = float(tl)  # compute luminosity preserving tint factors
    sr, sg, sb = map(lambda tv: tv/tl, (tr, tg, tb))  # per component
                                                      # adjustments
    # create look-up tables to map luminosity to adjusted tint
    # (using floating-point math only to compute table)
    luts = (tuple(map(lambda lr: int(lr*sr + 0.5), range(256))) +
            tuple(map(lambda lg: int(lg*sg + 0.5), range(256))) +
            tuple(map(lambda lb: int(lb*sb + 0.5), range(256))))
    l = grayscale(src)  # 8-bit luminosity version of whole image
    if Image.getmodebands(src.mode) < 4:
        merge_args = (src.mode, (l, l, l))  # for RGB verion of grayscale
    else:  # include copy of src image's alpha layer
        a = Image.new("L", src.size)
        a.putdata(src.getdata(3))
        merge_args = (src.mode, (l, l, l, a))  # for RGBA verion of grayscale
        luts += tuple(range(256))  # for 1:1 mapping of copied alpha values

    return Image.merge(*merge_args).point(luts)
Ejemplo n.º 6
0
def image_tint(src,
               tint="#ffffff"):  # From https://stackoverflow.com/a/12310820
    if src.mode not in ["RGB", "RGBA"]:
        raise TypeError("Unsupported source image mode: {}".format(src.mode))
    src.load()

    tr, tg, tb = getrgb(tint)
    tl = getcolor(tint, "L")
    if not tl:
        tl = 1
    tl = float(tl)
    sr, sg, sb = map(lambda tv: tv / tl, (tr, tg, tb))
    luts = (tuple(map(lambda lr: int(lr * sr + 0.5), range(256))) +
            tuple(map(lambda lg: int(lg * sg + 0.5), range(256))) +
            tuple(map(lambda lb: int(lb * sb + 0.5), range(256))))
    l = grayscale(src)
    if Image.getmodebands(src.mode) < 4:
        merge_args = (src.mode, (l, l, l))
    else:
        a = Image.new("L", src.size)
        a.putdata(src.getdata(3))

        luts += tuple(range(256))

    return Image.merge(*merge_args).point(luts)
Ejemplo n.º 7
0
def get_binary_mask(obj_img, bg_img, bbox, new_w, new_h):

    bg_mask = Image.new('RGBA', bg_img.size, getcolor('rgba(0,0,0,0)', 'RGBA'))
    bg_mask.paste(obj_img, bbox, obj_img)
    bg_mask = bg_mask.resize((new_w, new_h))

    mask = np.array(bg_mask)[:, :, -1] / 255
    mask = np.rint(mask).astype(int)

    return mask
Ejemplo n.º 8
0
    def image_tint(image, tint='#ffffff'):
        """
        Function to merge two images without alpha

        Parameters:
        image (str): Path to the image
        tint (str): Hex code for the tint

        Returns:
        Image object for further use
        """
        image = image.convert('RGBA')
        image.load()

        tr, tg, tb = getrgb(tint)
        tl = getcolor(tint, "L")  # tint color's overall luminosity
        tl = 1 if not tl else tl  # avoid division by zero
        tl = float(tl)  # compute luminosity preserving tint factors
        sr, sg, sb = map(lambda tv: tv / tl, (tr, tg, tb))  # per component
        # adjustments
        # create look-up tables to map luminosity to adjusted tint
        # (using floating-point math only to compute table)
        luts = (tuple(map(lambda lr: int(lr * sr + 0.5), range(256))) +
                tuple(map(lambda lg: int(lg * sg + 0.5), range(256))) +
                tuple(map(lambda lb: int(lb * sb + 0.5), range(256))))
        lum = grayscale(image)  # 8-bit luminosity version of whole image
        if Image.getmodebands(image.mode) < 4:
            merge_args = (image.mode, (lum, lum, lum)
                          )  # for RGB verion of grayscale
        else:  # include copy of image image's alpha layer
            a = Image.new("L", image.size)
            a.putdata(image.getdata(3))
            merge_args = (image.mode, (lum, lum, lum, a)
                          )  # for RGBA verion of grayscale
            luts += tuple(range(256))  # for 1:1 mapping of copied alpha values

        return Image.merge(*merge_args).point(luts)
Ejemplo n.º 9
0
from pathlib import Path
from sys import setrecursionlimit

from PIL.ImageColor import getcolor

import bfs, dfs

# Folders
INPUT_FOLDER = Path('mazes')
OUTPUT_FOLDER = Path('solutions')

# Maze Solving Algorithm
ALGORITHM = bfs

# Pixel Color
PATH_COLOR = getcolor('red', 'RGB')
SEARCH_COLOR = getcolor('gray', 'RGB')

# Recursion Limit
increase_recursion_limit = False
if increase_recursion_limit:
    setrecursionlimit(9999)
Ejemplo n.º 10
0
 def get_saturation(self, rgb_tuple):
     try:
         return getcolor('rgb' + repr(rgb_tuple), 'L')
     except:
         print "Bad tuple " + repr(rgb_tuple)
         return 51
Ejemplo n.º 11
0
from PIL import ImageDraw
from PIL.ImageColor import getcolor
config = dict(
    weizuotu = [
        dict(
            name='rectangle',
            kwargs=dict(
                xy=[(x0, y0), (x1, y1)],
                fill=getcolor((r, g, b))
            )
        )
    ]
)

name_to_op = dict(

)
Ejemplo n.º 12
0
        for c in range(args.num_x):
            for i in range(len(lengths)):
                L = lengths[i]
                img = Image.open(join(args.source_path, args.input))
                hL = L / 2
                bbox = (int(cx[c] - hL), int(cy[r] - hL), int(cx[c] + hL),
                        int(cy[r] + hL))

                # Place mask or shape depending on args
                if args.mask is not None:
                    mask = Image.open(join(mask_path, args.mask))
                    mask = mask.resize((L, L))
                    img.paste(mask, bbox, mask)
                else:
                    mask = Image.new('RGBA', (L, L),
                                     getcolor('rgba(0,0,0,0)', 'RGBA'))
                    draw = ImageDraw.Draw(mask)
                    draw.ellipse((0, 0, L, L), fill=COLOR)
                    img.paste(mask, bbox, mask)

                # Get binary mask of object for depth calculations
                bmask = get_binary_mask(mask, img, bbox, in_w, in_h)

                # Save the color image
                img.save(join(color_path, 'color_{}_{}_{}.png'.format(r, c,
                                                                      i)))

                # Convert to tensor
                orig_shape = img.size
                img = img.resize((opts['width'], opts['height']))
                input = pilToTensor(img)
Ejemplo n.º 13
0
 def get_saturation(self, rgb_tuple):
     try:
         return getcolor('rgb'+repr(rgb_tuple), 'L') 
     except:
         print "Bad tuple "+repr(rgb_tuple)
         return 51