日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 编程语言 > python >内容正文

python

Python多线程下载网络URL图片的方法

發布時間:2024/4/15 python 33 豆豆
生活随笔 收集整理的這篇文章主要介紹了 Python多线程下载网络URL图片的方法 小編覺得挺不錯的,現在分享給大家,幫大家做個參考.

Python多線程下載網絡URL圖片的方法

? ? 采用多線程的方法,通過URL地址,下載資源圖片

? ? GitHub地址:https://github.com/PanJinquan/python-learning-notes/blob/master/modules/multiThread/download_image.py

? ? 實現源碼:

# -*-coding: utf-8 -*- """@Project: python-learning-notes@File : download_image.py@Author : panjq@E-mail : pan_jinquan@163.com@Date : 2019-06-06 15:51:05 """import time from multiprocessing.pool import ThreadPool import requests import os import PIL.Image as Image from io import BytesIO import cv2 import numpy as npdef download_image(url, our_dir):'''根據url下載圖片:param url::return: 返回保存的圖片途徑'''basename = os.path.basename(url)try:res = requests.get(url)if res.status_code == 200:print("download image successfully:{}".format(url))filename = os.path.join(our_dir, basename)with open(filename, "wb") as f:content = res.content# 使用Image解碼為圖片# image = Image.open(BytesIO(content))# image.show()# 使用opencv解碼為圖片content = np.asarray(bytearray(content), dtype="uint8")# image = cv2.imdecode(content, cv2.IMREAD_COLOR)# cv2.imshow("Image", image)# cv2.waitKey(1000)# f.write(content)# time.sleep(2)return filenameexcept Exception as e:print(e)return Noneprint("download image failed:{}".format(url))return Nonedef download_image_thread(url_list, our_dir, num_processes, remove_bad=False, Async=True):'''多線程下載圖片:param url_list: image url list:param our_dir: 保存圖片的路徑:param num_processes: 開啟線程個數:param remove_bad: 是否去除下載失敗的數據:param Async:是否異步:return: 返回圖片的存儲地址列表'''# 開啟多線程if not os.path.exists(our_dir):os.makedirs(our_dir)pool = ThreadPool(processes=num_processes)thread_list = []for image_url in url_list:if Async:out = pool.apply_async(func=download_image, args=(image_url, our_dir)) # 異步else:out = pool.apply(func=download_image, args=(image_url, our_dir)) # 同步thread_list.append(out)pool.close()pool.join()# 獲取輸出結果image_list = []if Async:for p in thread_list:image = p.get() # get會阻塞image_list.append(image)else:image_list = thread_listif remove_bad:image_list = [i for i in image_list if i is not None]return image_listif __name__ == "__main__":our_dir = "./image"url_list = ["https://farm3.staticflickr.com/2334/1828778894_271415878a_o.jpg","https://farm4.staticflickr.com/3149/2355285447_290193393a_o.jpg","https://farm3.staticflickr.com/2090/1792526652_8f37410561_o.jpg","https://farm3.staticflickr.com/2099/1791684639_044827f860_o.jpg"]startTime = time.time()image_list = download_image_thread(url_list, our_dir=our_dir, num_processes=4, remove_bad=True, Async=True)endTime = time.time()consumeTime = endTime - startTimeprint("程序運行時間:" + str(consumeTime) + " 秒")print(image_list)

?

總結

以上是生活随笔為你收集整理的Python多线程下载网络URL图片的方法的全部內容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。