Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game. |
| Notices |
Welcome to LinuxQuestions.org, a friendly and active Linux Community.
You are currently viewing LQ as a guest. By joining our community you will have the ability to post topics, receive our newsletter, use the advanced search, subscribe to threads and access many other special features. Registration is quick, simple and absolutely free. Join our community today!
Note that registered members see fewer ads, and ContentLink is completely disabled once you log in.
Are you new to LinuxQuestions.org? Visit the following links:
Site Howto |
Site FAQ |
Sitemap |
Register Now
If you have any problems with the registration process or your account login, please contact us. If you need to reset your password, click here.
Having a problem logging in? Please visit this page to clear all LQ-related cookies.
Get a virtual cloud desktop with the Linux distro that you want in less than five minutes with Shells! With over 10 pre-installed distros to choose from, the worry-free installation life is here! Whether you are a digital nomad or just looking for flexibility, Shells can put your Linux machine on the device that you want to use.
Exclusive for LQ members, get up to 45% off per month. Click here for more info.
|
 |
05-01-2005, 08:01 AM
|
#1
|
|
Member
Registered: Nov 2002
Location: Central VA
Distribution: Ubuntu/Debian
Posts: 228
Rep:
|
Help with small Python Script
Gretting all.
I have a need to convert PNG files to EPS format. I did some searching and found som python commands I can execute from the python interpreter and they seem to work just fine.
Code:
>>> import Image
>>> im=Image.open("logo_nosonis.png")
>>> im.save("logo_nosonis.eps")
I have no experience with python.
How would I turn this into a script that could take the base file name as an argument and spit out the eps file?
Thanks,
Chris
|
|
|
|
05-01-2005, 10:20 AM
|
#2
|
|
Member
Registered: Aug 2003
Location: Edinburgh, Scotland
Distribution: Gentoo
Posts: 246
Rep:
|
Try something along the lines of:
Code:
#!/usr/bin/env python
import sys
import Image
def main():
if len(sys.argv) < 2:
print """Usage:
%s input_file [output_file]""" % (sys.argv[0],)
return
if len(sys.argv) == 2:
sys.argv.append(sys.argv[1].replace(".png",".eps"))
img = Image.open(sys.argv[1])
img.save(sys.argv[2])
if __name__ == '__main__':
main()
And if you "chmod u+x" the file then executing it (like a normal executable) will run it fine (although I've never actually tested the code)
Last edited by nhs; 05-01-2005 at 10:22 AM.
|
|
|
|
05-01-2005, 12:13 PM
|
#3
|
|
Member
Registered: Nov 2002
Location: Central VA
Distribution: Ubuntu/Debian
Posts: 228
Original Poster
Rep:
|
Thanks for the response.
I do get an error when trying to execute.
I named the script png2eps.py.
Here's what I get.
Code:
gcnpd:/home/user/temp# ./png2eps.py NPMTV_FP_EX.PNG
File "./png2eps.py", line 9
return
^
SyntaxError: invalid syntax
Any advice?
|
|
|
|
05-01-2005, 12:19 PM
|
#4
|
|
Member
Registered: Aug 2003
Location: Edinburgh, Scotland
Distribution: Gentoo
Posts: 246
Rep:
|
Sorry, my error. Use instead.
|
|
|
|
05-01-2005, 03:18 PM
|
#5
|
|
Member
Registered: Nov 2004
Distribution: KDE Neon User edition; Manjaro; OpenSUSE Leap
Posts: 298
Rep:
|
I've been using python for a while and seen "if __name__ == '__main__'" but what is the point of it? You could just call the function main without that line.
|
|
|
|
05-01-2005, 03:49 PM
|
#6
|
|
Member
Registered: Aug 2003
Location: Edinburgh, Scotland
Distribution: Gentoo
Posts: 246
Rep:
|
The advantage isn't so great in this example but the idea is that it allows making an executable script which is also usable as a module. In this case another program could "import png2eps" (where __name__ != '__main__') and it won't attempt to execute main() at load. The script could then set up sys.argv and call png2eps.main(). This is fairly pointless here but it is not uncommon to have an executable script structured so that it can be imported. The usage is varied (think code + test case) or a program+lib in one file where the other functions are useful in a modular context. In any case it's never the decision of the programmer how the code will be used and it's generally polite to make sure that the right thing happens if the module is imported regardless of whether it makes sense or not.
|
|
|
|
05-01-2005, 04:06 PM
|
#7
|
|
Member
Registered: Nov 2004
Distribution: KDE Neon User edition; Manjaro; OpenSUSE Leap
Posts: 298
Rep:
|
Ah ok cheers nhs. Thanks for clearing that up for me.
|
|
|
|
05-02-2005, 07:02 AM
|
#8
|
|
Member
Registered: Nov 2002
Location: Central VA
Distribution: Ubuntu/Debian
Posts: 228
Original Poster
Rep:
|
nhs,
Here's my script. I culled the bits trying to make it a module.
Code:
#!/usr/bin/python
import sys
import Image
if len(sys.argv) < 2:
print """Usage:
%s input_file [output_file]""" % (sys.argv[0],)
return None
if len(sys.argv) == 2:
sys.argv.append(sys.argv[1].replace(".png",".eps"))
img = Image.open(sys.argv[1])
img.save(sys.argv[2])
I am still getting the same error:
Code:
user@gcnpd:~/temp$ ./png2eps.py NPMTV_FP_EX.PNG
File "./png2eps.py", line 7
return None
SyntaxError: 'return' outside function
Should that be a quit or exit command? Does python have such a construct?
|
|
|
|
05-02-2005, 08:10 AM
|
#9
|
|
Member
Registered: Nov 2004
Distribution: KDE Neon User edition; Manjaro; OpenSUSE Leap
Posts: 298
Rep:
|
That's because you haven't made it as a function. It needs to start "def func_name():" otherwise return doesn't work. If you don't want it as a function you could use sys.exit() to close the script.
|
|
|
|
All times are GMT -5. The time now is 06:20 PM.
|
|
LinuxQuestions.org is looking for people interested in writing
Editorials, Articles, Reviews, and more. If you'd like to contribute
content, let us know.
|
Latest Threads
LQ News
|
|