Drawing WebP with Python Image Library (PIL, Pillow)

  • Posted on: 8 January 2015
  • By: oon
Indonesia Proverb

Contoh kode sederhana untuk menyimpan gambar dalam format WebP [1] menggunakan Python Image Library (Pillow, pengembangan dari PIL).

from PIL import ImageFont, Image, ImageDraw

BGCOLOR = (0x19, 0x76, 0xD2)
WHITE = (0xFF, 0xFF, 0xFF)

fontLI = ImageFont.truetype("Roboto-LightItalic.ttf",40)
fontR  = ImageFont.truetype("Roboto-Regular.ttf",24)
fontT  = ImageFont.truetype("Roboto-Thin.ttf",32)

img=Image.new("RGBA", (640,360), BGCOLOR)
draw = ImageDraw.Draw(img)
draw.text((40, 40),"tidur tak lelap, makan tak kenyang",WHITE,font=fontLI)
draw.text((40, 100),"sangat gelisah (karena bersusah hati,",WHITE,font=fontT)
draw.text((40, 140),"banyak pikiran, dsb)",WHITE,font=fontT)
draw.text((40, 280),"http://kateglo.com/?mod=proverb",WHITE,font=fontR)
draw = ImageDraw.Draw(img)
#img.save("proverb.png")
img.save("proverb.webp","WEBP")

Teks yang ditulis ini adalah sebuah peribahasa (proverb) yang diambil dari situs http://kateglo.com

Untuk menginstall Pillow bisa menggunakan pip Python Package Manager, cukup dengan: pip install Pillow

[1]https://developers.google.com/speed/webp/

Tags: 

Add new comment

Filtered HTML

  • Web page addresses and e-mail addresses turn into links automatically.
  • Allowed HTML tags: <a> <em> <strong> <cite> <blockquote> <code> <ul> <ol> <li> <dl> <dt> <dd>
  • Lines and paragraphs break automatically.

Plain text

  • No HTML tags allowed.
  • Web page addresses and e-mail addresses turn into links automatically.
  • Lines and paragraphs break automatically.
By submitting this form, you accept the Mollom privacy policy.