1

The program takes a picture and saves it to a directory named images.

#!/usr/bin/env/python
import cv2
import datetime
filename = datetime.datetime.now()
cam = cv2.VideoCapture(0)
s, img = cam.read()
if s:
    cv2.imwrite("images/" + str(filename) + ".jpg",img)
    

Running it from pycharm works like a charm (pun not intended) , but running it from terminal fails. The light for the camera flashes, but when I go to my files the file isn't there.

6
  • 1
    Welcome to Ask Ubuntu. This question might be better on Stack Overflow, but a few thoughts. I'm not sure how you are running it, but the first line should be #!/usr/bin/env python3 if it is a Python 3 script, which it seems like it is (note the space before python3). Also, you could try replacing "images/" with "./images/" and see what happens
    – cocomac
    Oct 8, 2021 at 20:21
  • 1
    Sorry, it didn't work. @cocomac
    – Gurseerit
    Oct 8, 2021 at 20:36
  • A few ideas... (1) replace cv2.imwrite(...) with cv2.imwrite(r"/home/you/testimg.jpg", img), and see if the file appears. (2) If it works in PyCharm, the code is probably mostly fine. Specifically, how are you running your code? I would do python3 myfile.py
    – cocomac
    Oct 8, 2021 at 20:44
  • The first method worked, but I'd had rather it a specific name and directory if that's ok. I use python3 <name>.py @cocomac
    – Gurseerit
    Oct 8, 2021 at 20:47
  • Glad it worked. If it worked, the solution is pretty easy. If you replace the line with cv2.imwrite("/home/you/" + str(filename) + ".jpg", img), it should work.
    – cocomac
    Oct 8, 2021 at 20:49

0

You must log in to answer this question.

Browse other questions tagged .