Python使用OpenCV轉(zhuǎn)換圖像大小
在Python中,使用OpenCV庫來轉(zhuǎn)換圖像大小是一個常見的操作,它可以幫助你調(diào)整圖像到特定的尺寸,以適應(yīng)不同的應(yīng)用場景,比如圖像預(yù)處理、模型輸入等。下面是一個詳細(xì)的代碼示例,展示了如何使用OpenCV來轉(zhuǎn)換圖像的大小。
首先,確保你已經(jīng)安裝了OpenCV庫。如果還沒有安裝,可以通過pip安裝:
pip install opencv-python
接下來,是一個完整的Python腳本,它加載一個圖像文件,將其大小轉(zhuǎn)換為指定的寬度和高度,然后顯示并保存轉(zhuǎn)換后的圖像。
import cv2 def resize_image(input_image_path, output_image_path, width=None, height=None, inter=cv2.INTER_AREA): """ 調(diào)整圖像大小 :param input_image_path: 輸入圖像的路徑 :param output_image_path: 輸出圖像的路徑 :param width: 目標(biāo)寬度,如果為None,則不改變寬度 :param height: 目標(biāo)高度,如果為None,則不改變高度 :param inter: 插值方法,默認(rèn)為cv2.INTER_AREA(適用于縮小圖像) :return: None """ # 讀取圖像 image = cv2.imread(input_image_path) if image is None: print(f"Error: Unable to load image at {input_image_path}") return # 檢查是否指定了寬度和高度 if width is None and height is None: print("Error: Both width and height cannot be None.") return # 如果只指定了寬度或高度,則計算另一個維度以保持圖像的寬高比 if width is None: width = int(image.shape[1] * (height / float(image.shape[0]))) elif height is None: height = int(image.shape[0] * (width / float(image.shape[1]))) # 調(diào)整圖像大小 resized_image = cv2.resize(image, (width, height), interpolation=inter) # 顯示圖像(可選) cv2.imshow('Resized Image', resized_image) cv2.waitKey(0) # 等待按鍵 cv2.destroyAllWindows() # 保存圖像 cv2.imwrite(output_image_path, resized_image) # 使用示例 input_image = 'path_to_your_image.jpg' # 替換為你的圖像路徑 output_image = 'resized_image.jpg' resize_image(input_image, output_image, width=640, height=480)
在這個示例中,resize_image函數(shù)接受輸入圖像的路徑、輸出圖像的路徑、目標(biāo)寬度、目標(biāo)高度以及插值方法作為參數(shù)。它首先讀取圖像,然后檢查是否指定了寬度和高度。如果只指定了其中一個,則根據(jù)原始圖像的寬高比計算另一個維度。之后,使用cv2.resize函數(shù)調(diào)整圖像大小,并通過cv2.imshow顯示圖像(這是可選的,主要用于調(diào)試),最后使用cv2.imwrite保存調(diào)整大小后的圖像。
請確保將'path_to_your_image.jpg'替換為你自己的圖像文件路徑,并根據(jù)需要調(diào)整目標(biāo)寬度和高度。
這里我會提供一個稍微不同的例子,這次我們將專注于只指定寬度或高度中的一個參數(shù),讓OpenCV自動根據(jù)原始圖像的寬高比計算另一個維度,以確保圖像不會失真。
import cv2 def resize_image_keep_aspect_ratio(input_image_path, output_image_path, max_width=None, max_height=None, inter=cv2.INTER_AREA): """ 調(diào)整圖像大小,同時保持寬高比 :param input_image_path: 輸入圖像的路徑 :param output_image_path: 輸出圖像的路徑 :param max_width: 最大寬度,如果為None,則不限制寬度 :param max_height: 最大高度,如果為None,則不限制高度 :param inter: 插值方法,默認(rèn)為cv2.INTER_AREA(適用于縮小圖像) :return: None """ # 讀取圖像 image = cv2.imread(input_image_path) if image is None: print(f"Error: Unable to load image at {input_image_path}") return # 獲取原始圖像的寬高 height, width = image.shape[:2] # 計算新的尺寸 if max_width is None and max_height is None: print("Error: Both max_width and max_height cannot be None.") return elif max_width is None: max_width = int(width * (max_height / float(height))) elif max_height is None: max_height = int(height * (max_width / float(width))) else: # 確保寬度和高度不會超過指定的最大值,同時保持寬高比 ratio = min(max_width / width, max_height / height) max_width = int(width * ratio) max_height = int(height * ratio) # 調(diào)整圖像大小 resized_image = cv2.resize(image, (max_width, max_height), interpolation=inter) # 保存圖像 cv2.imwrite(output_image_path, resized_image) # 可選:顯示圖像(注意,在生產(chǎn)環(huán)境中通常不會這樣做) # cv2.imshow('Resized Image with Aspect Ratio', resized_image) # cv2.waitKey(0) # cv2.destroyAllWindows() # 使用示例 input_image = 'your_image.jpg' # 替換為你的圖像文件路徑 output_image = 'resized_image_with_aspect_ratio.jpg' resize_image_keep_aspect_ratio(input_image, output_image, max_width=800) # 只指定最大寬度 # 或者 # resize_image_keep_aspect_ratio(input_image, output_image, max_height=600) # 只指定最大高度
在這個例子中,resize_image_keep_aspect_ratio 函數(shù)允許你通過指定最大寬度或最大高度來調(diào)整圖像大小,同時保持圖像的原始寬高比。如果同時指定了最大寬度和最大高度,函數(shù)將計算一個縮放比例,該比例是兩者中較小的那個,以確保圖像不會超過這兩個限制中的任何一個。
請記得將 'your_image.jpg' 替換為你自己的圖像文件路徑,并根據(jù)需要調(diào)整 max_width 或 max_height 參數(shù)。如果你想要查看調(diào)整大小后的圖像,可以取消注釋與 cv2.imshow 相關(guān)的代碼行。但在實際的生產(chǎn)環(huán)境中,通常不會這樣做,因為 cv2.imshow 需要一個GUI環(huán)境來顯示圖像。
以上就是Python使用OpenCV轉(zhuǎn)換圖像大小的詳細(xì)內(nèi)容,更多關(guān)于Python OpenCV圖像大小的資料請關(guān)注腳本之家其它相關(guān)文章!
相關(guān)文章
python requests模擬登陸github的實現(xiàn)方法
這篇文章主要介紹了python requests模擬登陸github的實現(xiàn)方法,文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2019-12-12python3實現(xiàn)抓取網(wǎng)頁資源的 N 種方法
這兩天學(xué)習(xí)了python3實現(xiàn)抓取網(wǎng)頁資源的方法,發(fā)現(xiàn)了很多種方法,所以,今天添加一點小筆記。2017-05-05Python實現(xiàn)去除列表中重復(fù)元素的方法小結(jié)【4種方法】
這篇文章主要介紹了Python實現(xiàn)去除列表中重復(fù)元素的方法,結(jié)合實例形式總結(jié)分析了Python列表去重的4種實現(xiàn)方法,涉及Python針對列表的遍歷、判斷、排序等相關(guān)操作技巧,需要的朋友可以參考下2018-04-04python中的__init__ 、__new__、__call__小結(jié)
這篇文章主要介紹了python中的__init__ 、__new__、__call__小結(jié),需要的朋友可以參考下2014-04-04