4 Replies - 4298 Views - Last Post: 06 April 2010 - 01:24 PM Rate Topic: -----

#1 j1_osu2002  Icon User is offline

  • D.I.C Head

Reputation: 2
  • View blog
  • Posts: 57
  • Joined: 09-February 09

Python Image Manipulation

Posted 06 April 2010 - 06:04 AM

I keep getting this error and I can't figure it out:

Traceback (most recent call last):
File "/Users/j1/Desktop/COMP480/Week 11/imageMaster.py", line 157, in <module>
main()
File "/Users/j1/Desktop/COMP480/Week 11/imageMaster.py", line 128, in main
im = Image.open('/Users/j1/Desktop/COMP480/Week 11/hw7pics/pie.jpg')
NameError: global name 'Image' is not defined

Here is what my code looks like:

#!/usr/bin/env python

# starter program for image manipulation program
#
# written by e.bonakdarian nov 2009
# updated mar 2010
#

from imageMaster import *

# choices
DIAGONAL = 1
STRIPE_HORIZ = 2
STRIPE_VERTICAL = 3
COLOR_BLUE = 4
REVERSE_DIAGONAL = 5



#####################################################
def Diag(img, pixel_color):

    # put a diagonal line across the image in the
    # specified color
    #
    # *** ALREADY WORKING - PLEASE DO NOT MODIFY ***

    pix_ar = img.load() # load image into 2D array
    cols = img.size[0] # number of columns
    rows = img.size[1] # number of rows

    for row in range(rows): # our nested loop

        for col in range(cols):

            if row == col:

                pix_ar[col, row] = pixel_color



#####################################################
def Reverse_Diag(img, pixel_color):

    # put a reverse diagonal line across the image in
    # the specified color (OPTIONAL for extra credit)

    pix_ar = img.load() # load image into 2D array
    cols = img.size[0] # number of columns
    rows = img.size[1] # number of rows

    print 'not finished'



#####################################################
def Stripe_H(img, pixel_color):

    # put a line in the specified color across the
    # image every other line.

    pix_ar = img.load() # load image into 2D array
    cols = img.size[0] # number of columns
    rows = img.size[1] # number of rows

    print 'not finished'


#####################################################
def Stripe_V(img, pixel_color):

    # put a line in the specified color down the
    # image every other line.

    pix_ar = img.load() # load image into 2D array
    cols = img.size[0] # number of columns
    rows = img.size[1] # number of rows

    print 'not finished'


#####################################################
def ColorIt_Blue(img):

    # set all the pixels to red

    pix_ar = img.load() # load image into 2D array
    cols = img.size[0] # number of columns
    rows = img.size[1] # number of rows

    print 'not finished'


#######################################################
def display_menu():

    # displays the menu, and returns the user selection

    print '\t========================================='
    print '\tMenu'
    print '\t========================================='
    print '\t1) Diagonal line' #done for you
    print '\t2) Stripe vertical (every other column)'
    print '\t3) Stripe horizontal (every other row)'
    print '\t4) Color image solid blue'
    print '\t5) Reverse diagonal line (optional)' # optional for extra credit
    print '\t========================================='
    print '\t0) Quit'

    selection = input('\n\tEnter your choice: ')
    return selection


#######################################################
def main():

    # our main program

    choice = display_menu()

    if choice == 0:

        print 'Quitting program.'

    else:

        #load image
        im = Image.open('/Users/j1/Desktop/COMP480/Week 11/hw7pics/pie.jpg')
        pixel_color = 255, 0, 0 # red RGB pixel

        if choice == DIAGONAL:

            Diag(im, pixel_color)

        elif choice == STRIPE_HORIZ:

            Stripe_H(im, pixel_color)

        elif choice == STRIPE_VERTICAL:

            Stripe_V(im, pixel_color)

        elif choice == COLOR_BLUE:

            ColorIt_Blue(im)

        elif choice == REVERSE_DIAGONAL:

            Reverse_Diag(im, pixel_color)

        else:
            print 'Error: Unknown choice: ', choice

        im.show()

if __name__ == '__main__':
    main()



Any help is greatly appreciated!

This post has been edited by j1_osu2002: 06 April 2010 - 06:05 AM


Is This A Good Question/Topic? 0
  • +

Replies To: Python Image Manipulation

#2 girasquid  Icon User is offline

  • Barbarbar
  • member icon

Reputation: 108
  • View blog
  • Posts: 1,825
  • Joined: 03-October 06

Re: Python Image Manipulation

Posted 06 April 2010 - 08:15 AM

You need to import the Image class(which comes from PIL - try reading over this tutorial to see how to use it.
Was This Post Helpful? 1
  • +
  • -

#3 j1_osu2002  Icon User is offline

  • D.I.C Head

Reputation: 2
  • View blog
  • Posts: 57
  • Joined: 09-February 09

Re: Python Image Manipulation

Posted 06 April 2010 - 08:29 AM

View Postgirasquid, on 06 April 2010 - 07:15 AM, said:

You need to import the Image class(which comes from PIL - try reading over this tutorial to see how to use it.


It works fine in the terminal. However, I cannot get it to work for Komodo IDE. I'm running Mac OS X Leopard.
Was This Post Helpful? 0
  • +
  • -

#4 girasquid  Icon User is offline

  • Barbarbar
  • member icon

Reputation: 108
  • View blog
  • Posts: 1,825
  • Joined: 03-October 06

Re: Python Image Manipulation

Posted 06 April 2010 - 12:30 PM

In that case, it sounds like you have a broken install of PIL - which is all too common under OS X.

If you have pip, try installing PIL using this command:
pip install http://dist.repoze.org/PIL-1.1.6.tar.gz 



That should install a version of PIL for you that works on OS X just fine. If you don't have pip, you can install it using easy_install pip.
Was This Post Helpful? 1
  • +
  • -

#5 j1_osu2002  Icon User is offline

  • D.I.C Head

Reputation: 2
  • View blog
  • Posts: 57
  • Joined: 09-February 09

Re: Python Image Manipulation

Posted 06 April 2010 - 01:24 PM

View Postgirasquid, on 06 April 2010 - 11:30 AM, said:

In that case, it sounds like you have a broken install of PIL - which is all too common under OS X.

If you have pip, try installing PIL using this command:
pip install http://dist.repoze.org/PIL-1.1.6.tar.gz 



That should install a version of PIL for you that works on OS X just fine. If you don't have pip, you can install it using easy_install pip.


thanks for the help. I finally got it to work. I had to reconfigure my python.Framework directory. For some reason I have 3 different versions of Python. I have no idea why, but anyhow everything works fine. Thanks for the help.
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1