def write_animation(self, filename_out): if not self.frames: return webp.save_images(self.frames, filename_out, fps=self.fps, quality=self.quality, preset=self.preset)
def test_anim_image_palette(self, image_bars_palette): with TemporaryDirectory() as tmpdir: file_name = os.path.join(tmpdir, 'image.webp') assert image_bars_palette.mode == 'P' webp.save_images([image_bars_palette] * 3, file_name, lossless=True) dec_imgs = webp.load_images(file_name, 'RGBA') actual = np.asarray(dec_imgs[0], dtype=np.uint8) image_bars_rgba = image_bars_palette.convert('RGBA') expected = np.asarray(image_bars_rgba, dtype=np.uint8) assert_array_equal(actual, expected)
def test_anim_simple_resample(self): width = 256 height = 64 img1 = Image.new('RGB', (width, height)) draw = ImageDraw.Draw(img1) draw.rectangle([0, 0, width-1, height-1], fill=(0, 0, 255)) draw.rectangle([0, 0, (width/4-1), height-1], fill=(255, 0, 0)) img2 = Image.new('RGB', (width, height)) draw = ImageDraw.Draw(img2) draw.rectangle([0, 0, width-1, height-1], fill=(0, 0, 255)) draw.rectangle([0, 0, (width/4-1), height-1], fill=(0, 255, 0)) imgs = [img1, img1, img2, img2] with TemporaryDirectory() as tmpdir: file_name = os.path.join(tmpdir, 'anim.webp') webp.save_images(imgs, file_name, fps=4, lossless=True) dec_imgs = webp.load_images(file_name, 'RGBA', fps=4) assert len(dec_imgs) == 4
def test_anim_simple(self): imgs = [] width = 256 height = 64 for i in range(4): img = Image.new('RGBA', (width, height)) draw = ImageDraw.Draw(img) draw.rectangle([0, 0, width-1, height-1], fill=(0, 0, 255)) x = i * (width/4) draw.rectangle([x, 0, x + (width/4-1), height-1], fill=(255, 0, 0)) imgs.append(img) with TemporaryDirectory() as tmpdir: file_name = os.path.join(tmpdir, 'anim.webp') webp.save_images(imgs, file_name, fps=4, lossless=True) dec_imgs = webp.load_images(file_name, 'RGBA') assert len(dec_imgs) == 4 for dec_img, img in zip(dec_imgs, imgs): actual = np.asarray(dec_img, dtype=np.uint8) expected = np.asarray(img, dtype=np.uint8) assert_array_equal(actual, expected)
from PIL import Image import re fp_in = "./*.png" fp_out = "out.gif" def atoi(text): return int(text) if text.isdigit() else text def natural_keys(text): return [atoi(c) for c in re.split(r'(\d+)', text)] img, *imgs = [ Image.open(f) for f in sorted(glob.glob(fp_in), key=natural_keys) ] # the crate i use to extract images works # not very well, therefore I do some make up on # all resulting images. # unpredictably, this inverts the image. It's fine by me - but what the hell? for value in imgs: pixels = value.load() for i in range(img.size[0]): for j in range(img.size[1]): if pixels[i, j] > (200, 200, 200): pixels[i, j] = (0, 0, 0) webp.save_images(imgs, "anim.webp", lossless=False, fps=30)