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

Time for action – rotating

  1. Download the file Rotate.png from the Packt website. Alternatively, you can use any supported image file of your choice.
  2. 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)
  3. 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.
  4. 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.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.

主站蜘蛛池模板: 甘泉县| 扶沟县| 明水县| 剑阁县| 荆州市| 鲁甸县| 江油市| 静安区| 元阳县| 镇远县| 万载县| 会昌县| 沙河市| 凌云县| 榆树市| 富民县| 盘锦市| 南皮县| 丰顺县| 东安县| 南宫市| 阿拉善右旗| 嘉义市| 澎湖县| 台山市| 太谷县| 舒兰市| 闽清县| 鄂温| 集贤县| 城固县| 中阳县| 萝北县| 南平市| 井冈山市| 毕节市| 仲巴县| 观塘区| 青川县| 商水县| 宁阳县|