I am currently using the Pil library for a project, however i keep running into the same problem which will not let me do anything at all.

def bw_negative(filename):

# Create the handle and then create a list of pixels.
image = Image.open(filename)
pixels = list(image.getdata())


print pixels[0]
print pixels[1]

I have imported the PIL library into the task but i keep getting the same eror stating: "NameError: global name 'Image' is not defined"

I cant find how to fix this any help?

from PIL import Image

did you not read that i said i have imported the PIL library. its telling me that image = Image.open(filename) is not defined

Give the first lines (the import lines)
of your program

Of course I read what you wrote. I know perfectly well what your problem is.

ok sweet its written as test cases first case it to turn a .bmp image from black and white to negative. We have been given some snippets which we need to use.

def bw_negative(filename):
"""
This function creates a black and white negative of a bitmap image
using the following parameters:
filename is the name of the bitmap image
"""

# Create the handle and then create a list of pixels.
image = Image.open(filename)
pixels = list(image.getdata())


print pixels[0]
print pixels[1]

pixels[100] = 200

image.putdata(pixels)
image.save('new.bmp')
Thats all the code which has been given.

After that code has been put after the test cases:
if __name__ == '__main__':

from PIL import *

## Creates the black and white negative bitmap
bw_negative('farmland.bmp')

I have tried it with the from PIL import Image but i kept getting teh same error for a while untill i edited out some code and then it would jsut not do anything.

Rewrite you code this way to make the import global ...

from PIL import Image

def bw_negative(filename):
    """
    This function creates a black and white negative of a bitmap image
    using the following parameters:
    filename is the name of the bitmap image
    """

    # Create the handle and then create a list of pixels.
    image = Image.open(filename)
    pixels = list(image.getdata())

    print pixels[0]
    print pixels[1]

    pixels[100] = 200

    image.putdata(pixels)
    image.save('new.bmp')

if __name__ == '__main__':

    ## Creates the black and white negative bitmap
    bw_negative("french_flag.bmp")

However it will not create a black and white image.

I did figure out my problem, I had to reinstall the library. Its all fine now i just wanted that code to work for me, my challenge it to create that negative image so thats all i needed thanks guys.

Rewrite you code this way to make the import global ...

Actually, the way he wrote it is still the same, imports are the same wherever you put them, as long as you just import them before you use them.
That's why the problem was perplexing.

Actually, the way he wrote it is still the same, imports are the same wherever you put them, as long as you just import them before you use them.
That's why the problem was perplexing.

You are correct of course, but generally one tries to get the imports out of the way in the beginning of the code. Just an old habit from the C days.

One thing you don't want to do is to import inside a function that is called frequently.

Actually I surprise myself as I changed two things, and got one code to work.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.