How to Pass filename as variable in python during reading a file?

Submitted 4 years, 7 months ago
Ticket #120
Views 380
Language/Framework Python
Priority Medium
Status Closed

I have an image file in the following location.

'D:\\projects\\IMG_2012-09-26T09-00-07.jpg'

While I am trying to read by directly giving the filename it reads the image properly.

Working code

mm = cv2.imread( r'D:\\projects\\IMG_2012-09-26T09-00-07.jpg' )

But when I pass the path as variable, it is showing NoneType Object and is not reading the actual contents of file.

Failed code

tt = 'D:\\projects\\IMG_2012-09-26T09-00-07.jpg'

mm = cv2.imread( tt )

how to pass the name as a variable as it is not static. How to achieve this ?

Really need your support !!!

Submitted on Sep 08, 20

Can you close the ticket? If answer below helped you to move forward? - Vengat 4 years, 6 months ago
add a comment

2 Answers

Here in this code first we have to  import cv2 package and after reading the imagefile we have to use function to display that image 

from cv2 import cv2
tt = 'D:\\projects\\IMG_2012-09-26T09-00-07.jpg'
mm = cv2.imread( tt )
cv2.imshow('image', mm)
cv2.waitKey(10000000)
cv2.destroyAllWindows()

Submitted 4 years, 7 months ago


Verified

Did you try?

tt = r'D:\\projects\\IMG_2012-09-26T09-00-07.jpg'

mm= cv2.imread( tt )

Submitted 4 years, 6 months ago


Latest Blogs