简体   繁体   中英

how get image name by using python open-cv

I am reading one image using cv2.imread() function. I want to store image title that is 10_left.jpeg in one variable and that name write in one CSV file. code is as below:

import cv2

img = cv2.imread('home/pycharmprojects/diabetic/testing/10_left.jpeg')
print("shape of original image ",img.shape)
cv2.imshow('Orgninal Image',img)

To clarify, you want to read the image name from a CSV file and use that to open the image with open-cv? In that case, you'll just need to parse your csv file with csv module . If your CSV is just rows of image names, this is fairly easy.

import cv2
import csv

ifile = open(‘test.csv’, “rb”)
reader = csv.reader(ifile)

imageNames = []
for row in reader
    imageNames.append(row)

img = cv2.imread(imageNames[0])
print("shape of original image ",img.shape)
cv2.imshow('Original Image',img)
cv2.waitkey(0)

If your csv is more involved, the link above should help get you on your way.

You can create a custom class for image object and store actual image and file name in separate variables.

class MyImage:
    img = None
    name = ''

    def __init__(self,name):
        self.name = name
        self.img = cv2.imread(name)

myImage = MyImage('10_left.jpeg')

To write the name in CSV file, simple get the value of myImage.name attribute

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