OpenCV using command line argument for input image (Python) - python

I am trying to load an image from a file specified on the command line then process it using the OpenCV command HoughCircles. What happens is that I am opening the file:
img = cv2.imread(argv[0],0)
Then trying to use the following function:
def _getCircles(img):
_circles = cv2.HoughCircles(img,cv2.cv.CV_HOUGH_GRADIENT,1,20,param1=50,param2=30,minRadius=1,maxRadius=20)
But returns the error:
cv2.error: error: (-206) Unrecognized or unsupported array type in function cvGetMat
However if I load the file directly ie change argv[0] to the explicit filename everything works perfectly. Any ideas?

In python and most other languages argv[0] contains the program name. Try using argv[1] to get the correct result.
Here is some Python Documentation to help you. Python has an amazing library of documentation I highly recommend using it.

Related

Python: Popen-ing command that expects a file, replace the file with a string?

I'm using python to run "drush scr" (drupal 7 command line) with Popen. Typical usage is "drush scr file.php", where file.php is a file containing php code. I use my python code to generate the php code as a string subsequently sent to file.php, and I don't want to have to create file.php just to then immediately send it to "drush scr file.php".
I've tried
sub=subprocess.Popen(['drush','scr'],stdin=subprocess.PIPE)
ret=sub.communicate(input=createPHP())
where createPHP() creates the code that otherwise would be sent to file.php and then simply used as subprocess.Popen(['drush','scr','file.php']) to minimic command $drush scr file.php
def createPHP():
#dynamically create php code as multi-line phpstring#
return phpString
sub=subprocess.Popen(['drush','scr'],stdin=subprocess.PIPE)
ret=sub.communicate(input=createPHP())
print(ret)
This code throws an error from drush but also returns (None,None) from the print command.
The following code correctly executes the drush command:
f=open("file.php","w+")
f.write(createPHP())
f.close()
sub=subprocess.Popen(['drush','scr','file.php'])
ret=sub.communicate()
print(ret)
and returns (None,None). The latter code definitely works, but I would prefer to not be creating and closing files if possible, and I feel like I am just not using Popen correctly.

Reading string arguments passed from batch file to python script

I have seen multiple posts on passing the string but not able to find good solution on reading the string passed to python script from batch file. Here is my problem.
I am calling python script from batch file and passing the argument.
string_var = "123_Asdf"
bat 'testscript.py %string_var%'
I have following in my python code.
import sys
passed_var = sys.argv[1]
When I run the above code I always see below error.
passed_var = sys.argv[1]
IndexError: list index out of range
Has anyone seen this issue before? I am only passing string and expect it to be read as part of the first argument I am passing to the script.
Try this:
import sys
for x,parameter in enumerate(sys.argv):
print(x, parameter)
If I have read your question and its formatting correctly, I think your .bat file should read:
Set string_var="123_Asdf"
"D:\BuildTools\tools\python27\python.exe" testscript.py %string_var%
Or better still:
Set "string_var=123_Asdf"
"D:\BuildTools\tools\python27\python.exe" testscript.py "%string_var%"
Where %string_var% can be passed with or without its enclosing doublequotes.
Your batch file should be a bit simpler, make sure you have your PATH set correctly or else this won't work.
python testscript.py [argument]

How to copy a math output to clipboard in Python?

I'm super new to Python so I'm wondering if someone can help me out or link me to an appropriate post that explains this?
What I would like to do is
9999**9999
in Python Terminal, then copy the output directly to my clipboard or sent to a file.
I tried in Batch using
py 9999**9999 >>pythonoutput.txt
but only got an error of
python.exe: can't open file '9999**9999': [Errno 22] Invalid argument
and not sure how I could make that work either.
Any ideas? Cheers
Here's how to write (append) to a file:-
obj=open("yourfile.txt","a+") #open a reference to your file, in append mode. (Use 'w' for write, and 'r' for read if you ever need to)
obj.write("your chars, numbers or whatever here") #use this as many times as you want before closing
obj.close() #close your reference once you're done
Try using:
python -c print(9999*9999) > outfile.txt
You might want to use py instead of python there since you seem to have your executable renamed.
Sent to result to file is much easier than to clipboard.
In the python terminal,you can do this:
with open("/home/my/output","w") as file:#start a file object for writing
file.write(str(9999*9999))#write the content

Using python to carry out actions in the windows cmd

I have a program, which saves a canvas into a postscript file. The program then opens the file with IrfanView, where I can manually save it as a .png and then I can run another function from python, which does another operation with it and saves it as a .png again. My question is whether there is a way to either cut out the middle manual bit (where I have to click the save as button) or whether the saving from IrfanView can all be done through python code?
This far I've found out that I cannot save the canvas and whatever is on it (im using turtles) can only be saved using postscript.
Also converting postscript to png or jpeg from within python also seems to be a bit of a tall order.
Note: Essentially I use Irfan to do the postscript to .png conversion, but I would like to hide this step of the process from the user, so it would be nice if the program could do it for me.
New Note: I have tried to use the python subprocess module to make a call to the cmd and use that to convert, but whenever I attempt to run the .Popen or the .call function I get an error - Access denied or file not found, either way the commands don't want to run from the python program. I even tried just opening a file from python, through the cmd only to get an error (the same command works when typed directly into the cmd):
WindowsError: [Error 193] %1 is not a valid Win32 application
Assuming that you have a postscript file named saved.ps that you want to convert to a png file with Ghostscript using the device pngalpha, you could do:
gspath = "/path/to/gs" # would be gspath="c:\path\to\gswin32c" on Windows...
infile = "saved.ps"
outfile = "output.png"
gs = subprocess.Popen(["gs", "-o", "output.png", "-sDEVICE=pngalpha",
"-dBatch", infile], executable=gspath,
stdout=subprocess.PIPE, stderr = subprocess.PIPE)
out, err = gs.communicate()
if gs.returncode != 0:
# do error processing, at least display out and err

How to Accept Command Line Arguments With Python Using < [duplicate]

This question already has answers here:
Python command line 'file input stream'
(3 answers)
Closed 8 years ago.
Is it possible to run a python script and feed in a file as an argument using <? For example, my script works as intended using the following command python scriptname.py input.txt and the following code stuffFile = open(sys.argv[1], 'r').
However, what I'm looking to do, if possible, is use this command line syntax: python scriptname.py < input.txt. Right now, running that command gives me only one argument, so I likely have to adjust my code in my script, but am not sure exactly how.
I have an automated system processing this command, so it needs to be exact. If that's possible with a Python script, I'd greatly appreciate some help!
< file is handled by the shell: the file doesn't get passed as an argument. Instead it becomes the standard input of your program, i.e., sys.stdin.
When you use the < operator in a shell you are actually opening the file and adding its contents to your scripts stdin
However there is is a python module that can do both. It's called fileinput.
https://docs.python.org/2/library/fileinput.html
It was shown in this post
How do you read from stdin in Python?
You can use the sys module's stdin attribute as a file like object.

Categories

Resources