def augment_image(self, image):
        '''
		Turns one image into a list of 160, 32x32 greyscale images.
		Inputs:
			image: The image to be augmented as a PIL Image object.
		Outputs:
			A list of 160 PIL Image objects, each 32x32 and greyscale.
		'''
        # downsample the image
        img_resized = image.resize((96, 96), Image.BILINEAR)

        # reverse the image
        imgs_reversed = [img_resized.copy(), mirror(img_resized.copy())]

        # brighten and darken the image
        imgs_enhanced = []
        enhance_amounts = [0.5, 0.75, 1.0, 1.25, 1.5]
        for img_reversed in imgs_reversed:
            img_enhancer = Brightness(img_reversed)
            for enhance_amount in enhance_amounts:
                imgs_enhanced.append(img_enhancer.enhance(enhance_amount))

        # scan along the image to create several sub-images at 32x32
        imgs_scanned = []
        for img_enhanced in imgs_enhanced:
            for i in range(4):
                for j in range(4):
                    imgs_scanned.append(
                        img_enhanced.crop(
                            (16 * i, 16 * j, 32 + 16 * i, 32 + 16 * j)))

        return imgs_scanned
示例#2
0
def _onBrightnessChange(value):
    img = editWindow.controlImg(False)
    try:
        val = float(value)
        bright = Brightness(img)
        img = bright.enhance(val)
        editWindow.updateImage(img)
    except:
        pass
示例#3
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()
示例#4
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 synthesis(job):
    image = Image.new('RGB', options.size)
    bg = Image.open(join(job.input_directory,
                         options.filename)).convert('LA').convert('RGB')
    bg = Brightness(bg).enhance(0.5)
    image.paste(bg)
    return image
示例#6
0
def starter_window(root: Tk, options: StarterConfig,
                   events: List[startlists.Event]) -> Starter:
    """Displays the starter simulator window."""

    if options.get_bool("fullscreen"):
        root.resizable(False, False)
        root.overrideredirect(True)  # hide titlebar
        root.attributes(
            '-zoomed',
            True)  # on Linux only root.state("zoomed") on windows/macos
    else:
        # Simulator is varible size
        root.resizable(True, True)
    content = Starter(root, options, events)
    content.grid(column=0, row=0, sticky="news")
    # FIXME: Background images need fixing
    if options.get_str("image_bg") != "":
        try:
            image = Image.open(options.get_str("image_bg"))
            content.bg_image(
                Brightness(image).enhance(options.get_float("image_bright")),
                options.get_str("image_scale"))
        except FileNotFoundError:
            pass
        except UnidentifiedImageError:
            pass

    def return_to_settings(_) -> None:
        root.unbind('<Double-1>')
        content.destroy()
        root.state('normal')  # Un-maximize
        settings_window(root, options)

    root.bind('<Double-1>', return_to_settings)
    return content
示例#7
0
 def _adjust_brightness(img, factor):
     # adjust the brightness of image using
     # PIL.ImageEnhance.Brightness
     from PIL.ImageEnhance import Brightness
     from PIL import Image
     img = Image.fromarray(img)
     brightened_img = Brightness(img).enhance(factor)
     return np.asarray(brightened_img)
示例#8
0
def synthesis(job):
    im, size, topleft, bottomright = getImageCluster(options.lat, options.lon,
                                                     options.deltalat,
                                                     options.deltalon,
                                                     options.zoom)
    filename = 'map.png'
    im.save(filename)
    job.register_file(filename)
    cropim, size, topleft, bottomright = crop(im,
                                              size,
                                              topleft,
                                              bottomright,
                                              options.crop,
                                              resize=options.resize)
    cropim = cropim.convert('LA').convert('RGB')
    cropim = Brightness(cropim).enhance(0.5)
    filename = 'crop.png'
    cropim.save(filename)
    job.register_file(filename)
    return size, topleft, bottomright
示例#9
0
    def write_brightness_normalized(self, file: SkyPiFile, stream):
        if self.target_brightness is None:
            stream.write(file.path.read_bytes())
            return

        with Image.open(file.path) as im:
            crop_area = [
                self.BRIGHTNESS_BORDER_SIZE[0] * im.width,
                self.BRIGHTNESS_BORDER_SIZE[1] * im.height,
                (1 - self.BRIGHTNESS_BORDER_SIZE[0]) * im.width,
                (1 - self.BRIGHTNESS_BORDER_SIZE[1]) * im.height,
            ]

            stat = ImageStat.Stat(im.crop(crop_area))
            r, g, b = stat.mean
            brightness = math.sqrt(0.299 * (r**2) + 0.587 * (g**2) + 0.114 *
                                   (b**2))
            factor = self.target_brightness / brightness
            enhancer = Brightness(im)
            enhancer.enhance(factor).save(stream, format='JPEG', quality=90)
示例#10
0
    def _add_background(self):
        bg_file = self._config.get_str("image_bg")
        if bg_file == "":
            return  # Nothing to do (no image)

        # Scale the image
        try:
            bg_img = Image.open(bg_file)
        except FileNotFoundError:
            return
        except UnidentifiedImageError:
            return
        mode = self._config.get_str("image_scale")
        alg = Image.BICUBIC
        if mode == "stretch":
            bg_img = bg_img.resize(self.size, alg)
        elif mode == "fit":
            factor = min(self.size[0] / bg_img.size[0],
                         self.size[1] / bg_img.size[1])
            bg_img = bg_img.resize(
                (int(bg_img.size[0] * factor), int(bg_img.size[1] * factor)),
                alg)
        elif mode == "cover":
            factor = max(self.size[0] / bg_img.size[0],
                         self.size[1] / bg_img.size[1])
            bg_img = bg_img.resize(
                (int(bg_img.size[0] * factor), int(bg_img.size[1] * factor)),
                alg)

        # Generate the crop bounding box such that the image is in the center
        # and the size matches the size of the main image
        left = (bg_img.size[0] - self.size[0]) // 2
        right = bg_img.size[0] - left
        top = (bg_img.size[1] - self.size[1]) // 2
        bottom = bg_img.size[1] - top
        bbox = (left, top, right, bottom)
        bg_img = bg_img.crop(bbox)

        # Adjust brightness and overlay it on the main image
        bg_img = Brightness(bg_img).enhance(
            self._config.get_float("image_bright"))
        self._i.alpha_composite(bg_img)
示例#11
0
 def f(img: PILImage) -> PILImage:
     return Brightness(img).enhance(factor)
示例#12
0
#total =60*5
total = 60 * 20
#for i in range(60*3-30, 60*30+30) :
for i in range(total):
    #f =Image.new(mode="RGBA", size=[720,480], color=(0xff, 0xff, 0xff, 1))
    #f =Image.new(mode="RGBA", size=[1280,720], color=(0xff, 0xff, 0xff, 1))
    f = mai.crop(box=(int(x), int(y), int(x + 1280), int(y + 720)))
    x += deplacement[0]
    y += deplacement[1]
    sz = mai.size[0] * 0.999, mai.size[1] * 0.999
    if i % 5 == 0:
        mai = mai.resize((int(sz[0]), int(sz[1])))
    a = i % (60 * 3)
    if a < 20:
        a = abs(a - 10)
        g = Brightness(f)
        g = g.enhance(a / 10)
        #g.show()
        #g =Image.new(mode="RGBA", size=[1280,720], color=(0x02, 0x02, 0x02, int(0xff-0xff*a/30)))
        #f.paste(g,box=(0,0,1280,720))
        g.save(open("cache/myabout/myabout%04d.png" % i, "bw"))
        continue
    #dr =ImageDraw(f)
    #s,ms =divmod(i,60)
    #dr.text(xy=[10,10], text="%02d:%02d.%04d"%(0,s,ms%60), fill=0)
    #dr.text(xy=[10,30], text="(%02d/%02d)"%(i,total), fill=0)

    f.save(open("cache/myabout/myabout%04d.png" % i, "bw"))
    print("%d/%d" % (i, total))

exit()
示例#13
0
    async def vcr(self, ctx: Context, *, url: str):
        # TODO support attachments
        buffer = BytesIO()
        resp = await asks.get(url, stream=True)

        async for chunk in resp.body:
            buffer.write(chunk)

        async with ctx.channel.typing:
            async with spawn_thread():
                with Image.open(buffer) as image:
                    filter = np.random.choice(range(3), p=[0.7, 0.2, 0.1])

                    if filter == SCANLINES:
                        image = add_scanlines(image)
                    elif filter == NOISE:
                        image = add_noise(image)
                    else:
                        image = add_scanlines(image)
                        image = add_noise(image)

                    Brightness(image).enhance(2.5)

                    # hoo boy

                    text = np.random.choice(['PLAY', '  PAUSE'], p=[0.8, 0.2])
                    font = truetype('VCR_OSD_MONO.ttf',
                                    size=int(min(image.size) / 10))

                    start = datetime.datetime(1980, 1, 1, 0, 0)
                    now = datetime.datetime.utcnow()

                    # https://stackoverflow.com/a/8170651/7581432
                    random_date = start + datetime.timedelta(
                        seconds=random.randint(
                            0, int((now - start).total_seconds())))

                    topleft_text = antialiased_text(text,
                                                    font,
                                                    image.width,
                                                    image.height,
                                                    offset_x=1 / 30,
                                                    offset_y=1 / 15)
                    image.paste(topleft_text, (0, 0), mask=topleft_text)

                    draw = Draw(image)
                    if text == 'PLAY':
                        width, height = font.getsize(text)
                        offset_x = width + image.width * (1 / 30) * 1.5
                        offset_y = image.height * (1 / 15)

                        draw.polygon([(offset_x, offset_y),
                                      (offset_x, offset_y + height),
                                      (offset_x + sqrt(height**2 -
                                                       (height / 2)**2),
                                       offset_y + height / 2)],
                                     fill=(255, 255, 255))
                    else:
                        _, height = font.getsize('  ')
                        offset_x = image.width * (1 / 35)
                        offset_y = image.height * (1 / 15)

                        part = (height - offset_x / 2) / 8

                        draw.rectangle(
                            [(offset_x, offset_y + part),
                             (offset_x + 3 * part, offset_y - part + height)],
                            fill=(255, 255, 255))
                        draw.rectangle(
                            [(offset_x + 5 * part, offset_y + part),
                             (offset_x + 8 * part, offset_y - part + height)],
                            fill=(255, 255, 255))

                    # This is a nasty hack but oh well
                    time, date = random_date.strftime('%H:%M|%b. %d %Y').split(
                        '|')
                    wrap_width = len(date)
                    botleft_text = antialiased_text(
                        time.ljust(wrap_width + 1) + date,
                        font,
                        image.width,
                        image.height,
                        offset_x=1 / 35,
                        offset_y=13 / 15,
                        wrap_width=wrap_width)
                    image.paste(botleft_text, (0, 0), mask=botleft_text)

                    buffer = save_image(image, format=image.format)
        await ctx.channel.messages.upload(buffer,
                                          filename='shoutouts.' + image.format)
示例#14
0
class Generator:
    def __init__(self,
                 labels_file='labels.csv',
                 batch_size=32,
                 val_split=0.2,
                 train=True,
                 scale=3,
                 min_area=1200,
                 flip=False,
                 rotate=False,
                 translate=False,
                 brightness=False):

        self.batch_size = batch_size  #batch size
        self.min_area = min_area  #the minimum area of a car in the image in pixels
        self.lock = threading.Lock()  #for multithreading on next()
        self.image_IDs = None  #the image IDs in the dataframe
        self.labels = None  #.csv file
        self.val_split = val_split  #validatin split fraction
        self.train = train  #float - whether this is a training or validation generator
        self.scale = tuple([int(x / scale) for x in (1200, 1920)])

        # call to set up labels
        self.setup_data(labels_file)
        self.num_batches = int(np.ceil(len(self.image_IDs) /
                                       self.batch_size))  #number of batches

        #variables holding temporary data
        self.img = None  #the current input image
        self.curr_img_info = None  #all label info of the current image
        self.mask = None  #mask of current image

        #image augmentation
        self.flip = flip
        self.rotate = rotate
        self.brightness = brightness
        self.translate = translate
        self.non = lambda s: s if s < 0 else None
        self.mom = lambda s: max(0, s)

        #for frame shift of images in list img_dir
        self.start = 0
        self.end = self.batch_size

        #init output batch
        self.X_batch = np.zeros((batch_size, self.scale[0], self.scale[1], 3),
                                dtype=np.uint8)
        self.y_batch = np.zeros((batch_size, self.scale[0], self.scale[1], 1),
                                dtype=np.uint8)

    def setup_data(self, labels_file):
        '''get and verify images are in directory, labels are correct '''
        labels = pd.read_csv(labels_file)
        self.labels = labels[labels.Label != 'Pedestrian']
        self.image_IDs = self.labels.Frame.unique()  #get all jpg file names

        #create training split
        if self.train:
            self.image_IDs = self.image_IDs[:int(
                len(self.image_IDs) * (1 - self.val_split))]
        else:
            self.image_IDs = self.image_IDs[
                int(len(self.image_IDs) * (1 - self.val_split)):]

        seed(0)
        shuffle(self.image_IDs)
        assert len(self.image_IDs) > 0, 'no images found, check directory'

    def read_image(self, img_name):
        '''read in the image and color correction'''
        self.curr_img_info = self.labels[self.labels.Frame ==
                                         img_name]  #all IDs in that jpg
        im_path = os.path.join('../crowdai', img_name)
        self.img = image.load_img(im_path)

    def get_area(self, x):
        return (x.ymax - x.xmax) * (x.ymin - x.xmin)

    def create_mask(self):
        '''create a vehicle mask from the image'''
        self.mask = np.zeros(shape=(1200, 1920))

        for i in range(self.curr_img_info.shape[0]):
            vehicle_ID = self.curr_img_info.iloc[i]

            #thresold small cars out
            area = self.get_area(vehicle_ID)
            if area > self.min_area:
                self.mask[vehicle_ID.xmax:vehicle_ID.ymax,
                          vehicle_ID.xmin:vehicle_ID.ymin] = 1

        self.mask = np.expand_dims(self.mask, axis=2)
        self.mask = image.array_to_img(self.mask)

    def flip_img(self):
        '''50/50 odds to randomly flip the image'''
        if np.random.randint(0, 2):
            self.img = self.img.transpose(Image.FLIP_LEFT_RIGHT)
            self.mask = self.mask.transpose(Image.FLIP_LEFT_RIGHT)

    def rotate_img(self):
        '''50/50 odds to rotate the image and mask by +/- 6 degrees'''
        if np.random.randint(0, 2):
            angle = np.random.random() * 12 - 6
            self.img = self.img.rotate(angle)
            self.mask = self.mask.rotate(angle)

    def translate_img(self):
        '''50/50 odds to shift the img asn mask
            by +/- 50 pixels in x and/or y direction'''
        if np.random.randint(0, 2):
            # (ax+by+c, dx+ey+f)
            # c, f = left/right, up.down
            a, b, d, e = 1, 0, 0, 1
            c, f = np.random.uniform(-50, 50), np.random.uniform(-50, 50)
            self.img = self.img.transform(self.img.size, Image.AFFINE,
                                          (a, b, c, d, e, f))
            self.mask = self.mask.transform(self.img.size, Image.AFFINE,
                                            (a, b, c, d, e, f))

    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))

    def process(self):
        '''resize and convert PIL images to arrays'''
        self.img = self.img.resize(self.scale[::-1])
        self.mask = self.mask.resize(self.scale[::-1])

        #scale from 255 -> 1 to match output of old generator
        self.img = image.img_to_array(self.img)
        self.mask = image.img_to_array(self.mask).reshape(self.scale) / 255

    def __next__(self):
        '''Yields data tensor of size [batch_size, 1200, 1920, 1], 
        label tensor of size [batch_size, 1]. GPU compatible. '''

        #lock and release threads at iteration execution
        with self.lock:
            for i in range(self.num_batches):
                img_batch_files = self.image_IDs[self.start:self.end]

                for j, img_name in enumerate(img_batch_files):

                    self.read_image(img_name)
                    self.create_mask()

                    #augment image and mask
                    if self.flip: self.flip_img()
                    if self.translate: self.translate_img()
                    if self.rotate: self.rotate_img()
                    if self.brightness: self.jitter_brightness()

                    #resize and cvt to array
                    self.process()

                    #for debugging
                    #print (j, img_name)
                    # plt.imshow(self.img)
                    # plt.show()
                    # plt.imshow(self.mask)
                    # plt.show()

                    self.X_batch[j, :, :, :] = self.img.reshape(
                        self.scale[0], self.scale[1], 3)
                    self.y_batch[j, :, :, :] = self.mask.reshape(
                        self.scale[0], self.scale[1], 1)

                #clip last batch
                if i == self.num_batches - 1:
                    self.X_batch = self.X_batch[:j, :, :, :]

                #increment images for next iteration
                self.start += self.batch_size
                self.end += self.batch_size

                return self.X_batch, self.y_batch

    def __iter__(self):
        return self
示例#15
0
def brightness(img, prob, mag=1):
    # mag [0, 1, >>>]
    if random.random() < prob:
        return Brightness(img).enhance(mag)
    else:
        return img
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)
示例#17
0
def adjust_colors(map):
    from PIL.ImageEnhance import Brightness, Contrast
    return Contrast(Brightness(map).enhance(ADJ_BRIGHTNESS)).enhance(
        ADJ_CONTRAST).convert('L')
示例#18
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))