Score:1

Python file not working in terminal, only in pycharm

cn flag

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.

cocomac avatar
cn flag
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
Gurseerit avatar
cn flag
Sorry, it didn't work. @cocomac
cocomac avatar
cn flag
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`
Gurseerit avatar
cn flag
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
cocomac avatar
cn flag
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.
Greenonline avatar
us flag
Just out of interest... when you run it from a terminal, what directory are you in? Is it the same directory as the script resides or a different directory? Also, are you using the "Run from terminal" menu option from within PyCharm, or a standard xterm? Using the "Run from terminal" option I was unable to reproduce the issue.
mangohost

Post an answer

Most people don’t grasp that asking a lot of questions unlocks learning and improves interpersonal bonding. In Alison’s studies, for example, though people could accurately recall how many questions had been asked in their conversations, they didn’t intuit the link between questions and liking. Across four studies, in which participants were engaged in conversations themselves or read transcripts of others’ conversations, people tended not to realize that question asking would influence—or had influenced—the level of amity between the conversationalists.