這篇文章主要介紹了Python 透過URL開啟圖片實例詳解的相關資料,需要的朋友可以參考下
Python 透過URL開啟圖片實例詳解
不論是用OpenCV還是PIL,skimage等函式庫,在之前做影像處理的時候,幾乎都是讀取本地的圖片。最近嘗試爬蟲爬取圖片,在儲存之前,我希望能先快速瀏覽一次圖片,然後有選擇性的保存。這裡就需要從url讀取圖片了。查了很多資料,發現有這麼多種方法,這裡做個記錄。
本文用到的圖片URL如下:
img_src = 'http://wx2.sinaimg.cn/mw690/ac38503ely1fesz8m0ov6j20qo140dix.jpg'
1.用OpenCV
OpenCV的imread()只能載入本地的圖片,並不能透過網址載入圖片。但是,opencv的VideoCapture類別可以從url載入影片。如果只用opencv的話,我們可以一個迂迴的方式:先用VideoCapure載入網址下的圖片,再傳給Mat。
import cv2 cap = cv2.VideoCapture(img_src) if( cap.isOpened() ) : ret,img = cap.read() cv2.imshow("image",img) cv2.waitKey()
2. OpenCV+Numpy+urllib
import numpy as np import urllib import cv2 resp = urllib.urlopen(img_src) image = np.asarray(bytearray(resp.read()), dtype="uint8") image = cv2.imdecode(image, cv2.IMREAD_COLOR) cv2.imshow("Image", image) cv2.waitKey(0)
urlopen傳回一個類別檔案物件,它提供如下方法:
read() , readline() , readlines() , fileno() , close() :這些方法的使用方式與檔案物件完全一樣。然後把傳回的類別檔案物件重新編碼,轉換為圖片傳給Mat。
3.PIL+requests
import requests as req from PIL import Image from io import BytesIO response = req.get(img_src) image = Image.open(BytesIO(response.content)) image.show()
requests能以位元組的方式存取請求回應體,以上就是以請求傳回的二進位資料建立一張圖片的程式碼。
4.skimage
from skimage import io image = io.imread(img_src) io.imshow(image) io.show()
相對來說,這種方式應該是最簡單的,因為skimage可以直接以imread()函數來讀取網頁圖片,而不需要其他的輔助,也不需要迂迴。
以上是Python如何透過URL實現開啟圖片的實例詳解的詳細內容。更多資訊請關注PHP中文網其他相關文章!

Arraysinpython,尤其是Vianumpy,ArecrucialInsCientificComputingfortheireftheireffertheireffertheirefferthe.1)Heasuedfornumerericalicerationalation,dataAnalysis和Machinelearning.2)Numpy'Simpy'Simpy'simplementIncressionSressirestrionsfasteroperoperoperationspasterationspasterationspasterationspasterationspasterationsthanpythonlists.3)inthanypythonlists.3)andAreseNableAblequick

你可以通過使用pyenv、venv和Anaconda來管理不同的Python版本。 1)使用pyenv管理多個Python版本:安裝pyenv,設置全局和本地版本。 2)使用venv創建虛擬環境以隔離項目依賴。 3)使用Anaconda管理數據科學項目中的Python版本。 4)保留系統Python用於系統級任務。通過這些工具和策略,你可以有效地管理不同版本的Python,確保項目順利運行。

numpyarrayshaveseveraladagesoverandastardandpythonarrays:1)基於基於duetoc的iMplation,2)2)他們的aremoremoremorymorymoremorymoremorymoremorymoremoremory,尤其是WithlargedAtasets和3)效率化,效率化,矢量化函數函數函數函數構成和穩定性構成和穩定性的操作,製造

數組的同質性對性能的影響是雙重的:1)同質性允許編譯器優化內存訪問,提高性能;2)但限制了類型多樣性,可能導致效率低下。總之,選擇合適的數據結構至關重要。

到CraftCraftExecutablePythcripts,lollow TheSebestPractices:1)Addashebangline(#!/usr/usr/bin/envpython3)tomakethescriptexecutable.2)setpermissionswithchmodwithchmod xyour_script.3)

numpyArraysareAreBetterFornumericalialoperations andmulti-demensionaldata,而learthearrayModuleSutableforbasic,內存效率段

numpyArraySareAreBetterForHeAvyNumericalComputing,而lelethearRayModulesiutable-usemoblemory-connerage-inderabledsswithSimpleDatateTypes.1)NumpyArsofferVerverVerverVerverVersAtility andPerformanceForlargedForlargedAtatasetSetsAtsAndAtasEndCompleXoper.2)

ctypesallowscreatingingangandmanipulatingc-stylarraysinpython.1)usectypestoInterfacewithClibrariesForperfermance.2)createc-stylec-stylec-stylarraysfornumericalcomputations.3)passarraystocfunctions foreforfunctionsforeffortions.however.however,However,HoweverofiousofmemoryManageManiverage,Pressiveo,Pressivero


熱AI工具

Undresser.AI Undress
人工智慧驅動的應用程序,用於創建逼真的裸體照片

AI Clothes Remover
用於從照片中去除衣服的線上人工智慧工具。

Undress AI Tool
免費脫衣圖片

Clothoff.io
AI脫衣器

Video Face Swap
使用我們完全免費的人工智慧換臉工具,輕鬆在任何影片中換臉!

熱門文章

熱工具

SublimeText3 英文版
推薦:為Win版本,支援程式碼提示!

ZendStudio 13.5.1 Mac
強大的PHP整合開發環境

MinGW - Minimalist GNU for Windows
這個專案正在遷移到osdn.net/projects/mingw的過程中,你可以繼續在那裡關注我們。 MinGW:GNU編譯器集合(GCC)的本機Windows移植版本,可自由分發的導入函式庫和用於建置本機Windows應用程式的頭檔;包括對MSVC執行時間的擴展,以支援C99功能。 MinGW的所有軟體都可以在64位元Windows平台上運作。

SAP NetWeaver Server Adapter for Eclipse
將Eclipse與SAP NetWeaver應用伺服器整合。

Atom編輯器mac版下載
最受歡迎的的開源編輯器