Esempio n. 1
0
def autocontrast(img, prob, mag=0):
    # maximize the image contrst to black and white
    if random.random() < prob:
        enhancer = Contrast(img)
        return enhancer.enhance(4)
    else:
        return img
Esempio n. 2
0
def _do_enh_contrast(c):
    img = editWindow.controlImg()
    if img and c and c!=1:
        enh = Contrast(img)
        img = enh.enhance(c)
        editWindow.updateImage(img) #win.update_img(img, True, label='enh_contrast %f'%c, operation=do_enh_contrast, params=(c,))
    editWindow.unsetCtrl()
Esempio n. 3
0
def contrast(img, prob=0.2, mag=1.3):
    # mag : 0: balck 1: original  3.0 almost max    [0.1, 1.9]
    if random.random() < prob:
        enhancer = Contrast(img)
        return enhancer.enhance(mag)
    else:
        return img
Esempio n. 4
0
def _onContrastChange(value):
    img = editWindow.controlImg(False)
    try:
        val = float(value)
        contr = Contrast(img)
        img = contr.enhance(val)
        editWindow.updateImage(img)
    except:
        pass
Esempio n. 5
0
def _do_set_brightness(vals): #b, c):
    img = editWindow.controlImg()
    if not vals: # rollback
        editWindow.updateImage(editWindow.savedImg)
    if img and vals and vals != (1,1):
        if vals[0] != 1:
            bright = Brightness(img)
            img = bright.enhance(vals[0])

        if vals[1] != 1:
            contr = Contrast(img)
            img = contr.enhance(vals[0])
        editWindow.updateImage(img) #win.update_img(img, True, label='set_brightness %f, %f'%(b, c), operation=do_set_brightness, params=(b,c))
    editWindow.unsetCtrl()
Esempio n. 6
0
def halftone(original, radius=3, border=21, black_and_white=False):

    owidth, oheight = original.size
    original = util.resize_jam_background(original, owidth + border * 2,
                                          oheight + border * 2)
    original = original.convert('RGB')
    width, height = original.size

    if black_and_white:
        bg = (255, 255, 255)
        fg = (0, 0, 0)
    else:
        colours = util.get_dominant_colours(original, 10)
        colours = util.order_colours_by_brightness(colours)
        fg = tuple(random.choice(colours[-6:]))
        bg = tuple(random.choice(colours[:3]))
        if fg == bg:
            bg = (255, 255, 255)
            fg = (0, 0, 0)

        original = Contrast(original).enhance(1.5)

    pix = original.load()

    new = Image.new('RGB', (width, height), bg)

    draw = aggdraw.Draw(new)
    pen = aggdraw.Pen(fg)
    brush = aggdraw.Brush(fg)

    x_incr = 2 * radius
    y_incr = math.sqrt(3) * radius
    for y in xrange(0, height + 1, int(y_incr)):
        odd_offset = radius * (y / int(y_incr) % 2)
        for x in range(odd_offset, width + 1, x_incr):
            avg_gray = util.get_avg_gray(pix, x, y, radius)
            if avg_gray > .9:
                r = radius
                rnd = lambda: 1
            else:
                r = radius * avg_gray
                rnd = lambda: 1 + random.randint(-1, 1) / 5.0
            draw.ellipse((x - r * rnd(), y - r * rnd(), x + r * rnd(), y + r * rnd()), pen, brush)
    draw.flush()

    new = util.centre_crop(new, owidth, oheight)
    new = new.point(lambda p: p - 20)

    return new
Esempio n. 7
0
 def _augment_images(self, images, random_state, parents, hooks):
     result = images
     for i, img in enumerate(images):
         f = random.uniform(1 - self.contrast_changes, 1 + self.contrast_changes)
         im = Image.fromarray(img)
         result[i] = np.array(Contrast(im).enhance(f), dtype="uint8")
     return result
Esempio n. 8
0
def halftone(original, radius=3, border=21, black_and_white=False):
    original = util.resize_jam_background(original, util.WIDTH + border * 2,
                                          util.HEIGHT + border * 2)
    original = original.convert('RGB')
    width, height = original.size

    if black_and_white:
        bg = (255, 255, 255)
        fg = (0, 0, 0)
    else:
        colours = util.get_dominant_colours(original, 10)
        colours = util.order_colours_by_brightness(colours)
        fg = tuple(random.choice(colours[-6:]))
        bg = tuple(random.choice(colours[:3]))
        if fg == bg:
            bg = (255, 255, 255)
            fg = (0, 0, 0)

        original = Contrast(original).enhance(1.5)

    pix = original.load()

    new = Image.new('RGB', (width, height), bg)

    draw = aggdraw.Draw(new)
    pen = aggdraw.Pen(fg)
    brush = aggdraw.Brush(fg)

    x_incr = 2 * radius
    y_incr = math.sqrt(3) * radius
    for y in range(0, height + 1, int(y_incr)):
        odd_offset = radius * (y // int(y_incr) % 2)
        for x in range(odd_offset, width + 1, x_incr):
            avg_gray = util.get_avg_gray(pix, x, y, radius)
            if avg_gray > .9:
                r = radius
                rnd = lambda: 1
            else:
                r = radius * avg_gray
                rnd = lambda: 1 + random.randint(-1, 1) / 5.0
            draw.ellipse((x - r * rnd(), y - r * rnd(), x + r * rnd(), y + r * rnd()), pen, brush)
    draw.flush()

    new = util.centre_crop(new, util.WIDTH, util.HEIGHT)
    new = new.point(lambda p: p - 20)

    return new
Esempio n. 9
0
 def _adjust_contrast(img, factor):
     from PIL.ImageEnhance import Contrast
     from PIL import Image
     # Image.fromarray defaultly supports RGB, not BGR.
     # convert from BGR to RGB
     img = Image.fromarray(img[..., ::-1], mode='RGB')
     contrasted_img = Contrast(img).enhance(factor)
     # convert from RGB to BGR
     return np.asarray(contrasted_img)[..., ::-1]
Esempio n. 10
0
def _adjust(img, alpha, etype):
    if alpha == 0.0:
        return img

    pil_img = toimage(img)

    enhancer = None
    if etype == "brightness":
        enhancer = Brightness(pil_img)
    elif etype == "color":
        enhancer = Color(pil_img)
    elif etype == "contrast":
        enhancer = Contrast(pil_img)

    return fromimage(enhancer.enhance(alpha))
    def contrast(self, image_arr):
        random_contrast = random.uniform(
            self.augmentation_config.contrast_range[0],
            self.augmentation_config.contrast_range[1])

        # Image.fromarray() handles only (x,y) as grayscale shapes, so flatten the array
        image_arr = image_arr.reshape(image_arr.shape[0], image_arr.shape[1])

        image_container = Image.fromarray(image_arr.astype(np.uint8))
        enhanced_image_arr = np.array(
            Contrast(image_container).enhance(random_contrast))

        # Reshape array again to Keras grayscale format (x, y, 1)
        enhanced_image_arr = enhanced_image_arr.reshape(
            enhanced_image_arr.shape + (1, ))

        return enhanced_image_arr.astype(np.float32)
Esempio n. 12
0
 def f(img: PILImage) -> PILImage:
     return Contrast(img).enhance(factor)
Esempio n. 13
0
 def _prepare(self):
     bwimg = self.orig.convert("L")
     return Contrast(autocontrast(bwimg, 10)).enhance(contrast).point(
         self._digitize)
import os
from PIL import Image
from PIL.ImageEnhance import Color, Brightness, Contrast, Sharpness

index_prefix = 0
for color_factor in [0.5, 0.8, 1.2, 1.5]:
    for bright_factor in [0.5, 0.8, 1.2, 1.5]:
        for contrast_factor in [0.5, 0.8, 1.2, 1.5]:
            for sharp_factor in [0.2, 0.5, 2.0, 3.0]:
                index_prefix += 1
                for ori_name in os.listdir(
                        os.path.join('..', 'NameCardReal', 'JPEGImages')):
                    with Image.open(
                            os.path.join('..', 'NameCardReal', 'JPEGImages',
                                         ori_name)) as im:
                        new_name = str(index_prefix) + ori_name
                        im = Color(im).enhance(color_factor)
                        im = Brightness(im).enhance(bright_factor)
                        im = Contrast(im).enhance(contrast_factor)
                        im = Sharpness(im).enhance(sharp_factor)
                        im.save(os.path.join('JPEGImages', new_name))
                print(color_factor, bright_factor, contrast_factor,
                      sharp_factor)
Esempio n. 15
0
def adjust_colors(map):
    from PIL.ImageEnhance import Brightness, Contrast
    return Contrast(Brightness(map).enhance(ADJ_BRIGHTNESS)).enhance(
        ADJ_CONTRAST).convert('L')
Esempio n. 16
0
 def jitter_brightness(self):
     '''jitter the brightness of img'''
     if np.random.randint(0, 2) == 0:
         self.img = Contrast(self.img).enhance(np.random.uniform(.5, 2.2))
     if np.random.randint(0, 2) == 0:
         self.img = Brightness(self.img).enhance(np.random.uniform(.5, 1.5))