コード例 #1
0
 def thumbnail_created_callback(sender, **kwargs):
     try:
         file_path = sender.path
         file_webp_path = file_path.rsplit('.', 1)[0] + ".webp"
         webp.save_image(Image.open(file_path), file_webp_path, quality=80)
     except:
         pass
コード例 #2
0
ファイル: test_webp.py プロジェクト: mostlyuseful/pywebp
 def test_greyscale_save_image(self):
     width = 256
     height = 64
     img1 = Image.new('L', (width, height))
     with TemporaryDirectory() as tmpdir:
         file_name = os.path.join(tmpdir, 'image.webp')
         with pytest.raises(webp.WebPError) as ex_info:
             webp.save_image(img1, file_name)
         assert str(ex_info.value) == 'unsupported image mode: L'
コード例 #3
0
ファイル: test_webp.py プロジェクト: theFong/pywebp
 def test_greyscale_save_image(self):
     width = 256
     height = 64
     img1 = Image.new('P', (width, height))
     with TemporaryDirectory() as tmpdir:
         file_name = os.path.join(tmpdir, 'image.webp')
         with self.assertRaises(webp.WebPError) as context:
             webp.save_image(img1, file_name)
         self.assertEqual(str(context.exception),
                          'unsupported image mode: P')
コード例 #4
0
def to_webp_optimized(field, width, height):
    # Using PIL to open the image and then resizing it
    img = Image.open(field)
    resized_img = resize_img(img, width, height)
    # Getting the file path we want to save the webp image to and setting the right extension
    file_path = change_extension_of_path(path=field.path, extension=".webp")
    # Using webp module to convert and save the image
    webp.save_image(resized_img, file_path, quality=70)
    # Save image to cloudinary and return the url
    return save_to_cloudinary(file_path)['secure_url']
コード例 #5
0
ファイル: test_webp.py プロジェクト: mostlyuseful/pywebp
    def test_image_palette_opaque(self, image_bars_palette_opaque):
        with TemporaryDirectory() as tmpdir:
            file_name = os.path.join(tmpdir, 'image.webp')

            assert image_bars_palette_opaque.mode == 'P'
            webp.save_image(image_bars_palette_opaque, file_name, lossless=True)
            dec_img = webp.load_image(file_name, 'RGB')

            actual = np.asarray(dec_img, dtype=np.uint8)
            image_bars_rgb = image_bars_palette_opaque.convert('RGB')
            expected = np.asarray(image_bars_rgb, dtype=np.uint8)
            assert_array_equal(actual, expected)
コード例 #6
0
 def handle(self, *args, **options):
     for relative_root, dirs, files in os.walk(
             os.path.join(settings.STATIC_ROOT, 'images')):
         for file_ in files:
             if '.png' in file_ or '.jpg' in file_ or '.gif' in file_:
                 try:
                     file_path = os.path.join(relative_root, file_)
                     file_webp_path = file_path.rsplit('.', 1)[0] + ".webp"
                     print(file_path)
                     webp.save_image(Image.open(file_path),
                                     file_webp_path,
                                     quality=80)
                 except:
                     pass
コード例 #7
0
ファイル: test_webp.py プロジェクト: mostlyuseful/pywebp
    def test_image_simple(self):
        width = 256
        height = 64
        img = Image.new('RGB', (width, height))
        draw = ImageDraw.Draw(img)
        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))

        with TemporaryDirectory() as tmpdir:
            file_name = os.path.join(tmpdir, 'image.webp')

            webp.save_image(img, file_name, lossless=True)
            dec_img = webp.load_image(file_name, 'RGB')

            actual = np.asarray(dec_img, dtype=np.uint8)
            expected = np.asarray(img, dtype=np.uint8)
            assert_array_equal(actual, expected)