Exemple #1
0
def split_frames(image_obj, need_frame=None):
    image_seq = ImageSequence.all_frames(image_obj)
    image_arr_last = [np.asarray(image_seq[-1])
                      ] if -1 in need_frame and len(need_frame) > 1 else []
    image_arr = [
        np.asarray(item) for i, item in enumerate(image_seq)
        if (i in need_frame or need_frame == [-1])
    ]
    image_arr += image_arr_last
    return image_arr
Exemple #2
0
 def __getitem__(self, index):
     with self._zip.open(self._zip.filelist[self.range[index]]) as f:
         seq = ImageSequence.all_frames(Image.open(f))
         ranges = self.params["blurs"]
         blurs, frames = seq[:len(ranges)], seq[len(ranges):]
         if self.blurs:
             ranges = [ranges[i] for i in self.blurs]
             blurs = [blurs[i] for i in self.blurs]
         frames += [Image.new("RGBA", blurs[0].size)] * self.dummy_frames
         return blurs, frames, ranges
Exemple #3
0
def all_frames(image_obj):
    if isinstance(image_obj, list):
        image_obj = image_obj[0]
    stream = io.BytesIO(image_obj)
    pil_image = Image.open(stream)
    image_seq = ImageSequence.all_frames(pil_image)
    array_seq = [np.asarray(im.convert("RGB")) for im in image_seq]
    # [1::2]
    bytes_arr = [cv2.imencode('.png', img_arr)[1] for img_arr in array_seq]
    split_flag = b'\x99\x99\x99\x00\xff\xff999999.........99999\xff\x00\x99\x99\x99'
    return split_flag.join(bytes_arr).split(split_flag)
    def test_all_frames(self):
        # Test a single image
        im = Image.open("Tests/images/iss634.gif")
        ims = ImageSequence.all_frames(im)

        self.assertEqual(len(ims), 42)
        for i, im_frame in enumerate(ims):
            self.assertFalse(im_frame is im)

            im.seek(i)
            self.assert_image_equal(im, im_frame)

        # Test a series of images
        ims = ImageSequence.all_frames([im, hopper(), im])
        self.assertEqual(len(ims), 85)

        # Test an operation
        ims = ImageSequence.all_frames(im,
                                       lambda im_frame: im_frame.rotate(90))
        for i, im_frame in enumerate(ims):
            im.seek(i)
            self.assert_image_equal(im.rotate(90), im_frame)
Exemple #5
0
    def test_all_frames(self):
        # Test a single image
        with Image.open("Tests/images/iss634.gif") as im:
            ims = ImageSequence.all_frames(im)

            assert len(ims) == 42
            for i, im_frame in enumerate(ims):
                assert im_frame is not im

                im.seek(i)
                assert_image_equal(im, im_frame)

            # Test a series of images
            ims = ImageSequence.all_frames([im, hopper(), im])
            assert len(ims) == 85

            # Test an operation
            ims = ImageSequence.all_frames(
                im, lambda im_frame: im_frame.rotate(90))
            for i, im_frame in enumerate(ims):
                im.seek(i)
                assert_image_equal(im.rotate(90), im_frame)
Exemple #6
0
def image_transform(origin_img_path):
    '''
        input the path to the origin image and check its format and size.
        generate new image that satisfy the need of ocr. 
        return the path to the new image.
    '''
    im = Image.open(origin_img_path)
    name, file_format = os.path.splitext(origin_img_path)

    if file_format == 'gif':
        # get the first image in this gif
        im = ImageSequence.all_frames(im)[0]
    h, w = im.size
    h = max(16, h)
    h = min(4096, h)
    w = max(16, w)
    w = min(4096, w)
    im = im.resize((h, w))
    output_path = name + '.png'
    print("Save local image:%s, resize: " % output_path, im.size)
    im = im.convert('RGBA')
    im.save(output_path)
    return output_path
def _main():

    for filename in tqdm(sorted(os.listdir(DATA_RAW_DIR))[:64]):
        mat = re_data_raw_filename.match(filename)
        assert mat is not None, filename

        label = mat.group(1).lower()
        serial = mat.group(2)

        dirname = os.path.join(DATA_FRAME_DIR, "%s_%s" % (label, serial))
        if not os.path.exists(dirname):
            os.mkdir(dirname)

        filepath = os.path.join(DATA_RAW_DIR, filename)
        im = Image.open(filepath)
        w, h = im.size

        frames = ImageSequence.all_frames(im)
        assert len(frames) == 16, len(frames)

        for ix, fim in enumerate(frames):
            filepath = os.path.join(dirname, "%02d.png" % ix)
            fim.save(filepath)

        for i in range(4):
            j = 4 * i + 3

            if i == 0:
                fim = frames[j]

                fim = fim.convert('L')
                fim = morph_dilate_cross(fim, ksize=3, iterations=1)
                fim = morph_erode_cross(fim, ksize=3, iterations=1)
                fim = denoise(fim, ksize=5, threshold=20, iterations=2)
                fim = fim.filter(ImageFilter.EDGE_ENHANCE_MORE)
                fim = fim.convert('1')

            else:
                M1 = np.array(frames[j - 4], dtype=np.int32)
                M2 = np.array(frames[j], dtype=np.int32)
                fim = Image.fromarray(0xff - np.abs(M2 - M1).astype(np.uint8))

                fim = fim.convert('L')
                fim = morph_erode_cross(fim, ksize=3, iterations=1)
                fim = morph_dilate_cross(fim, ksize=3, iterations=1)
                fim = denoise(fim, ksize=5, threshold=20, iterations=1)
                fim = fim.filter(ImageFilter.EDGE_ENHANCE_MORE)
                fim = fim.convert('1')

            filepath = os.path.join(dirname, "c%d_%s_raw.png" % (i, label[i]))
            fim.save(filepath)

            fim = crop(fim)

            filepath = os.path.join(dirname, "c%d_%s.png" % (i, label[i]))
            fim.save(filepath)

            filepath = os.path.join(DATA_CROP_DIR, "%s_c%d_%s_%s.png" % (label, i, label[i], serial))
            fim.save(filepath)

        im.close()
Exemple #8
0
fp_in = "Bird1/*.jpg"
fp_out = "./Bird1.gif"

# https://pillow.readthedocs.io/en/stable/handbook/image-file-formats.html#gif
img, *imgs = [Image.open(f) for f in sorted(glob.glob(fp_in))]
img.save(fp=fp_out,
         format='GIF',
         append_images=imgs,
         save_all=True,
         duration=30,
         loop=0)

# Reduce frames
im = Image.open(fp_out)
index = 1
for frame in ImageSequence.all_frames(im):
    frame = frame.convert('RGB')
    frame.save(f"gif{index}.jpg", quality=100)
    index = index + 1

frame_limit = 60

n = int(index / frame_limit) + 1
images = []
for i in range(1, index):
    if i % n == 0:
        images.append(imageio.imread(f'gif{i}.jpg'))
imageio.mimsave(fp_out, images, duration=0.1)

for i in range(1, index):
    f = 'gif' + str(i) + '.jpg'