Beispiel #1
0
def main():
    # scaling up & down
    src = np.array(Image.open(root / 'fig/castle.jpg'))
    h, w, c = src.shape
    padding = np.zeros((h, 4, c), dtype=np.uint8)
    scale_down = seam_carving.resize(src, (w - 200, h))
    scale_up = seam_carving.resize(src, (w + 200, h))
    merged = np.hstack((src, padding, scale_down, padding, scale_up))
    Image.fromarray(merged).show()

    # forward energy vs backward energy
    src = np.array(Image.open(root / 'fig/bench.jpg'))
    h, w, c = src.shape
    padding = np.zeros((h, 4, c), dtype=np.uint8)
    backward = seam_carving.resize(src, (w - 200, h))
    forward = seam_carving.resize(src, (w - 200, h), energy_mode='forward')
    merged = np.hstack((src, padding, backward, padding, forward))
    Image.fromarray(merged).show()

    # object removal
    src = np.array(Image.open(root / 'fig/beach.jpg'))
    h, w, c = src.shape
    mask = np.array(Image.open(root / 'fig/beach_girl.png').convert('L'))
    dst = seam_carving.remove_object(src, mask)
    padding = np.zeros((h, 4, c), dtype=np.uint8)
    merged = np.hstack((src, padding, dst))
    Image.fromarray(merged).show()
Beispiel #2
0
    def work(self):
        h, w, _ = img.shape

        if h > w:
            rew = w
            reh = int(w * 1.414)
            if reh > h:
                rew = h // 1.414
                reh = h
        else:
            reh = h
            rew = h // 1.414
            if rew > w:
                reh = int(w * 1.414)
                rew = w
        print('height, width = ', h, w, '=>', reh, rew)
        out = seam_carving.resize(
            img,
            (rew, reh),
            energy_mode='backward',  # Choose from {backward, forward}
            order='width-first',  # Choose from {width-first, height-first}
            keep_mask=None)
        out = self.kuwahara(out)
        out = self.puttext(out, texts, point, font_path, font_size, color)
        self.work_ended = True
        cv2.imwrite("./img/poster.png", out)
Beispiel #3
0
def main():
    parser = argparse.ArgumentParser()
    parser.add_argument('src', type=str)
    parser.add_argument('-o', dest='dst', type=str, default='a.jpg')
    parser.add_argument('--keep', type=str, default=None)
    parser.add_argument('--drop', type=str, default=None)
    parser.add_argument('--dw', type=int, default=0)
    parser.add_argument('--dh', type=int, default=0)
    parser.add_argument('--energy', type=str, default='backward',
                        choices=['backward', 'forward'])
    parser.add_argument('--order', type=str, default='width-first',
                        choices=['width-first', 'height-first', 'optimal'])
    args = parser.parse_args()

    try:
        print('Loading source image from {}'.format(args.src))
        src = np.array(Image.open(args.src))

        drop_mask = None
        if args.drop is not None:
            print('Loading drop_mask from {}'.format(args.drop))
            drop_mask = np.array(Image.open(args.drop).convert('L'))

        keep_mask = None
        if args.keep is not None:
            print('Loading keep_mask from {}'.format(args.keep))
            keep_mask = np.array(Image.open(args.keep).convert('L'))

        print('Performing seam carving...')
        start = time.time()
        if drop_mask is not None:
            dst = seam_carving.remove_object(src, drop_mask, keep_mask)
        else:
            src_h, src_w, _ = src.shape
            dst = seam_carving.resize(src, (src_w + args.dw, src_h + args.dh),
                                      args.energy, args.order, keep_mask)
        print('Done at {:.4f} second(s)'.format(time.time() - start))

        print('Saving output image to {}'.format(args.dst))
        Path(args.dst).parent.mkdir(parents=True, exist_ok=True)
        Image.fromarray(dst).save(args.dst)
    except Exception as e:
        print(e)
        exit(1)
Beispiel #4
0
def resize_image(image_bytes, w_pct, h_pct):
    image = Image.open(BytesIO(image_bytes))
    final_buffer = BytesIO()

    src = np.array(image)
    src_h, src_w, _ = src.shape
    dst_h, dst_w = int(src_h * (h_pct / 100)), int(src_w * (w_pct / 100))

    dst = seam_carving.resize(
        src,
        (dst_w, dst_h),
        energy_mode='backward',  # Choose from {backward, forward}
        order='width-first',  # Choose from {width-first, height-first}
        keep_mask=None)
    dst = Image.fromarray(dst)
    dst.save(final_buffer, "jpeg")

    final_buffer.seek(0)

    return final_buffer, src_h, src_w, dst_h, dst_w
Beispiel #5
0
def processImages(width, imgDir, outputDir, seam_carve=True):
    '''takes in all images in imgDir, and resizes them with seam carving
       to the dimensions width x width, via forward energy.
    '''
    assert outputDir is not None and imgDir is not None

    count = 0
    start = time.time()
    prev = time.time()

    allImg = sorted(os.listdir(imgDir))

    for filename in allImg:
        if filename in os.listdir(outputDir):
            continue
        # read the image
        full_path = imgDir + filename
        im = cv2.imread(full_path)

        # downsize the image
        im = seam_carving.resize(im, width=DOWNSIZE_WIDTH)

        # get image resolution
        h, w = im.shape[:2]

        # make the larges square possible for a particular image
        # TODO ignore widths for now
        # dy is number of vertical seams to extract,
        # dx is number of horizontal seams to extract
        dy = None
        dy = None

        # TODO this makes no sense
        if h > w:
            dy = 0
            dx = w - h
        else:
            dy = h - w
            dx = 0
        assert dy != None and dx != None

        # for now we assume the mask is None
        #print('   processing image', full_path, '...')

        if USE_SEAM_CARVING:
            output = seam_carving.seam_carve(im, dx, dy, mask=None, vis=False)
        else:
            output = cv2.resize(im, (WIDTH, WIDTH),
                                interpolation=cv2.INTER_AREA)

        cv2.imwrite(outputDir + filename, output)

        # print out progress
        count += 1
        now = time.time()
        if count % 50 == 0:
            print('   processed image', full_path, 'in', int(now - prev), 's')

            print('-- time elapse:', int(prev - start), 's, processed:', count,
                  'average time:', int((prev - start) / count), 's',
                  int(count / len(allImg) * 100), 'percent complete')
        prev = now
Beispiel #6
0
def main():
    parser = argparse.ArgumentParser()
    parser.add_argument('src', type=str)
    parser.add_argument('-o', dest='dst', type=str, required=True)
    parser.add_argument('--keep', type=str, default=None)
    parser.add_argument('--drop', type=str, default=None)
    parser.add_argument('--dw', type=int, default=0)
    parser.add_argument('--dh', type=int, default=0)
    parser.add_argument('--energy',
                        type=str,
                        default='backward',
                        choices=['backward', 'forward'])
    parser.add_argument('--order',
                        type=str,
                        default='width-first',
                        choices=['width-first', 'height-first', 'optimal'])
    parser.add_argument('--face', action='store_true')
    args = parser.parse_args()

    try:
        print('Loading source image from {}'.format(args.src))
        src = np.array(Image.open(args.src))

        drop_mask = None
        if args.drop is not None:
            print('Loading drop_mask from {}'.format(args.drop))
            drop_mask = np.array(Image.open(args.drop).convert('L'))

        keep_mask = None
        if args.keep is not None:
            print('Loading keep_mask from {}'.format(args.keep))
            keep_mask = np.array(Image.open(args.keep).convert('L'))

        if args.face:
            # Face detection using face++ API
            with open(Path(__file__).parent / 'api_config.json') as f:
                api_config = json.load(f)
            with open(args.src, 'rb') as f:
                img_data = f.read()
            image_base64 = base64.b64encode(img_data)
            url = 'https://api-us.faceplusplus.com/facepp/v3/detect'
            data = {
                'api_key': api_config['api_key'],
                'api_secret': api_config['api_secret'],
                'image_base64': image_base64
            }
            response = requests.post(url, data)
            data = response.json()

            src_h, src_w, _ = src.shape
            if keep_mask is None:
                keep_mask = np.zeros((src_h, src_w), dtype=np.bool)

            for face in data['faces']:
                rect = face['face_rectangle']
                x1 = rect['left']
                y1 = rect['top']
                w = rect['width']
                h = rect['height']
                keep_mask[y1:y1 + h, x1:x1 + w] = True

        print('Performing seam carving...')
        start = time.time()
        if drop_mask is not None:
            dst = seam_carving.remove_object(src, drop_mask, keep_mask)
        else:
            src_h, src_w, _ = src.shape
            dst = seam_carving.resize(src, (src_w + args.dw, src_h + args.dh),
                                      args.energy, args.order, keep_mask)
        print('Done at {:.4f} second(s)'.format(time.time() - start))

        print('Saving output image to {}'.format(args.dst))
        Path(args.dst).parent.mkdir(parents=True, exist_ok=True)
        Image.fromarray(dst).save(args.dst)
    except Exception as e:
        print(e)
        exit(1)
Beispiel #7
0
def resize_image_gif(image_bytes, w_pct, h_pct, image_format):
    # Get the PIL Image
    image = Image.open(BytesIO(image_bytes))

    # Resize in case the image is too big
    if image_format == "gif":
        image.thumbnail((750, 750))
    else:
        image.thumbnail((1250, 1250))

    # Get the output buffer
    final_buffer = BytesIO()

    # Get the numpy image
    src = np.array(image)

    # Get the image sizes
    start_h, start_w, _ = src.shape

    # Get the desired sizes
    end_w, end_h = int(start_w * (w_pct / 100)), int(start_h * (h_pct / 100))

    # The step : positive if growing, negative if not.
    step_h = GIF_STEP if end_h < start_h else -GIF_STEP

    # Find the biggest of the two, for the gif size
    big_h = max(start_h, end_h)

    # Same for width
    step_w = GIF_STEP if end_w < start_w else -GIF_STEP
    big_w = max(start_w, end_w)

    images = [
        pad_image_to(Image.fromarray(src), big_h, big_w, start_h, start_w)
    ]
    durations = [BASE_DURATION * 5]

    # Resize height
    for curr_h in range(start_h, end_h, step_h):
        # Width not yet resized
        curr_w = start_w

        src = seam_carving.resize(
            src,
            (curr_w, curr_h),
            energy_mode='backward',  # Choose from {backward, forward}
            order='width-first',  # Choose from {width-first, height-first}
            keep_mask=None)

        images.append(
            pad_image_to(Image.fromarray(src), big_h, big_w, curr_h, curr_w))
        durations.append(BASE_DURATION)

    # Resize width

    # The height is resized now
    curr_h = end_h
    for curr_w in range(start_w, end_w, step_w):
        src = seam_carving.resize(
            src,
            (curr_w, curr_h),
            energy_mode='backward',  # Choose from {backward, forward}
            order='width-first',  # Choose from {width-first, height-first}
            keep_mask=None)

        images.append(
            pad_image_to(Image.fromarray(src), big_h, big_w, curr_h, curr_w))
        durations.append(BASE_DURATION)

    durations[-1] = BASE_DURATION * 15

    images[0].save(final_buffer,
                   format=image_format,
                   save_all=True,
                   append_images=images[1:],
                   duration=BASE_DURATION,
                   loop=0)

    final_buffer.seek(0)
    return final_buffer, start_h, start_w, end_h, end_w