使用Python進行數位影像處理__Python

來源:互聯網
上載者:User

PIL(Python Imaging Library),是Python平台上的影像處理標準庫。功能非常強大,API卻簡單易用。
由於PIL僅支援到Python2.7,加上年久失修,於是一群志願者在PIL的基礎上建立了相容的版本,名叫Pillow,支援最新的Python3.x,又加入了許多新的特性。 — 廖雪峰

1. 安裝Pillow

在命令列使用pip安裝即可。

pip install pillow

官方文檔:
Pillow官方文檔 2. 操作映像 2.1 映像的開啟、旋轉和顯示

將映像旋轉90°,顯示

from PIL import Imageim = Image.open("Middlebury_02_noisy_depth.PNG")im.rotate(45).show()
2.2 映像的儲存

為目前的目錄中的PNG檔案建立一個128*128的縮圖。

from PIL import Imageimport glob,ossize = 128,128for infile in glob.glob("*.PNG"):    file,ext = os.path.splitext(infile)    im = Image.open(infile)    im.thumbnail(size)    im.save(file + ".thumbnail","PNG")

官網參考 2.3 同時展示多張圖片

Display this image.

Image.show(title=None,command=None)

This method is mainly intended for debugging purposes.
On windows, it saves the image to a temporary BMP file and uses the standard BMP display utility to show it (usually Paint).

Parameters: title:Optional title to use for the image window,where possible command:command used to show the image

這個任務對於opencv和Matlab都很簡單。
opencv的實現:在一個視窗中顯示兩張圖片
Matlab的實現:用subplot即可。

那麼用python如何做到這一點兒呢。網上找了很多方法,感覺均是雲裡霧裡的,直到我看到了下面這句話:(福音的柑橘)

在 python 中除了用 opencv,也可以用 matplotlib 和 PIL 這兩個庫操作圖片。本人偏愛 matpoltlib,因為它的文法更像 matlab。

在matplotlib中,一個Figure對象可以包含多個子圖(Axes),可以使用subplot()快速繪製。
matplotlib繪製多個子圖——subplot

#!/usr/bin/env python3        # -*- coding: utf-8 -*-  import matplotlib.pyplot as pltimport numpy as npdef f(t):    return np.exp(-t) * np.cos(2 * np.pi * t)if __name__=='__main__':    t1 = np.arange(0, 5, 0.1)    t2 = np.arange(0, 5, 0.02)    plt.figure(12)    plt.subplot(221)    plt.plot(t1, f(t1), 'bo', t2, f(t2), 'r--')    plt.subplot(222)    plt.plot(t2, np.cos(2 * np.pi * t2), 'r--')    plt.subplot(212)    plt.plot([1,2,3,4],[1,4,9,16])    plt.show()

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.