在編程的世界中,Python一直以其簡(jiǎn)潔、易讀的語(yǔ)法而備受推崇。然而,除了Python本身的強(qiáng)大功能之外,還有許多令人瞠目結(jié)舌的Python庫(kù),它們?yōu)殚_(kāi)發(fā)者們帶來(lái)了無(wú)盡的驚喜和創(chuàng)造力。在本文中,筆者為大家分享7個(gè)這樣的Python庫(kù),建議收藏。
rembg是一個(gè)強(qiáng)大的Python庫(kù),用于圖像背景的自動(dòng)去除。它基于深度學(xué)習(xí)和人工智能技術(shù),能夠高度準(zhǔn)確地將圖像中的背景摳出,留下前景圖像。
安裝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是一個(gè)基于Jupyter Notebook的Python庫(kù),用于創(chuàng)建交互式的3D可視化和動(dòng)畫(huà)。它提供了豐富的功能和工具,使得在Notebook中可視化數(shù)據(jù)變得更加簡(jiǎn)單和直觀。
示例代碼:
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是一個(gè)使用Bokeh為Pandas數(shù)據(jù)幀提供交互式繪圖的庫(kù),它對(duì)于創(chuàng)建交互式可視化數(shù)據(jù)非常有用。
安裝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是一個(gè)Python庫(kù),旨在將復(fù)雜的數(shù)據(jù)類(lèi)型和單位轉(zhuǎn)換為更易讀的形式,以增加人類(lèi)可理解性。它提供了一些有用的函數(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 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擴(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 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是一個(gè)用于對(duì)圖像進(jìn)行動(dòng)畫(huà)繪制的Python模塊。sketchpy模塊是在Python中的turtle模塊之上創(chuàng)建的。
安裝Sketchpy:
pip install sketchpy
示例-使用 Python 繪制 Vijay:
from sketchpy import librarymyObject = library.vijay()myObject.draw()
FTFY是一個(gè)Python庫(kù),它的全稱(chēng)是"Fixes Text For You",用于修復(fù)和糾正文本中的常見(jiàn)編碼問(wèn)題和Unicode字符問(wèn)題。它可以自動(dòng)檢測(cè)和修復(fù)各種編碼問(wèn)題,使得文本在處理和顯示時(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'))
輸出:
本文鏈接:http://www.www897cc.com/showinfo-26-34621-0.html七個(gè)令人瞠目結(jié)舌的Python庫(kù)
聲明:本網(wǎng)頁(yè)內(nèi)容旨在傳播知識(shí),若有侵權(quán)等問(wèn)題請(qǐng)及時(shí)與本網(wǎng)聯(lián)系,我們將在第一時(shí)間刪除處理。郵件:2376512515@qq.com