Cookbook

creating a color of red background JPEG image

from pgmagick.api import Image

img = Image((300, 200), 'red')
img.write('bg-red.jpg')
_images/bg-red.jpg

creating a transparency PNG image

from pgmagick.api import Image

img = Image((300, 200), 'transparent')
img.write('transparent.png')
_images/transparent.png

creating a gradient image

from pgmagick.api import Image

img = Image((300, 200), 'gradient:#ffffff-#000000')
img.write('gradient.png')
_images/gradient.png

draw text

from pgmagick.api import Image

img = Image((300, 200))
img.annotate('Hello World')
img.write('helloworld.png')
_images/helloworld.png

斜め45度に傾いた文字を描く draw text of rotated 45 degrees ——————————-

from pgmagick.api import Image

img = Image((300, 200))
img.annotate('Hello World', angle=45)
img.write('helloworld45.png')
_images/helloworld45.png

draw text of 2byte code

# coding: utf-8
from pgmagick.api import Image

img = Image((300, 200))
img.font("/usr/share/fonts/truetype/ttf-japanese-gothic.ttf")
img.annotate('Hello World')
img.annotate('ようこそpgmagickへ!!')
img.write('japanese-text.png')
_images/japanese-text.png

scaling a image

from pgmagick.api import Image

img = Image('gradient.png')
img.scale(0.5)
img.write('halfscale.png')
_images/halfscale.png
from pgmagick.api import Image

img = Image('gradient.png')
img.scale((150, 100), 'lanczos')
img.write('halfscale2.png')
_images/halfscale2.png

scaling a JPEG image

from pgmagick import Image, Blob

img = Image(Blob(open('lena_std.jpg').read()), Geometry(200, 200))
img.scale('200x200')
img.write('lena_scale.jpg')
_images/lena_std.jpg _images/lena_scale.jpg

get image size

from pgmagick.api import Image

img = Image((300, 200))
print img.columns(), img.rows()
print img.width, img.height
$ python size.py
300 200
300 200

sharpen filtering

from pgmagick.api import Image

img = Image('lena.jpg')
img.sharpen(1)
img.write('lena_sharpen1.jpg')
_images/lena.jpg _images/lena_sharpen1.jpg

blur filtering

from pgmagick.api import Image

img = Image('lena.jpg')
img.blur(10, 5)
img.write('lena_blur.jpg')
_images/lena.jpg _images/lena_blur.jpg

Edge extraction

from pgmagick.api import Image

img = Image('lena.jpg')
img.edge(2)
img.write('lena_edge.jpg')
_images/lena.jpg _images/lena_edge.jpg

removing a EXIF information

import sys
from pgmagick import Image, Blob

blob = Blob()
img = Image('exif-sample.jpg')
img.profile("*", blob)
img.write('exif-strip.jpg')
_images/exif-sample.jpg _images/exif-strip.jpg