700字范文,内容丰富有趣,生活中的好帮手!
700字范文 > python opencv图像处理教程 github_python图像处理工具pillow opencv等练习1 旋转

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

时间:2023-06-09 03:02:27

相关推荐

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

Published: 三 26 十二月

对如下图片旋转

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

pillow

#!/usr/bin/env python3

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

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

# /china-testing/python-api-tesing/blob/master/practices/pillow/rotate.py

# 项目实战讨论QQ群630011153 144081101

# CreateDate: -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)

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

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

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

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

参考资料:

/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

旋转前:

旋转后

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

参考代码:

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 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辅助,具体代码参见:

/china-testing/python-api-tesing/blob/master/practices/cv/rotate.py

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

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

参考资料

讨论 qq群144081101 567351477

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

本内容不代表本网观点和政治立场,如有侵犯你的权益请联系我们处理。
网友评论
网友评论仅供其表达个人看法,并不表明网站立场。