欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

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比較簡(jiǎn)單,代碼量又很少,在Anacanda下執(zhí)行起來(lái)也比較方便。因此,打算在后面遇到圖像的常規(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字典循環(huán)添加一鍵多值的用法實(shí)例

    Python字典循環(huán)添加一鍵多值的用法實(shí)例

    今天小編就為大家分享一篇Python字典循環(huán)添加一鍵多值的用法實(shí)例,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過(guò)來(lái)看看吧
    2019-01-01
  • Python使用MD5加密字符串示例

    Python使用MD5加密字符串示例

    這篇文章主要介紹了Python使用MD5加密字符串示例,對(duì)一些可能出現(xiàn)的錯(cuò)誤點(diǎn)上本文也給出提醒,需要的朋友可以參考下
    2014-08-08
  • Flask核心機(jī)制之上下文源碼剖析

    Flask核心機(jī)制之上下文源碼剖析

    這篇文章主要介紹了Flask核心機(jī)制之上下文源碼剖析,小編覺(jué)得挺不錯(cuò)的,現(xiàn)在分享給大家,也給大家做個(gè)參考。一起跟隨小編過(guò)來(lái)看看吧
    2018-12-12
  • Python?pygame繪制游戲圖像

    Python?pygame繪制游戲圖像

    這篇文章主要介紹了Python?pygame繪制游戲圖像,文章圍繞主題展開(kāi)pygame模塊完成飛機(jī)大戰(zhàn)游戲的實(shí)戰(zhàn)開(kāi)發(fā)的案例詳情,需要的朋友可以參考一下
    2022-08-08
  • Keras 多次加載model出錯(cuò)的解決方案

    Keras 多次加載model出錯(cuò)的解決方案

    這篇文章主要介紹了Keras 多次加載model出錯(cuò)的解決方案,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教
    2021-05-05
  • 使用python進(jìn)行時(shí)間序列預(yù)測(cè)的流程

    使用python進(jìn)行時(shí)間序列預(yù)測(cè)的流程

    使用 Python 進(jìn)行時(shí)間序列預(yù)測(cè)是一個(gè)非常常見(jiàn)的任務(wù),可以應(yīng)用于各種領(lǐng)域,時(shí)間序列預(yù)測(cè)的方法有很多,包括統(tǒng)計(jì)方法、機(jī)器學(xué)習(xí)方法、以及深度學(xué)習(xí)方法,下面是一個(gè)簡(jiǎn)單的時(shí)間序列預(yù)測(cè)流程示例,需要的朋友可以參考下
    2024-09-09
  • python基于itchat實(shí)現(xiàn)微信群消息同步機(jī)器人

    python基于itchat實(shí)現(xiàn)微信群消息同步機(jī)器人

    本篇文章主要介紹了python基于itchat實(shí)現(xiàn)微信群消息同步機(jī)器人,小編覺(jué)得挺不錯(cuò)的,現(xiàn)在分享給大家,也給大家做個(gè)參考。一起跟隨小編過(guò)來(lái)看看吧
    2017-02-02
  • 一文弄懂10大Pandas的索引

    一文弄懂10大Pandas的索引

    索引在我們的日常生活中其實(shí)是很常見(jiàn)的,本文主要介紹了一文弄懂10大Pandas的索引,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧
    2024-03-03
  • python 常見(jiàn)的反爬蟲(chóng)策略

    python 常見(jiàn)的反爬蟲(chóng)策略

    這篇文章主要介紹了python反爬蟲(chóng)策略,幫助大家更好的理解和使用python 爬蟲(chóng),感興趣的朋友可以了解下
    2020-09-09
  • python中opencv?直方圖處理

    python中opencv?直方圖處理

    這篇文章主要介紹了python中opencv?直方圖處理,直方圖從圖像內(nèi)部灰度級(jí)的角度對(duì)圖像進(jìn)行表述,直方圖是圖像內(nèi)灰度值的統(tǒng)計(jì)特性與圖像灰度值之間的函數(shù),直方圖統(tǒng)計(jì)圖像內(nèi)各個(gè)灰度級(jí)出現(xiàn)的次數(shù),更多相關(guān)內(nèi)容需要的小伙伴可以參考一下
    2022-06-06

最新評(píng)論