七個(gè)令人瞠目結(jié)舌的Python庫(kù)
在編程的世界中,Python一直以其簡(jiǎn)潔、易讀的語法而備受推崇。然而,除了Python本身的強(qiáng)大功能之外,還有許多令人瞠目結(jié)舌的Python庫(kù),它們?yōu)殚_發(fā)者們帶來了無盡的驚喜和創(chuàng)造力。在本文中,筆者為大家分享7個(gè)這樣的Python庫(kù),建議收藏。
1. rembg
rembg是一個(gè)強(qiáng)大的Python庫(kù),用于圖像背景的自動(dòng)去除。它基于深度學(xué)習(xí)和人工智能技術(shù),能夠高度準(zhǔn)確地將圖像中的背景摳出,留下前景圖像。
安裝rembg:
#Installation
pip install rembg
示例:
# Importing libraries
from rembg import remove
import cv2
# path of input image (my file: image.jpeg)
input_path = 'demo.jpg'
# path for saving output image and saving as a output.jpeg
output_path = 'output.jpg'
# Reading the input image
input = cv2.imread(input_path)
# Removing background
output = remove(input)
# Saving file
cv2.imwrite(output_path, output)
2. Ipyvolume
Ipyvolume是一個(gè)基于Jupyter Notebook的Python庫(kù),用于創(chuàng)建交互式的3D可視化和動(dòng)畫。它提供了豐富的功能和工具,使得在Notebook中可視化數(shù)據(jù)變得更加簡(jiǎn)單和直觀。
示例代碼:
from colormaps import parula
X = np.arange(-5, 5, 0.25*1)
Y = np.arange(-5, 5, 0.25*1)
X, Y = np.meshgrid(X, Y)
R = np.sqrt(X**2 + Y**2)
Z = np.sin(R)
colormap = parula
znorm = Z - Z.min()
znorm /= znorm.ptp()
znorm.min(), znorm.max()
color = colormap(znorm)
ipv.figure()
mesh = ipv.plot_surface(X, Z, Y, color=color[...,:3])
ipv.show()
3. Pandas-Bokeh
Pandas-Bokeh是一個(gè)使用Bokeh為Pandas數(shù)據(jù)幀提供交互式繪圖的庫(kù),它對(duì)于創(chuàng)建交互式可視化數(shù)據(jù)非常有用。
安裝pandas-bokeh:
pip install pandas-bokeh
交互式可視化效果:
示例代碼:
import pandas as pd
import pandas_bokeh
data = {
'fruits':
['Apples', 'Pears', 'Nectarines', 'Plums', 'Grapes', 'Strawberries'],
'2015': [2, 1, 4, 3, 2, 4],
'2016': [5, 3, 3, 2, 4, 6],
'2017': [3, 2, 4, 4, 5, 3]
}
df = pd.DataFrame(data).set_index("fruits")
p_bar = df.plot_bokeh.bar(
ylabel="Price per Unit [€]",
title="Fruit prices per Year",
alpha=0.6)
4. Humanize
Humanize是一個(gè)Python庫(kù),旨在將復(fù)雜的數(shù)據(jù)類型和單位轉(zhuǎn)換為更易讀的形式,以增加人類可理解性。它提供了一些有用的函數(shù),用于將數(shù)字、時(shí)間、文件大小等轉(zhuǎn)換為更友好和可讀性強(qiáng)的格式。
使用Humanize庫(kù),你可以將整數(shù)轉(zhuǎn)換為帶有逗號(hào)的易讀形式,例如將1000轉(zhuǎn)換為"1,000";將時(shí)間間隔轉(zhuǎn)換為更具描述性的形式,例如將60秒轉(zhuǎn)換為"1分鐘";將字節(jié)數(shù)轉(zhuǎn)換為更易理解的文件大小表示,例如將1024轉(zhuǎn)換為"1KB"。
安裝Humanize:
pip install Humanize
示例(integers):
# Importing library
import humanize
import datetime as dt
# Formatting numbers with comma
a = humanize.intcomma(951009)
# converting numbers into words
b = humanize.intword(10046328394)
#printing
print(a)
print(b)
輸出:
951,009 10.0 billion
示例(Date&Time):
import humanize
import datetime as dt
a = humanize.naturaldate(dt.date(2023, 9,7))
b = humanize.naturalday(dt.date(2023, 9,7))
print(a)
print(b)
輸出:
today
today
5. Pendulum
Pendulum擴(kuò)展了內(nèi)置的Python DateTime模塊,添加了一個(gè)更直觀的API,用于處理時(shí)區(qū),對(duì)日期和時(shí)間進(jìn)行操作,如添加時(shí)間間隔、刪去日期以及在時(shí)區(qū)之間進(jìn)行轉(zhuǎn)換。它為格式化日期和時(shí)間提供了一個(gè)簡(jiǎn)單、人性化的API。
安裝Pendulum:
pip install pendulum
示例:
# import library
import pendulum
dt = pendulum.datetime(2023, 8, 31)
print(dt)
#local() creates datetime instance with local timezone
local = pendulum.local(2023, 8, 31)
print("Local Time:", local)
print("Local Time Zone:", local.timezone.name)
# Printing UTC time
utc = pendulum.now('UTC')
print("Current UTC time:", utc)
# Converting UTC timezone into Europe/Paris time
europe = utc.in_timezone('Europe/Paris')
print("Current time in Paris:", europe)
輸出:
2023-08-31T00:00:00+00:00
Local Time: 2023-08-31T00:00:00+08:00
Local Time Zone: Asia/Shanghai Current
UTC time: 2023-09-07T04:06:05.436553+00:00
Current time in Paris: 2023-09-07T06:06:05.436553+02:00
6. Sketchpy
Sketchpy是一個(gè)用于對(duì)圖像進(jìn)行動(dòng)畫繪制的Python模塊。sketchpy模塊是在Python中的turtle模塊之上創(chuàng)建的。
安裝Sketchpy:
pip install sketchpy
示例-使用 Python 繪制 Vijay:
from sketchpy import library
myObject = library.vijay()
myObject.draw()
7. FTFY
FTFY是一個(gè)Python庫(kù),它的全稱是"Fixes Text For You",用于修復(fù)和糾正文本中的常見編碼問題和Unicode字符問題。它可以自動(dòng)檢測(cè)和修復(fù)各種編碼問題,使得文本在處理和顯示時(shí)更加準(zhǔn)確和一致。
安裝FTFY:
pip install ftfy
示例:
print(ftfy.fix_text('Correct the sentence using a€?ftfya€\x9d.'))
print(ftfy.fix_text('a?” No problems with text'))
print(ftfy.fix_text('? perturber la r??flexion'))
輸出: