python圖像處理工具pillow opencv等練習1 旋轉

對如下圖片旋轉

將上面圖片逆時針旋轉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)

注意點:pillow在沒有設置expand=True的情況,旋轉可能會丟失部分內容。設置expand=True的情況下,則可能增大圖片像素。

90度的旋轉和45的類似。具體參考代碼 https://github.com/china-testing/python-api-tesing/blob/master/practices/pillow/rotate.py

90度旋轉在沒有設置expand=True的情況下,圖片也是有丟失的。

另外: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

參考資料:python圖像處理庫

要求實現的命令行界面如下:

#!sh

$ 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

旋轉前:

旋轉後

需求來源: 用戶拍的圖片人臉未必是頭在上,下巴在下面,但是人臉識別的時扶正的識別效果比較好,爲此...

參考代碼:

#!python
import 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)s 1.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

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章