自拍偷在线精品自拍偷,亚洲欧美中文日韩v在线观看不卡

Python 處理圖片的十個(gè)庫(kù)

開發(fā)
今天一起來聊一聊Python 處理圖片的十個(gè)庫(kù)。

Matplotlib

類型: 數(shù)據(jù)可視化庫(kù)

功能: 創(chuàng)建線圖、柱狀圖、散點(diǎn)圖、直方圖、餅圖等多種靜態(tài)圖表,支持子圖布局、色彩映射、圖例、注解等高級(jí)定制。

適用場(chǎng)景: 科研論文、報(bào)告、數(shù)據(jù)分析展示。

import matplotlib.pyplot as plt
import numpy as np
x = np.linspace(0, 2 * np.pi, 400)
y = np.sin(x ** 2)
plt.figure(figsize=(8, 6))
plt.plot(x, y)
plt.title('Sine of Squared X')
plt.xlabel('X', fontsize=14)
plt.ylabel('Y', fontsize=14)
plt.grid(True)
plt.show()

PIL/Pillow

類型: 圖像處理庫(kù)

功能: 打開、修改、保存多種格式的圖像文件,支持像素級(jí)操作、裁剪、旋轉(zhuǎn)、縮放、顏色空間轉(zhuǎn)換、濾鏡應(yīng)用等。

適用場(chǎng)景: 基本圖像編輯、批處理、Web開發(fā)中的圖像預(yù)處理。

from PIL import Image, ImageDraw, ImageFont
# 創(chuàng)建一個(gè)空白圖像
img = Image.new('RGB', (500, 300), color='white')
# 創(chuàng)建繪圖對(duì)象
draw = ImageDraw.Draw(img)
# 寫入文字
font = ImageFont.truetype('arial.ttf', size=50)
text = "Hello, World!"
text_width, text_height = font.getsize(text)
draw.text((img.width // 2 - text_width // 2, img.height // 2 - text_height // 2), text=text, fill='black', fnotallow=font)
# 保存圖像
img.save('hello_world.png')

字體下載鏈接:https://font.chinaz.com/120308013581.htm

NumPy

類型: 數(shù)值計(jì)算庫(kù)

功能: 提供高效的多維數(shù)組對(duì)象(ndarray),是處理圖像數(shù)據(jù)的基礎(chǔ)。圖像通常被表示為NumPy數(shù)組,便于進(jìn)行數(shù)學(xué)運(yùn)算和算法處理。

適用場(chǎng)景: 圖像數(shù)據(jù)的基本操作、與圖像處理庫(kù)配合使用。

import numpy as np
import matplotlib.pyplot as plt
# 創(chuàng)建一個(gè)5x5的隨機(jī)數(shù)組
img_array = np.random.randint(0, 256, size=(5, 5), dtype=np.uint8)
# 顯示數(shù)組作為灰度圖像
plt.imshow(img_array, cmap='gray')
plt.colorbar()
plt.title('Random 5x5 Image Array')
plt.show()

scikit-image (skimage)

類型: 計(jì)算機(jī)視覺庫(kù)

功能: 提供大量圖像處理算法,包括濾波、邊緣檢測(cè)、形態(tài)學(xué)操作、圖像分割、特征提取、色彩空間轉(zhuǎn)換等。

適用場(chǎng)景: 學(xué)術(shù)研究、工業(yè)應(yīng)用中的圖像分析與處理。

from skimage import io
from skimage.filters import sobel
from matplotlib import pyplot as plt


# 讀取圖像
image = io.imread('example.jpg', as_gray=True)# 替換成需要處理的圖片
# 應(yīng)用 Sobel 邊緣檢測(cè)
edges = sobel(image)
# 顯示原圖和邊緣檢測(cè)結(jié)果
fig, ax = plt.subplots(nrows=1, ncols=2, figsize=(10, 5))
ax[0].imshow(image, cmap='gray')
ax[0].set_title('Original Image')
ax[1].imshow(edges, cmap='gray')
ax[1].set_title('Sobel Edge Detection')
plt.tight_layout()
plt.show()

OpenCV

類型: 計(jì)算機(jī)視覺庫(kù)

功能: 高性能的圖像與視頻處理、物體檢測(cè)與識(shí)別、跟蹤、立體視覺、機(jī)器學(xué)習(xí)算法等。

適用場(chǎng)景: 實(shí)時(shí)視頻處理、復(fù)雜圖像分析、深度學(xué)習(xí)應(yīng)用。

import cv2
import numpy as np
# 讀取圖像
image = cv2.imread('example.jpg')
# 轉(zhuǎn)為灰度圖像
gray = cv2.cvtColor(image, cv2.COLOR_BGR2GRAY)
# 應(yīng)用Canny邊緣檢測(cè)
edges = cv2.Canny(gray, threshold1=100, threshold2=200)
# 顯示原圖和邊緣檢測(cè)結(jié)果
cv2.imshow('Original Image', image)
cv2.imshow('Canny Edge Detection', edges)
cv2.waitKey(0)
cv2.destroyAllWindows()

Seaborn

類型: 數(shù)據(jù)可視化庫(kù)(基于Matplotlib)

功能: 提供更美觀、更高級(jí)的數(shù)據(jù)可視化接口,專注于統(tǒng)計(jì)圖形,如熱圖、箱線圖、小提琴圖、聯(lián)合分布圖等。

適用場(chǎng)景: 數(shù)據(jù)探索性分析、統(tǒng)計(jì)報(bào)告、交互式可視化。

import seaborn as sns
import pandas as pd
import numpy as np
from matplotlib import pyplot as plt


# 創(chuàng)建模擬數(shù)據(jù)
data = pd.DataFrame({'x': np.random.normal(size=100),
                     'y': np.random.normal(size=100),
                     'class': np.repeat(['A', 'B'], 50)})
# 繪制散點(diǎn)圖并按類別著色
sns.scatterplot(data=data, x='x', y='y', hue='class', palette=['red', 'blue'])
plt.title('Seaborn Scatterplot with Class Coloring')
plt.show()

Plotly

類型: 數(shù)據(jù)可視化庫(kù)

功能: 創(chuàng)建交互式圖表,支持Web瀏覽器展示,包括2D/3D圖表、地圖、儀表盤等,可導(dǎo)出為HTML或嵌入到Web應(yīng)用程序中。

適用場(chǎng)景: Web應(yīng)用的數(shù)據(jù)可視化、在線報(bào)告、動(dòng)態(tài)交互式圖表。

import plotly.graph_objs as go
import numpy as np
# 創(chuàng)建數(shù)據(jù)
t = np.linspace(0, 2 * np.pi, 100)
x = np.cos(t)
y = np.sin(t)
# 構(gòu)建 Plotly 圖表對(duì)象
fig = go.Figure(data=[go.Scatter(x=t, y=x, name='Cosine'),
                       go.Scatter(x=t, y=y, name='Sine')])
# 設(shè)置圖表屬性
fig.update_layout(title='Plotly Interactive Plot',
                  xaxis_title='Time',
                  yaxis_title='Value')
# 顯示圖表
fig.show()

Bokeh

類型: 數(shù)據(jù)可視化庫(kù)

功能: 制作高性能交互式圖表,支持大型數(shù)據(jù)集,輸出為HTML,可在現(xiàn)代Web瀏覽器中展示,提供豐富的用戶交互和實(shí)時(shí)更新能力。

適用場(chǎng)景: 大數(shù)據(jù)可視化、Web應(yīng)用程序中的實(shí)時(shí)數(shù)據(jù)流展示、儀表盤構(gòu)建。

from bokeh.plotting import figure, show
from bokeh.io import output_notebook
import numpy as np
output_notebook()
t = np.linspace(0, 2*np.pi, ?00)
x = np.cos(t)
y = np.sin(t)
p = figure(title="Bokeh Interactive Plot", plot_width=600, plot_height=400)
p.line(t, x, legend_label="cosine", line_width=2)
p.line(t, y, legend_label="sine", line_color="orange", line_width=2)
show(p)

Pycairo

類型: 矢量圖形庫(kù)綁定

功能: 通過Python接口調(diào)用Cairo庫(kù)進(jìn)行矢量圖形繪制,支持SVG、PDF、PostScript等格式輸出,保持圖像無損縮放。

適用場(chǎng)景: 創(chuàng)建高質(zhì)量的矢量圖形、圖標(biāo)設(shè)計(jì)、打印出版物。

import cairo
surface = cairo.ImageSurface(cairo.FORMAT_ARGB32, 200, 200)
ctx = cairo.Context(surface)
ctx.set_source_rgb(1, 1, 1)  # Set background color to white
ctx.rectangle(0, 0, surface.get_width(), surface.get_height())
ctx.fill()
ctx.set_source_rgb(0, 0, 0)  # Set drawing color to black
ctx.select_font_face("Sans")
ctx.set_font_size(32)
ctx.move_to(50, ?0)
ctx.show_text("Hello, Pycairo!")
surface.write_to_png('hello_pycairo.png')

SimpleCV

類型: 計(jì)算機(jī)視覺庫(kù)(封裝了OpenCV、PIL、NumPy)

功能: 提供易用的API,簡(jiǎn)化計(jì)算機(jī)視覺任務(wù),如圖像獲取、預(yù)處理、特征檢測(cè)、物體識(shí)別等。

適用場(chǎng)景: 初學(xué)者快速上手計(jì)算機(jī)視覺項(xiàng)目、教育和原型開發(fā)。

from SimpleCV import Camera, Display
# 初始化攝像頭
cam = Camera()
# 創(chuàng)建顯示窗口
disp = Display()
while True:
    # 獲取一幀圖像
    img = cam.getImage()
    # 對(duì)圖像進(jìn)行灰度處理
    gray_img = img.grayscale()
    # 在窗口中顯示圖像
    gray_img.show(disp)
    # 檢查是否有按鍵事件(如'q'鍵按下退出循環(huán))
    if disp.isDone():
        break
disp.destroy()
責(zé)任編輯:華軒 來源: 測(cè)試開發(fā)學(xué)習(xí)交流
相關(guān)推薦

2023-06-27 15:50:23

Python圖像處理

2024-05-23 11:53:24

Python代碼異常處理

2024-02-01 12:53:00

PandasPython數(shù)據(jù)

2023-10-07 11:36:15

2020-06-14 14:51:27

Java數(shù)據(jù)開發(fā)

2023-10-16 07:55:15

JavaScript對(duì)象技巧

2024-01-30 00:36:41

Python機(jī)器學(xué)習(xí)

2024-02-20 14:25:39

Python數(shù)據(jù)分析

2024-10-15 10:40:09

2025-02-20 10:13:54

2023-02-14 08:10:14

Python人工智能XAI

2024-09-23 16:49:32

2021-10-22 09:09:27

Python圖像處理工具編程語(yǔ)言

2024-12-03 14:33:42

Python遞歸編程

2023-03-27 23:37:21

2024-05-13 11:43:39

Python數(shù)據(jù)分析CSV

2022-04-24 10:12:25

Python軟件包代碼

2024-05-28 14:36:00

Python庫(kù)開發(fā)

2024-10-10 15:04:34

2024-07-18 15:08:27

點(diǎn)贊
收藏

51CTO技術(shù)棧公眾號(hào)