最新国产好看的视频,伊人天堂AV在线,国产Aaaaaa视频,蜜臀视频在线观看一区,人妻av色图,密臀久久久精品影片,青青视频免费观看毛片,久草在线观看视,国产三级精品色情在线

Python中圖像通用操作的實(shí)現(xiàn)代碼

 更新時(shí)間:2023年07月03日 11:40:14   作者:fengbingchun  
這篇文章主要為大家詳細(xì)介紹了Python中圖像通用操作的實(shí)現(xiàn),例如:圖像旋轉(zhuǎn)、圖像縮放等,文中的示例代碼講解詳細(xì),需要的可以參考一下

平時(shí)經(jīng)常會(huì)對(duì)一個(gè)目錄下的圖像做統(tǒng)一處理,如縮放、旋轉(zhuǎn)等等,之前使用C++處理,有時(shí)不是很方便。發(fā)現(xiàn)使用Python比較簡單,代碼量又很少,在Anacanda下執(zhí)行起來也比較方便。因此,打算在后面遇到圖像的常規(guī)處理時(shí)都將其實(shí)現(xiàn)放入到同一個(gè)py文件中,用時(shí)可隨時(shí)執(zhí)行。

所有實(shí)現(xiàn)存放在OpenCV_Test/demo/Python的image_generic_operations.py文件中,目前實(shí)現(xiàn)的僅有:圖像旋轉(zhuǎn)(僅限90,180,270度)、圖像縮放,后面會(huì)逐漸增加,實(shí)現(xiàn)代碼如下:

import os
import sys
import cv2
from inspect import currentframe, getframeinfo
import argparse
 
def get_image_list(path, image_suffix):
    image_list = []
    for x in os.listdir(path):
        if x.endswith(image_suffix):
            image_list.append(path+"/"+x)
 
    return image_list
 
def get_image_name(image_name):
    pos = image_name.rfind("/")
    image_name = image_name[pos+1:]
 
    return image_name
 
def image_rotate_clockwise(image_list, degrees, result_path):
    print("image rotation ...")
    os.makedirs(result_path, exist_ok=True)
 
    if degrees == 90:
        rotate_code = cv2.ROTATE_90_CLOCKWISE
    elif degrees == 180:
        rotate_code = cv2.ROTATE_180
    elif degrees == 270:
        rotate_code = cv2.ROTATE_90_COUNTERCLOCKWISE
    else:
        raise Exception("Unsupported rotat degrees: {}, it only supports: clockwise 90, 180, 270; Error Line: {}".format(degrees, getframeinfo(currentframe()).lineno))
 
    for name in image_list:
        print(f"\t{name}")
        image_name = get_image_name(name)
        #print(f"image name:{image_name}"); sys.exit(1)
 
        mat = cv2.imread(name)
        mat = cv2.rotate(mat, rotateCode=rotate_code)
        cv2.imwrite(result_path+"/"+image_name, mat)
 
def image_resize(image_list, dst_width, dst_height, result_path):
    print("image resize ...")
    os.makedirs(result_path, exist_ok=True)
 
    mat = cv2.imread(image_list[0])
    h, w, _ = mat.shape
    if h > dst_width and w > dst_height:
        interpolation = cv2.INTER_AREA
    else:
        interpolation = cv2.INTER_CUBIC
 
    for name in image_list:
        print(f"\t{name}")
        image_name = get_image_name(name)
        #print(f"image name:{image_name}"); sys.exit(1)
 
        mat = cv2.imread(name)
        mat = cv2.resize(mat, (dst_width, dst_height), interpolation=interpolation)
        cv2.imwrite(result_path+"/"+image_name, mat)
 
def parse_args():
    parser = argparse.ArgumentParser(description="image generic operations", add_help=True)
 
    parser.add_argument("--image_src_path", required=True, type=str, help="the path of the image to be operated, for example: ../../test_images")
    parser.add_argument("--operation", required=True, type=str, choices=["rotate", "resize"], help="specifies the operation to take on the image")
    parser.add_argument("--image_dst_path", required=True, type=str, help="the path where the resulting image is saved, for example: ../../test_images/result")
 
    parser.add_argument("--degrees", default=90, type=int, choices=[90, 180, 270], help="the degrees by which the image is rotated clockwise")
 
    parser.add_argument("--width", default=256, type=int, help="the width of the image after scaling")
    parser.add_argument("--height", default=256, type=int, help="the height of the image after scaling")
 
    parser.add_argument("--image_suffix", default=".png", type=str, help="the suffix of the processed image")
    
    args = parser.parse_args()
    return args
 
if __name__ == "__main__":
    args = parse_args()
 
    image_list = get_image_list(args.image_src_path, args.image_suffix)
 
    if args.operation == "rotate":
        image_rotate_clockwise(image_list, args.degrees, args.image_dst_path)
 
    if args.operation == "resize":
        image_resize(image_list, args.width, args.height, args.image_dst_path)
 
    print("test finish")

使用如下所示:

GitHubhttps://github.com/fengbingchun/OpenCV_Test

到此這篇關(guān)于Python中圖像通用操作的實(shí)現(xiàn)代碼的文章就介紹到這了,更多相關(guān)Python圖像操作內(nèi)容請(qǐng)搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!

相關(guān)文章

  • Python實(shí)現(xiàn)3行代碼解簡單的一元一次方程

    Python實(shí)現(xiàn)3行代碼解簡單的一元一次方程

    這篇文章主要介紹了Python實(shí)現(xiàn)3行代碼解簡單的一元一次方程,很適合Python初學(xué)者學(xué)習(xí)借鑒,需要的朋友可以參考下
    2014-08-08
  • Python裝飾器語法糖

    Python裝飾器語法糖

    今天小編就為大家分享一篇關(guān)于Python裝飾器語法糖,小編覺得內(nèi)容挺不錯(cuò)的,現(xiàn)在分享給大家,具有很好的參考價(jià)值,需要的朋友一起跟隨小編來看看吧
    2019-01-01
  • python制作定時(shí)發(fā)送信息腳本的實(shí)現(xiàn)思路

    python制作定時(shí)發(fā)送信息腳本的實(shí)現(xiàn)思路

    這篇文章主要介紹了python實(shí)現(xiàn)企業(yè)微信定時(shí)發(fā)送文本消息的實(shí)例代碼,本文給大家介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友可以參考下
    2020-11-11
  • Python英文文本分詞(無空格)模塊wordninja的使用實(shí)例

    Python英文文本分詞(無空格)模塊wordninja的使用實(shí)例

    今天小編就為大家分享一篇關(guān)于Python英文文本分詞(無空格)模塊wordninja的使用實(shí)例,小編覺得內(nèi)容挺不錯(cuò)的,現(xiàn)在分享給大家,具有很好的參考價(jià)值,需要的朋友一起跟隨小編來看看吧
    2019-02-02
  • python實(shí)現(xiàn)百萬答題自動(dòng)百度搜索答案

    python實(shí)現(xiàn)百萬答題自動(dòng)百度搜索答案

    這篇文章主要為大家詳細(xì)介紹了python實(shí)現(xiàn)百萬答題自動(dòng)百度搜索答案,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下
    2018-01-01
  • Python網(wǎng)絡(luò)編程實(shí)戰(zhàn)之爬蟲技術(shù)入門與實(shí)踐

    Python網(wǎng)絡(luò)編程實(shí)戰(zhàn)之爬蟲技術(shù)入門與實(shí)踐

    這篇文章主要介紹了Python網(wǎng)絡(luò)編程實(shí)戰(zhàn)之爬蟲技術(shù)入門與實(shí)踐,了解這些基礎(chǔ)概念和原理將幫助您更好地理解網(wǎng)絡(luò)爬蟲的實(shí)現(xiàn)過程和技巧,需要的朋友可以參考下
    2023-04-04
  • python實(shí)現(xiàn)字符串字母大小寫轉(zhuǎn)換的幾種方法

    python實(shí)現(xiàn)字符串字母大小寫轉(zhuǎn)換的幾種方法

    本文主要介紹了python實(shí)現(xiàn)字符串字母大小寫轉(zhuǎn)換的幾種方法,包括islower()、isupper()、istitle()、lower()、casefold()、upper()、capitalize()、title()和swapcase(),具有一定的參考價(jià)值,感興趣的可以了解一下
    2025-03-03
  • 在Python下利用OpenCV來旋轉(zhuǎn)圖像的教程

    在Python下利用OpenCV來旋轉(zhuǎn)圖像的教程

    這篇文章主要介紹了在Python下利用OpenCV來旋轉(zhuǎn)圖像的教程,代碼和核心的算法都非常簡單,需要的朋友可以參考下
    2015-04-04
  • Python如何用字典完成匹配任務(wù)

    Python如何用字典完成匹配任務(wù)

    在生物信息學(xué)領(lǐng)域,經(jīng)常需要根據(jù)基因名稱匹配其對(duì)應(yīng)的編號(hào),本文介紹了一種通過字典進(jìn)行基因名稱與編號(hào)匹配的方法,首先定義一個(gè)空列表存儲(chǔ)對(duì)應(yīng)編號(hào),對(duì)于字典中不存在的基因名稱,其編號(hào)默認(rèn)為0
    2024-09-09
  • 利用python如何在前程無憂高效投遞簡歷

    利用python如何在前程無憂高效投遞簡歷

    這篇文章主要給大家介紹了關(guān)于利用python如何在前程無憂高效投遞簡歷的相關(guān)資料,文中通過示例代碼介紹的非常詳細(xì),對(duì)大家學(xué)習(xí)或者使用python具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面來一起學(xué)習(xí)學(xué)習(xí)吧
    2019-05-05

最新評(píng)論

汶川县| 广元市| 始兴县| 中江县| 双峰县| 赤壁市| 武陟县| 藁城市| 武功县| 海阳市| 广宁县| 钟山县| 厦门市| 沈丘县| 伊春市| 静安区| 都昌县| 五大连池市| 泽库县| 林西县| 葫芦岛市| 肇东市| 璧山县| 北票市| 凤翔县| 绥德县| 贵德县| 乌兰察布市| 徐闻县| 固镇县| 台中县| 奉节县| 南丹县| 灵台县| 玉屏| 石楼县| 中阳县| 乌恰县| 江安县| 辽源市| 巧家县|