By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
435,404 Members | 1,873 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 435,404 IT Pros & Developers. It's quick & easy.

[NameError] global name 'ImageDraw' is not defined

P: 3
Hi guys,

I am devoloping with Pillow and (Tkinter,BmpImagePlugin,cStringIO,subprocess,ctype s,re .. n) modules

I am using this way when if this situation show me AttributeError

Expand|Select|Wrap|Line Numbers
  1. from PIL import *
I solve this code line with try-except

Expand|Select|Wrap|Line Numbers
  1. try:
  2.     from PIL import *
  3. except AttributeError: #module has no attribute ImageN
  4.     import Image
  5.     import ImageDraw
  6.     import ImageFont
  7.  
My orginal code part

Expand|Select|Wrap|Line Numbers
  1. class capsGen(object):
  2.     def __init__(self):
  3.         pass
  4.  
  5.     def videoGen(self,path):
  6.         iv = InputVideoStream()
  7.         iv.open(path)
  8.         self.videoHead(path)
  9.         print self.topDuration
  10.         frameDiff = list(enumerate(iv.readframe())) #bmp -> [io][1]
  11.         self.totalFrame = frameDiff [-1][0]
  12.         #16 imgs
  13.         self.genImgs = []
  14.         curImg = 0
  15.         while True:
  16.             if curImg < 16:
  17.                 self.genImgs.append(randint(1,self.totalFrame))
  18.                 curImg = curImg + 1 
  19.             else:
  20.                 break
  21.  
  22.  
  23.         try:
  24.             src = PIL.Image.open("src.png")
  25.             ciz = PIL.ImageDraw.draw(src)
  26.             ft = PIL.ImageFont.truetype("arial.ttf",32)
  27.             ciz.text((190,15),self.fileName,font=ft) #fileName
  28.             src.save("1.png")
  29.  
  30.  
  31.             #print frameDiff[5][0]
  32.             for i in self.genImgs:
  33.                 imj = PIL.Image.open(StringIO.StringIO(frameDiff[i][1])) #base io -> [capsNo][1]
  34.                 imj.save("%s.png"%i)
  35.         except NameError:
  36.             src = Image.open("src.png")
  37.             ciz = ImageDraw.draw(src)
  38.             ft = ImageFont.truetype("arial.ttf",32)
  39.             ciz.text((190,15),self.fileName,font=ft) #fileName
  40.             src.save("1.png")
  41.  
  42.  
  43.             #print frameDiff[5][0]
  44.             for i in self.genImgs:
  45.                 imj = Image.open(StringIO.StringIO(frameDiff[i][1])) #base -> [io][1]
  46.                 imj.save("%s.png"%i)
  47.  
  48.  
  49.  
My error,

Expand|Select|Wrap|Line Numbers
  1. Traceback (most recent call last):
  2.   File "o.py", line 275, in (module)
  3.     run = capsGen()
  4.   File "o.py", line 42, in __init__
  5.     self.videoGen() #for developers
  6.   File "o.py", line 157, in videoGen
  7.     ciz = ImageDraw.draw(src)
  8. NameError: global name 'ImageDraw' is not defined
  9.  
But I installed Pillow module and I create empty python file in import PIL or Image..N module are working. Its isn't just working on the my_project (o.py)

Than you for interest.
Good works.
Apr 22 '14 #1
Share this Question
Share on Google+
2 Replies


Expert 100+
P: 621
It could be many things, including permission problems with the image file. Try the following code ("chairs.png", from Manning's book, is attached for testing). Substitute your file name(s) and see what works and what does not, and then you can hopefully adapt to your program.
Expand|Select|Wrap|Line Numbers
  1. import Image, ImageDraw
  2.  
  3. image_name="chairs.png"
  4. im = Image.open(image_name)
  5. draw = ImageDraw.Draw(im)
  6. draw.line((0, 0) + im.size, fill=255)
  7. draw.line((0, im.size[1], im.size[0], 0), fill=128)
  8.  
  9. im.show()
Attached Images
File Type: png chairs.png (62.2 KB, 235 views)
Apr 22 '14 #2

P: 3
Hello @dwblas,
I tried reference name like to you said to me.

And, I did invalid move, I forget Tkinter module in Image attribute

I changed this from Tkinter import *

Expand|Select|Wrap|Line Numbers
  1. import Tkinter as tk
  2.  
And, my project file in some .open functions, I changed to these names.

Thank for interest.
Good works.
Apr 23 '14 #3

Post your reply

Sign in to post your reply or Sign up for a free account.