7 Replies - 1067 Views - Last Post: 05 October 2012 - 08:18 AM Rate Topic: -----

#1 kehara15  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 42
  • Joined: 13-September 12

Opening Images in Python

Posted 04 October 2012 - 07:23 PM

I have an assignment. And its asking me to do this:

Write a function called openAll that has as input arguments the file names of three image files to be opened, then:

1. Creates the image for each file.

2. Displays each image.

Test case

Input: argument#1 orangefish.gif, argument#2 purplefish.gif, argument#3 purdue.gif

SO far I have this code but it is not showing the pictures:

def openAll(x,y,z):
    x = 'orangefish.gif'
    y = 'purplefish.gif'
    z = 'purdue.gif'
    myImage = Image.open(x)
    myImage = Image.open(y)
    myImage = Image.open(z)
    myImage.show()


Any Suggestions?

Is This A Good Question/Topic? 0
  • +

Replies To: Opening Images in Python

#2 Simown  Icon User is offline

  • Blue Sprat
  • member icon

Reputation: 319
  • View blog
  • Posts: 650
  • Joined: 20-May 10

Re: Opening Images in Python

Posted 05 October 2012 - 01:31 AM

Are you using PIL to do this? I'm not sure Python shows images by default.

Anyway, a few problems:

x, y and z are parameters to the function but you assign values to x, y and z on lines 2-4 anyway, so they are not used as "input arguments". So to call your function, it should be
openAll('orangefish.gif', 'purplefish.gif', 'purdue.gif')

And work with x, y and z within the function. Your code to open an image seems fine if you are using the image library but be careful, you assign 3 different string to the same value one after the other, and overwrite the previous value!

To open all 3, you need 3 different image variables and 3 show statements (or a loop).

Could the problem be that you aren't even calling the method? If that's all your code, that's just a function definition, you need to call the method as described above explicitly.

This post has been edited by Simown: 05 October 2012 - 01:32 AM

Was This Post Helpful? 0
  • +
  • -

#3 kehara15  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 42
  • Joined: 13-September 12

Re: Opening Images in Python

Posted 05 October 2012 - 02:11 AM

Yes i am using PIL.

What do you mean by this, "you need to call the method as described above explicitly"?
Was This Post Helpful? 0
  • +
  • -

#4 Simown  Icon User is offline

  • Blue Sprat
  • member icon

Reputation: 319
  • View blog
  • Posts: 650
  • Joined: 20-May 10

Re: Opening Images in Python

Posted 05 October 2012 - 03:04 AM

Well, you have your function definition which just describes the function, but does nothing:
def openAll(x,y,z):
    ...


And then you need to call it to invoke the functionality after the function definition:
openAll('orangefish.gif', 'purplefish.gif', 'purdue.gif')

This post has been edited by Simown: 05 October 2012 - 03:05 AM

Was This Post Helpful? 0
  • +
  • -

#5 kehara15  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 42
  • Joined: 13-September 12

Re: Opening Images in Python

Posted 05 October 2012 - 05:13 AM

So do I call this at the end of my function like this:

def openAll(filename,filename1,filename2):
    filename = 'orangefish.gif'
    filename1 = 'purplefish.gif'
    filename2 = 'purdue.gif'
    myImage = Image.open(filename)
    myImage1 = Image.open(filename1)
    myImage2 = Image.open(filename2)
    myImage.show()
    myImage1.show()
    myImage2.show()
openAll(orangefish.gif,purplefish.gif,purdue.gif)
    




Or do I just call it when I run the module?
Was This Post Helpful? 0
  • +
  • -

#6 baavgai  Icon User is offline

  • Dreaming Coder
  • member icon

Reputation: 5780
  • View blog
  • Posts: 12,595
  • Joined: 16-October 07

Re: Opening Images in Python

Posted 05 October 2012 - 06:11 AM

def openAll(filename,filename1,filename2):
	# here you're writing over the value passed with 'orangefish.gif'
	filename = 'orangefish.gif'



You want to call the function with strings and generally don't mess with those values once you have them.

You're so close, so:
def openAll(filename,filename1,filename2):
	myImage = Image.open(filename)
	myImage1 = Image.open(filename1)
	myImage2 = Image.open(filename2)
	myImage.show()
	myImage1.show()
	myImage2.show()
openAll('orangefish.gif','purplefish.gif','purdue.gif')


Was This Post Helpful? 0
  • +
  • -

#7 kehara15  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 42
  • Joined: 13-September 12

Re: Opening Images in Python

Posted 05 October 2012 - 06:43 AM

So the code you wrote at the bottom is how it should be done. i dont have to assign anything to the inputs filename, filename1 and filename2?

Also for the calling part of the function, can I call the function after I run the module in python or must it be called in the actual function.
Was This Post Helpful? 0
  • +
  • -

#8 Simown  Icon User is offline

  • Blue Sprat
  • member icon

Reputation: 319
  • View blog
  • Posts: 650
  • Joined: 20-May 10

Re: Opening Images in Python

Posted 05 October 2012 - 08:18 AM

The code baavgai wrote is correct. You don't assign to the parameters, they are already set when you pass them in. Consider:
def  showX(x):
  print "x is " + x

Now x is assigned to whatever I enter as a parameter for example:

showX("great")
...
x is great


Or any value I give it:
showX("my favourite letter")
...
x is my favourite letter


So when you write:
openAll('orangefish.gif','purplefish.gif','purdue.gif')

x IS 'orangefish.gif'
y IS 'purplefish.gif'
z IS 'purdue.gif'

inside the function until you end the function or change the value. You don't need to assign x, y and z because they already have taken the value of the parameters.

It's not called inside the function, it's called after the function (see how it isn't indented the same amount as the function code?). The call to the function with specific parameters can either go in the same file, as the example shows, or you can import it into another file. Given your assignment, I think just calling it at the end is fine.

This post has been edited by Simown: 05 October 2012 - 08:20 AM

Was This Post Helpful? 0
  • +
  • -

Page 1 of 1