官术网_书友最值得收藏!

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.

  1. 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)
  2. 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)
  3. The following illustration shows the output of the preceding code when the image is flipped horizontally and vertically.
  4. The same effect can be achieved using the ImageOps module. To flip the image horizontally, use ImageOps.mirror, and to flip the image vertically, use ImageOps.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.

主站蜘蛛池模板: 静安区| 濉溪县| 万全县| 澄城县| 新邵县| 昌乐县| 丹寨县| 鹿泉市| 鄂州市| 额尔古纳市| 巫山县| 安乡县| 郁南县| 普洱| 丹棱县| 柳河县| 鄂州市| 潼关县| 大新县| 呈贡县| 邮箱| 棋牌| 麻江县| 绥德县| 台南县| 精河县| 慈溪市| 浮梁县| 元谋县| 黄骅市| 霍山县| 柳林县| 福安市| 洪湖市| 齐齐哈尔市| 东源县| 嘉义市| 凉城县| 洪洞县| 繁昌县| 上林县|