- Python Multimedia
- Ninad Sathaye
- 271字
- 2021-08-03 15:27:40
Time for action – flipping
Imagine that you are building a symmetric image using a bunch of basic shapes. To create such an image, an operation that can flip (or mirror) the image would come in handy. So let's see how image flipping can be accomplished.
- Write the following code in a Python source file.
1 import Image 2 inPath = "C:\\images\\Flip.png" 3 img = Image.open(inPath) 4 outPath = "C:\\images\\Flip_out.png" 5 foo = img.transpose(Image.FLIP_LEFT_RIGHT) 6 foo.save(outPath)
- In this code, the image is flipped horizontally by calling the
transpose
method. To flip the image vertically, replace line 5 in the code with the following:foo = img.transpose(Image.FLIP_TOP_BOTTOM)
- The following illustration shows the output of the preceding code when the image is flipped horizontally and vertically.
- The same effect can be achieved using the
ImageOps
module. To flip the image horizontally, useImageOps.mirror
, and to flip the image vertically, useImageOps.flip
.import ImageOps # Flip image horizontally foo1 = ImageOps.mirror(img) # Flip image vertically foo2 = ImageOps.flip(img)
What just happened?
With the help of example, we learned how to flip an image horizontally or vertically using Image.transpose
and also by using methods in class ImageOps
. This operation will be applied later in this book for further image processing such as preparing composite images.
Capturing screenshots
How do you capture the desktop screen or a part of it using Python? There is ImageGrab
module in PIL. This simple line of code will capture the whole screen.
img = ImageGrab.grab()
Where, img
is an instance of class Image
.
However, note that in PIL Version 1.1.6, the ImageGrab
module supports screen grabbing only for Windows platform.
- Adobe創意大學After Effects CS5 產品專家認證標準教材
- 中文版Illustrator CC實用教程
- Spring Security 3
- Microsoft SharePoint 2010 Power User Cookbook: SharePoint Applied
- UG NX 9.0中文版 基礎教程 (UG工程師成才之路)
- Photoshop圖形圖像處理實用教程
- Premiere視頻編輯項目教程:Premiere Pro 2020(微課版)
- Photoshop網店美工實例教程(第2版 全彩微課版)
- SolidWorks 2018快速入門及應用技巧
- Indesign平面排版技術應用
- 中文版3ds Max/VRay效果圖制作完全自學教程(實例培訓教材版)
- 用Multisim玩轉電路仿真
- Configuring IPCop Firewalls: Closing Borders with Open Source
- Joomla! E/Commerce with VirtueMart
- 零基礎學Procreate萌物繪