python opencv图像处理教程 github_python图像处理工具pillow opencv等练习1 旋转

Published: 三 26 十二月 2018

对如下图片旋转

866d754c17f73d3cafd89284c0db6b7c.png

将上面图片逆时针旋转45度,90度,要求图片内容完整。

pillow

#!/usr/bin/env python3

# -*- coding: utf-8 -*-

# https://china-testing.github.io/pil1.html

# https://github.com/china-testing/python-api-tesing/blob/master/practices/pillow/rotate.py

# 项目实战讨论QQ群630011153 144081101

# CreateDate: 2018-12-26

from PIL import Image

im = Image.open("qun.jpg")

print(im.size)

im.show()

im2 = im.rotate(45)

print(im2.size)

im2.show()

im2.save("test1.jpg")

im3 = im.rotate(45, expand=True)

print(im3.size)

im3.show()

im3.save("test2.jpg")

执行结果:

(489, 594)

(489, 594)

(767, 766)

ec74597f751e8f05d2c61e52787bbb94.png

6792864f696926e6f87053b4cc26f6d1.png

注意点:pillow在没有设置expand=True的情况,旋转可能会丢失部分内容。设置expand=True的情况下,则可能增大图片像素。

90度的旋转和45的类似。具体参考代码 https://github.com/china-testing/python-api-tesing/blob/master/practices/pillow/rotate.py。

90度旋转在没有设置expand=True的情况下,图片也是有丢失的。

3179098faa634847cd6359c26eb1e400.png

另外:transpose(Image.ROTATE_90)和im.rotate(90, expand=True)的效果实际是相同。

参考资料:

https://www.toutiao.com/i6637827317458567687/

https://china-testing.github.io/python3_lib_pil.html

旋转图片更多pillow练习

把/home/andrew/code/tmp_photos2的jpg图片旋转270度,放在/home/andrew/code/tmp_photos3

要求实现的命令行界面如下:

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17$ python3 rotate.py -h

usage: rotate.py [-h] [-t TYPE] [-a ANGLE] [--version] src dst

功能:旋转图片

示例: $ python3 rotate.py /home/andrew/code/tmp_photos2 /home/andrew/code/tmp_photos3 -a 270

把/home/andrew/code/tmp_photos2的jpg图片旋转270度,放在/home/andrew/code/tmp_photos3

positional arguments:

src 源目录

dst 目的目录

optional arguments:

-h, --help show this help message and exit

-t TYPE 文件扩展名, 默认为jpg

-a ANGLE 旋转角度,默认为90度,方向都为逆时针。

--version show program's version number and exit

旋转前:

cd6f51c9009a487820c344015e13ad94.png

旋转后

d792679a5e590dd3e03abd7d9ba20f12.png

需求来源: 用户拍的图片人脸未必是头在上,下巴在下面,但是人脸识别的时扶正的识别效果比较好,为此...

参考代码:

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

20

21

22

23

24

25

26

27

28

29

30

31import glob

import os

import argparse

from PIL import Image

import photos

import data_common

description = '''

功能:旋转图片

示例: $ python3 rotate.py /home/andrew/code/tmp_photos2 /home/andrew/code/tmp_photos3 -a 270

'''

parser = argparse.ArgumentParser(description=description,

formatter_class=argparse.RawTextHelpFormatter)

parser.add_argument('src', action="store", help=u'源目录')

parser.add_argument('dst', action="store", help=u'目的目录')

parser.add_argument('-t', action="store", dest="type", default="jpg",

help=u'文件扩展名, 默认为jpg')

parser.add_argument('-a', action="store", dest="angle", default=90, type=int,

help=u'旋转角度,默认为90度,方向都为逆时针。')

parser.add_argument('--version', action='version',

version='%(prog)s1.0 Rongzhong xu 2018 04 26')

options = parser.parse_args()

data_common.check_directory(options.dst)

files = data_common.find_files_by_type(options.src, filetype=options.type)

photos.rotate(files, options.dst, options.angle)

参考资料

opencv

opencv的旋转比pillow复杂,不过好在有imutils辅助,具体代码参见:

https://github.com/china-testing/python-api-tesing/blob/master/practices/cv/rotate.py

本文如遇格式问题,请访问: https://china-testing.github.io/img_rotate.html

wand库旋转的参考: http://docs.wand-py.org/en/0.4.1/guide/transform.html

参考资料

讨论 qq群144081101 567351477

道家技术-手相手诊看相中医等钉钉群21734177 qq群:391441566 184175668 338228106 看手相、面相、舌相、抽签、体质识别。服务费50元每人次起。请联系钉钉或者微信pythontesting

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值