【Python OPENCV】 argparse load image

 """

Example to load an image using argparse

"""

# Import the required packages

import argparse

import cv2

# We first create the ArgumentParser object# The created object 'parser' will have the necessary information

# to parse the command-line arguments into data types.

parser = argparse.ArgumentParser()

# We add 'path_image' argument using add_argument() including a help. The type of this argument is string (by default)

parser.add_argument("path_image", help="path to input image to be displayed")

# The information about program arguments is stored in 'parser'

# Then, it is used when the parser calls parse_args().

# ArgumentParser parses arguments through the parse_args() method:

args = parser.parse_args()

print(args.path_image)

# We can now load the input image from disk:

image = cv2.imread(args.path_image)

# Parse the argument and store it in a dictionary:

args = vars(parser.parse_args())

# Now, we can also load the input image from disk using args:

image2 = cv2.imread(args["path_image"])

# Show the loaded image:

cv2.imshow("loaded image", image)

cv2.imshow("loaded image2", image2)

# Wait until a key is pressed:

cv2.waitKey(0)

# Destroy all windows:

cv2.destroyAllWindows()

No comments:

The Win Machine

The Road to Hell is Paved with Intervention ͏     ­͏     ­͏     ­͏     ­͏     ­͏     ­͏     ­͏     ­͏     ­͏     ­͏     ­͏     ­͏     ­͏    ...

Contact Form

Name

Email *

Message *