简体   繁体   中英

How to take input argument and convert that into a string (Python)

In my code, I am reading an image, and then plotting it with matplot. In my plotColor function, I want to create a plt.title(image) where the image that I pass (lena or link) will be displayed as a title.
So if I do plotColor(lena), I will get a plt of lena with lena as a title.
All my pictures are showing correctly with this code and I know I can pass a second literal string argument and use that, but I was wondering if there was a way to accomplish this by passing only 1 argument.

def plotColor(image):
    plt.figure()
    plt.imshow(cv2.cvtColor(image, cv2.COLOR_BGR2RGB))
    plt.title("image")

lena = cv2.imread("lena.png")
link = cv2.imread("link.png")

plotColor(lena)
plotColor(link)

Thanks!

EDIT
I'm having issues with calling upon that path in subsequent functions I've created. (This is based on the proposed solution below)

def grayScale(image):
    output = cv2.cvtColor(image.image, cv2.COLOR_BGR2GRAY)
    return output
def plotGrayscale(image):
    plt.figure()
    plt.imshow(image.image, cmap='gray')
#     plt.title(image.path)

image.path cannot be added when I try: plotGrayscale(grayScale(lena)) so I can't make the title on the output image.

The proposed solution isn't a robust way to achieve what you want; it relies on the fact that lena and link are defined globally. If they moved into another function, for example, then you wouldn't be able to eval() those strings. Additionally it's not really necessary to execute strings as code just to make a string available. If you want to pass a single object around with more than one piece of data/information, simply create a class to hold both things and just pass instances of that class around. Here's a simple example:

class PathedImage:
    def __init__(self, path):
        self.image = cv2.imread(path)
        self.path = path

def plotColor(image):
    plt.figure()
    plt.imshow(cv2.cvtColor(image.image, cv2.COLOR_BGR2RGB))
    plt.title(image.path)

lena = PathedImage("lena.png")
link = PathedImage("link.png")

plotColor(lena)
plotColor(link)

You can get much more fancy here if you wanted; for example, a PathedImage could subclass a numpy array so that it acts exactly like a numpy array, but with a name or path attribute added, for example.

A simple way to achieve what you want is passing the file name as a parameter to your function and opening the image file inside the function. That way, you have the file name string available to clean it up and make it your image title. You can also add a parameter to have a single plotting function and select whether you want to plot color or grayscale images:

import os
def plotImage(file, grayscale=False):
    image = cv2.imread(file)
    filename = os.path.basename(file).split('.')[0]
    plt.imshow(cv2.cvtColor(image, cv2.COLOR_BGR2GRAY if grayscale else cv2.COLOR_BGR2RGB))
    plt.title(filename)

plotImage("lena.png") # plot color image
plotImage("link.png", grayscale=True) # plot grayscale image

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM