Skip to content

Instantly share code, notes, and snippets.

@PyYoshi
Created June 8, 2011 03:25
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save PyYoshi/1013712 to your computer and use it in GitHub Desktop.
Save PyYoshi/1013712 to your computer and use it in GitHub Desktop.
python-webmで実際に変換してみる。PIL、python-webmそれとlibwebp.soが必要。いまのところpython-webmはWinには対応してない。 ちょっといじればできそうだけども^^;
#!/usr/bin/env python
# -*- coding: utf-8 -*-
from webm.encode import WebPEncoder
from webm.handlers import BitmapHandler
import Image
import os
# 初期値
IMAGE_WIDTH = 1200
IMAGE_HEIGHT = 1600
fpath = os.path.join( os.path.dirname( __file__ ), "test.jpg" )
# ここにImageを使用。Imageでいろいろいじったあと最終的に以下の行の処理を行えば問題ないとおもう
im = BitmapHandler(bytearray(Image.open(fpath).tostring() ),
BitmapHandler.RGB,
IMAGE_WIDTH,
IMAGE_HEIGHT,
IMAGE_WIDTH * 3)
encoder = WebPEncoder()
webp_obj = encoder.encodeRGB(im)
webp_file = open(os.path.join( os.path.dirname( __file__ ), "test.webp" ), 'wb')
webp_file.write(webp_obj.data)
webp_file.close()
# パフォーマンス微妙。 リサイズとかしなければconvertとか付属のcwebp、dwebp使ったほうがいいと思うよ。
# それとwebp自体まだ開発中でライブラリが突然使えなくなることあるからアレ。
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment