LinuxQuestions.org
Latest LQ Deal: Latest LQ Deals
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
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


Reply
  Search this Thread
Old 05-01-2005, 08:01 AM   #1
cmfarley19
Member
 
Registered: Nov 2002
Location: Central VA
Distribution: Ubuntu/Debian
Posts: 228

Rep: Reputation: 32
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
 
Old 05-01-2005, 10:20 AM   #2
nhs
Member
 
Registered: Aug 2003
Location: Edinburgh, Scotland
Distribution: Gentoo
Posts: 246

Rep: Reputation: 30
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.
 
Old 05-01-2005, 12:13 PM   #3
cmfarley19
Member
 
Registered: Nov 2002
Location: Central VA
Distribution: Ubuntu/Debian
Posts: 228

Original Poster
Rep: Reputation: 32
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?
 
Old 05-01-2005, 12:19 PM   #4
nhs
Member
 
Registered: Aug 2003
Location: Edinburgh, Scotland
Distribution: Gentoo
Posts: 246

Rep: Reputation: 30
Sorry, my error. Use
Code:
return None
instead.
 
Old 05-01-2005, 03:18 PM   #5
APB_4
Member
 
Registered: Nov 2004
Distribution: KDE Neon User edition; Manjaro; OpenSUSE Leap
Posts: 298

Rep: Reputation: 31
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.
 
Old 05-01-2005, 03:49 PM   #6
nhs
Member
 
Registered: Aug 2003
Location: Edinburgh, Scotland
Distribution: Gentoo
Posts: 246

Rep: Reputation: 30
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.
 
Old 05-01-2005, 04:06 PM   #7
APB_4
Member
 
Registered: Nov 2004
Distribution: KDE Neon User edition; Manjaro; OpenSUSE Leap
Posts: 298

Rep: Reputation: 31
Ah ok cheers nhs. Thanks for clearing that up for me.
 
Old 05-02-2005, 07:02 AM   #8
cmfarley19
Member
 
Registered: Nov 2002
Location: Central VA
Distribution: Ubuntu/Debian
Posts: 228

Original Poster
Rep: Reputation: 32
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?
 
Old 05-02-2005, 08:10 AM   #9
APB_4
Member
 
Registered: Nov 2004
Distribution: KDE Neon User edition; Manjaro; OpenSUSE Leap
Posts: 298

Rep: Reputation: 31
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.
 
  


Reply



Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
python cgi script and premature end of script headers Neruocomp Programming 1 07-28-2005 11:43 AM
help with a very small script hamish Linux - General 3 06-09-2004 02:10 AM
python script LinuxLala Programming 14 04-07-2004 06:19 AM
Small Script msj Linux - Newbie 2 08-27-2001 06:59 AM
small script msj Programming 1 08-26-2001 11:42 AM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 12:45 PM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration