- Python Multimedia
- Ninad Sathaye
- 282字
- 2021-08-03 15:27:40
Time for action – rotating
- 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
andoutPath
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 optionImage.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 areImage.ROTATE_90
andImage.ROTATE_180
.import Image inPath = "C:\\images\\Rotate.png" img = Image.open(inPath) outPath = "C:\\images\\Rotate_out.png" foo = img.transpose(Image.ROTATE_270) foo.save(outPath)
What just happened?
In the previous section, we used Image.rotate
to accomplish rotating an image by the desired angle. The image filter Image.BICUBIC
was used to obtain better quality output image after rotation. We also saw how Image.transpose
can be used for rotating the image by certain angles.
Flipping
There are multiple ways in PIL to flip an image horizontally or vertically. One way to achieve this is using the Image.transpose
method. Another option is to use the functionality from the ImageOps
module . This module makes the image-processing job even easier with some ready-made methods. However, note that the PIL documentation for Version 1.1.6 states that ImageOps
is still an experimental module.
推薦閱讀
- vtiger CRM Beginner's Guide
- Wordpress 3 Complete
- Premiere Pro 2022從新手到高手
- 超簡(jiǎn)單的攝影后期書
- VSTO 3.0 for Office 2007 Programming
- 中文版AutoCAD基礎(chǔ)培訓(xùn)教程
- Photoshop CC完全自學(xué)教程:從入門到實(shí)踐(全新版)
- 行攝 Photoshop CC后期修片高手之道(第2版)
- Cassandra High Performance Cookbook
- 電腦寫作與定制五筆(第2版)
- 老郵差 Photoshop數(shù)碼照片處理技法 圖層篇(修訂版)
- 技藝非凡Photoshop+SAI繪畫創(chuàng)作技法揭秘
- 新手學(xué)UG NX 8.0輔助設(shè)計(jì)
- 全鏈路UI設(shè)計(jì):創(chuàng)意思維+項(xiàng)目實(shí)戰(zhàn)+就業(yè)指導(dǎo)
- CorelDRA WX8中文版從入門到精通