I want to make changes to my image and want to implement some of the functions that are present in ImageMagick. How can I do the same in Python ? Is there a python binding available?
Here is the link of textcleaner in imagemagick.
http://www.fmwconcepts.com/imagemagick/textcleaner/index.php
Textcleaner is my script.
To run any (ImageMagick) bash shell script from Python, you would have to use the subprocess module. An example of this is at How to call a shell script from python code?
Here is an example that you would type in a terminal window or put in a PHP exec() command or from a python subprocess module.
Input:
textcleaner -g -e stretch -f 25 -o 10 -s 1 twinkle.jpg result.jpg
Related
I'm following along with Google's Python class, and the person in the videos always runs his scripts from the interactive session in command line using "./". Whenever I try it, I just get a syntax error. How can I use ./ to run scripts? I'm using Windows 10
To run a script from the command line you need to use the syntax
python3 script.py
Now on Unix systems, it's possible to add a shebang to the first line of the script as followings
#!/usr/bin/env python3
This then allows the shell syntax './name.py' to work. But windows doesn't have this mechanism. Instead, you need to create an 'association' between the .py extension and the python executable ('right click', 'open with'). Or just use the full syntax. Both require the python executable to be in your path, and generally on windows both python 2 and 3 will have the same executable name
If I want to run a specific command (with arguments) under Software Collections, I can use this command:
scl enable python27 "ls /tmp"
However, if I try to make a shell script that has a similar command as its shebang line, I get errors:
$ cat myscript
#!/usr/bin/scl enable python27 "ls /tmp"
echo hello
$ ./myscript
Unable to open /etc/scl/prefixes/"ls!
What am I doing wrong?
You should try using -- instead of surrounding your command with quotes.
scl enable python27 -- ls /tmp
I was able to make a python script that uses the rh-python35 collection with this shebang:
#!/usr/bin/scl enable rh-python35 -- python
import sys
print(sys.version)
The parsing of arguments in the she-bang command is not really defined. From man execve:
The semantics of the optional-arg argument of an interpreter script vary across implementations. On Linux, the entire string following the interpreter name is passed as a single argument to the interpreter, and this string can include white space. However, behavior differs on some other systems. Some systems use the first white space to terminate optional-arg. On some systems, an interpreter script can have multiple arguments, and white spaces in optional-arg are used to delimit the arguments.
No matter what, argument splitting based on quote sis not supported. So when you write:
#!/usr/bin/scl enable python27 "ls /tmp"
It's very possible that what gets invoked is (using bash notation):
'/usr/bin/scl' 'enable' 'python27' '"ls' '/tmp"'
This is probably why it tries to open the "ls file at /etc/scl/prefixes/"ls
But it is just as likely that the shebang evaluates to:
'/usr/bin/scl' 'enable python27 "ls /tmp"'
And that would fail since it wont be able to find a command named enable python27 "ls /tmp" for scl to execute.
There's a few workarounds you can use.
You can call your script via scl:
$ cat myscript
#!/bin/bash
echo hello
$ scl enable python27 ./myscript
hello
You can also use the heredoc notation, but it might lead to subtle issues. I personally avoid this:
$ cat ./myscript
#!/bin/bash
scl enable python27 -- <<EOF
echo hi
echo \$X_SCLS
EOF
$ bash -x myscript
+ scl enable python27 --
hi
python27
You can see one of the gotcha's already: I had to write \$X_SCLS to access the environment variable instead of just $X_SCL.
Edit: Another option is two have two scripts. One that has the actual code, and the second that simply does scl enable python27 $FIRST_SCRIPT. Then you wont have to remember to enter scl ... manually.
The software collections documentation may also be helpful. In particular you can try
cat myscript.sh | scl enable python27 -
As well as permanently enabling a software collection
source scl_source enable python27
./myscript.sh
I am trying to start mjpg-streamer from a python script on the raspberry pi. The instructions for how to start it from the command line are here and consist of running
export LD_LIBRARY_PATH=. ./mjpg_streamer -o "output_http.so -w ./www"
-i "input_raspicam.so"
from /var/www/mjpg-streamer/mjpg-streamer-experimental. When I do it in the terminal, it works fine.
However, I am trying to run it using subprocess.call like this:
subprocess.call('export LD_LIBRARY_PATH=.', shell=True, cwd='/var/www/mjpg-streamer/mjpg-streamer-experimental')
subprocess.call('./mjpg_streamer -o "output_http.so -w ./www" -i "input_raspicam.so -x 640 -y 480 -fps 15 -vf -hf"', shell=True, cwd='/var/www/mjpg-streamer/mjpg-streamer-experimental')
And that is giving me the error:
MJPG Streamer Version: svn rev: ERROR: could not find input plugin
Perhaps you want to adjust the search path with:
# export LD_LIBRARY_PATH=/path/to/plugin/folder
dlopen: input_raspicam.so: cannot open shared object file: No such file or directory
I'm guessing it is because the first command doesn't provide the relevant link to the plugin? I'm not entirely sure of how these commands work anyway, so any insight into that would also be helpful!
I have also tried using os.system to run these commands and have received the same error.
I'm sure I'm doing something silly, so thanks in advance for your patience!
I have a python script that I want to execute from the terminal, but I don't want to use the command pythonscriptName.py, instead, I'd like to just type scriptName. Is that possible? If it is, how?
I had a look in here and here, but it doesn't work (probably because I'm on a different os).
I'm using python 2.7.9 on osx Yosemite (10.10.3).
Put this as the first line in your Python script:
#!/usr/bin/python
(or wherever your Python interpreter lives).
Then give the script the executable bit:
chmod +x scriptName.py
Now you should be able to execute it like ./scriptName.py. You can then put a symlink without the .py extension somewhere in your path.
Let's say I've got this one-line Python module called say_hello.py.
print 'Hello World'
How can I make the script executable from any location in my terminal? That is, having Hello World printed outside the Python interpreter anywhere on my system. I'm running on OS X Mavericks.
General *nix answer
First line of script should look something like:
#!/usr/bin/python
although the exact path may be different on your system. Then, make the script executable and put it somewhere in your PATH.
Add as the first line of your script:
#!/usr/bin/env python
or, for a python3 script:
#!/usr/bin/env python3
The shell (actually the kernel) will use the first Python/Python3 interpreter found in your $PATH.