溫馨提示×

溫馨提示×

您好,登錄后才能下訂單哦!

密碼登錄×
登錄注冊×
其他方式登錄
點(diǎn)擊 登錄注冊 即表示同意《億速云用戶服務(wù)條款》

python圖像處理基本操作有哪些

發(fā)布時(shí)間:2021-06-08 14:36:01 來源:億速云 閱讀:275 作者:小新 欄目:開發(fā)技術(shù)

這篇文章主要介紹python圖像處理基本操作有哪些,文中介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們一定要看完!

一、PIL庫對圖像的基本操作

1、讀取圖片

PIL網(wǎng)上有很多介紹,這里不再講解。直接操作,讀取一張圖片,將其轉(zhuǎn)換為灰度圖像,并打印出來。

from  PIL  import Image
import matplotlib.pyplot as plt
pil_im = Image.open("empire.jpeg")
pil_image = pil_im.convert("L")
plt.gray()
plt.imshow(pil_image)
plt.show()

輸出如下所示:

python圖像處理基本操作有哪些

2、轉(zhuǎn)換圖片格式

PIL可以將圖像保存為多種格式,下面將PNG格式文件保存為JPG格式:

from PIL import Image
import glob
import os
filelist = glob.glob("E:/pythonProject1/filelist/*.png")
for infile in filelist:
    outfile = os.path.splitext(infile)[0]+'.jpg'
 
    if infile  != outfile:
        try:
            Image.open(infile).save(outfile)
        except IOError:
            print("cannot convert", infile)

輸出結(jié)果如下所示:

python圖像處理基本操作有哪些

3、輸出文件夾中所有圖片的文件名列表

import os
def get_imlist(path):
    """返回目錄中所有JPG圖像的文件名列表"""
    return [os.path.join(path,f)for f in os.listdir(path) if f.endswith('.jpg')]
print(get_imlist("E:/pythonProject1/filelist/"))

輸出為文件名列表

二、Matplotlib

1、繪制圖像、點(diǎn)和線

from PIL import Image
from pylab import *
 
#讀取圖像到數(shù)組中
im = array(Image.open("empire.jpeg"))
 
#繪制圖像
imshow(im)
 
#一些點(diǎn)
x = [100, 100, 400, 400]
y = [200, 500, 200, 500]
 
#使用紅色星狀標(biāo)記繪制點(diǎn)
plot(x, y)#默認(rèn)為藍(lán)色實(shí)線
# plot(x, y, 'r*')#紅色星狀標(biāo)記
# plot(x, y, 'go-')#帶有圓圈標(biāo)記的綠線
# plot(x, y, 'ks')#帶有正方形標(biāo)記的黑色虛線
 
#繪制連接前三個(gè)點(diǎn)的線
plot(x[:3], y[:3])
axis('off')
 
#添加標(biāo)題,顯示繪制的圖像
titles = ['empire']
plt.title = titles
show()

上面的代碼首先繪制出原始圖像,然后在 x 和 y 列表中給定點(diǎn)的 x 坐標(biāo)和 y 坐標(biāo)上繪制出紅色星狀標(biāo)記點(diǎn),最后在兩個(gè)列表表示的前兩個(gè)點(diǎn)之間繪制一條線段。該例子的繪制結(jié)果下圖:

python圖像處理基本操作有哪些

2、圖像輪廓和直方圖

繪制輪廓需要對每個(gè)坐標(biāo) [x, y] 的像素值施加同一個(gè)閾值,所以首先需要將圖像灰度化,這里用 PIL 的 convert() 方法將圖像轉(zhuǎn)換成灰度圖像。圖像的直方圖用來表征該圖像像素值的分布情況。

from PIL import Image
from pylab import *
 
# 讀取圖像到數(shù)組中
im = array(Image.open("empire.jpeg").convert('L'))
 
#創(chuàng)建一個(gè)圖像
figure()
#不使用顏色信息
gray()
#在原點(diǎn)的左上角顯示輪廓圖像
contour(im, origin = 'image')#檢測圖像輪廓
axis('equal')
axis('off')
show()
#新建一個(gè)圖像
figure
hist(im.flatten(), 128)#繪制圖像直方圖
show()

圖像輪廓圖輸出如下所示:

python圖像處理基本操作有哪些

輸出圖像直方圖如下所示:

python圖像處理基本操作有哪些

3、交互式標(biāo)注

在一幅圖像中標(biāo)記一些點(diǎn),或者標(biāo)注一些訓(xùn)練數(shù)據(jù)。PyLab 庫中的 ginput() 函數(shù)就可以實(shí)現(xiàn)交互式標(biāo)注。在圖像點(diǎn)擊三次,則程序會自動將這3個(gè)點(diǎn)的坐標(biāo)點(diǎn)[x, y]保存到x列表里。

from PIL import Image
from pylab import *
 
im = array(Image.open("empire.jpeg"))
imshow(im)
print("please click 3 points")
x = ginput(3)
print("you clicked",x)
show()

三、Numpy

1、圖像數(shù)組表示

對于圖像數(shù)據(jù),下面的例子闡述了這一點(diǎn)

from PIL import Image
import numpy as np
 
im = np.array(Image.open("empire.jpeg"))
print(im.shape,im.dtype)

輸出為:
(1024, 683, 3) uint8

 每行的第一個(gè)元組表示圖像數(shù)組的大?。ㄐ小⒘?、顏色通道),緊接著的字符串表示數(shù)組元素的數(shù)據(jù)類型。因?yàn)閳D像通常被編碼成無符號八位整數(shù)(uint8),載入圖像并將其轉(zhuǎn)換到數(shù)組中,數(shù)組的數(shù)據(jù)類型為“uint8”。

2、灰度變換

對圖像進(jìn)行灰度變換,如下所示:

from PIL import Image
import numpy as np
 
im = np.array(Image.open("empire.jpeg"))
print(im.shape,im.dtype)
 
from PIL import Image
from matplotlib.pylab import plt
from numpy import *
 
im1 = array(Image.open('empire.jpeg').convert('L'))
im2 = 255 - im1 #對圖像進(jìn)行反向處理
im3 = (100.0/255) * im1 + 100 #將圖像值變換到100-200之間
im4 = 255.0 * (im1/255) ** 2 #對圖像像素值求平方后得到的圖像
 
images = [im1, im2, im3, im4]
titles = ["f(x) = x", "f(x) = 255 - x", "f(x) = (100/255)*x +100", "f(x) = 255*(x/255)^2"]
#輸出圖中的最大像素值和最小像素值
print(int(im1.min()),int(im1.max()))
print(int(im2.min()),int(im2.max()))
print(int(im3.min()),int(im3.max()))
print(int(im4.min()),int(im4.max()))
 
for i in range(4):
    plt.subplot(2, 2, i+1)#2行2列,按編號順序排列
    plt.imshow(images[i])#顯示圖像
    plt.title(titles[i])#顯示標(biāo)題
    plt.gray()
    # plt.xticks([])
    # plt.yticks([])
    plt.axis('equal')
    plt.axis('off')
plt.show()

輸出接入如下所示:

python圖像處理基本操作有哪些

以上是“python圖像處理基本操作有哪些”這篇文章的所有內(nèi)容,感謝各位的閱讀!希望分享的內(nèi)容對大家有幫助,更多相關(guān)知識,歡迎關(guān)注億速云行業(yè)資訊頻道!

向AI問一下細(xì)節(jié)

免責(zé)聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀點(diǎn)不代表本網(wǎng)站立場,如果涉及侵權(quán)請聯(lián)系站長郵箱:is@yisu.com進(jìn)行舉報(bào),并提供相關(guān)證據(jù),一經(jīng)查實(shí),將立刻刪除涉嫌侵權(quán)內(nèi)容。

AI