在編程的世界中,Python一直以其簡潔、易讀的語法而備受推崇。然而,除了Python本身的強大功能之外,還有許多令人瞠目結舌的Python庫,它們為開發者們帶來了無盡的驚喜和創造力。在本文中,筆者為大家分享7個這樣的Python庫,建議收藏。
rembg是一個強大的Python庫,用于圖像背景的自動去除。它基于深度學習和人工智能技術,能夠高度準確地將圖像中的背景摳出,留下前景圖像。
安裝rembg:
#Installationpip install rembg
示例:
# Importing librariesfrom rembg import removeimport cv2 # path of input image (my file: image.jpeg)input_path = 'demo.jpg'# path for saving output image and saving as a output.jpegoutput_path = 'output.jpg'# Reading the input imageinput = cv2.imread(input_path)# Removing backgroundoutput = remove(input)# Saving file cv2.imwrite(output_path, output)
Ipyvolume是一個基于Jupyter Notebook的Python庫,用于創建交互式的3D可視化和動畫。它提供了豐富的功能和工具,使得在Notebook中可視化數據變得更加簡單和直觀。
示例代碼:
from colormaps import parulaX = 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 = parulaznorm = 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()
Pandas-Bokeh是一個使用Bokeh為Pandas數據幀提供交互式繪圖的庫,它對于創建交互式可視化數據非常有用。
安裝pandas-bokeh:
pip install pandas-bokeh
交互式可視化效果:
示例代碼:
import pandas as pdimport pandas_bokehdata = { '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)
Humanize是一個Python庫,旨在將復雜的數據類型和單位轉換為更易讀的形式,以增加人類可理解性。它提供了一些有用的函數,用于將數字、時間、文件大小等轉換為更友好和可讀性強的格式。
使用Humanize庫,你可以將整數轉換為帶有逗號的易讀形式,例如將1000轉換為"1,000";將時間間隔轉換為更具描述性的形式,例如將60秒轉換為"1分鐘";將字節數轉換為更易理解的文件大小表示,例如將1024轉換為"1KB"。
安裝Humanize:
pip install Humanize
示例(integers):
# Importing libraryimport humanizeimport datetime as dt# Formatting numbers with commaa = humanize.intcomma(951009)# converting numbers into wordsb = humanize.intword(10046328394)#printingprint(a)print(b)
輸出:
951,009 10.0 billion
示例(Date&Time):
import humanizeimport datetime as dta = humanize.naturaldate(dt.date(2023, 9,7))b = humanize.naturalday(dt.date(2023, 9,7))print(a)print(b)
輸出:
todaytoday
Pendulum擴展了內置的Python DateTime模塊,添加了一個更直觀的API,用于處理時區,對日期和時間進行操作,如添加時間間隔、刪去日期以及在時區之間進行轉換。它為格式化日期和時間提供了一個簡單、人性化的API。
安裝Pendulum:
pip install pendulum
示例:
# import libraryimport pendulumdt = pendulum.datetime(2023, 8, 31)print(dt) #local() creates datetime instance with local timezonelocal = pendulum.local(2023, 8, 31)print("Local Time:", local)print("Local Time Zone:", local.timezone.name)# Printing UTC timeutc = pendulum.now('UTC')print("Current UTC time:", utc) # Converting UTC timezone into Europe/Paris timeeurope = 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
Sketchpy是一個用于對圖像進行動畫繪制的Python模塊。sketchpy模塊是在Python中的turtle模塊之上創建的。
安裝Sketchpy:
pip install sketchpy
示例-使用 Python 繪制 Vijay:
from sketchpy import librarymyObject = library.vijay()myObject.draw()
FTFY是一個Python庫,它的全稱是"Fixes Text For You",用于修復和糾正文本中的常見編碼問題和Unicode字符問題。它可以自動檢測和修復各種編碼問題,使得文本在處理和顯示時更加準確和一致。
安裝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'))
輸出:
本文鏈接:http://www.www897cc.com/showinfo-26-34621-0.html七個令人瞠目結舌的Python庫
聲明:本網頁內容旨在傳播知識,若有侵權等問題請及時與本網聯系,我們將在第一時間刪除處理。郵件:2376512515@qq.com
上一篇: Go 語言中 sync 包的近距離觀察
下一篇: C 語言的入口真的是 main 函數嗎?