简体   繁体   中英

i save files(.jpg) in another folder and i want to use that files in my python program. how do i import them

I want to know how to import files.jpg in another folder to use in my program.

from myfolder import picture.jpg
import cv2
img = cv2.imread("picture.jpg", 1)
cv2.imshow("Image",img)
cv2.waitKey(0)
cv2.destroyAllWindows()

You only use import to interact with python modules (.py files). To open an image you just use appropriate image path in cv.open For example:

import cv2
img = cv2.imread("myfolder/picture.jpg", 1)
cv2.imshow("Image",img)
cv2.waitKey(0)
cv2.destroyAllWindows()

You do not need to import the.jpg file since it is not a python module. You can access the file directly from the cv2.imread() method by providing the absolute file path instead of just the name of the file.

For example, if picture.jpg was inside Documents/Project, then the code would be as follows:

img = cv2.imread("Documents/Project/picture.jpg", 1)

As for finding the absolute path of a file, the process is different for different operating systems. Here is how to find it on a mac , and here's the same for windows .

Hope this helps!

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