-
Book Overview & Buying
-
Table Of Contents
Python Multimedia
By :
Download the file Rotate.png from the Packt website. Alternatively, you can use any supported image file of your choice.
Write the following code in Python interpreter or in a Python file. As always, specify the appropriate path strings for inPath and outPath variables.
1 import Image
2 inPath = "C:\\images\\Rotate.png"
3 img = Image.open(inPath)
4 deg = 45
5 filterOpt = Image.BICUBIC
6 outPath = "C:\\images\\Rotate_out.png"
7 foo = img.rotate(deg, filterOpt)
8 foo.save(outPath)Upon running this code, the output image, rotated by 45 degrees, is saved to the outPath. The filter option Image.BICUBIC ensures highest quality. The next illustration shows the original and the images rotated by 45 and 180 degrees respectively—the original and rotated images.

There is another way to accomplish rotation for certain angles by using the Image.transpose functionality. The following code achieves a 270-degree rotation. Other valid options for rotation are Image.ROTATE_90 and Image...
Change the font size
Change margin width
Change background colour