473,387 Members | 1,517 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,387 software developers and data experts.

[NameError] global name 'ImageDraw' is not defined

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
2 9373
dwblas
626 Expert 512MB
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, 353 views)
Apr 22 '14 #2
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

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

Similar topics

5
by: NetKev | last post by:
I added a function 'warn_Admin' and defined it just before another function 'process_log'. 'process_log' calls this warn_Admin' function. However, when it gets called i get the following error...
8
by: Evan | last post by:
Hi I have a short script that makes 2 calls to methods in another script as follows: import canPlaces as canp callOne=canp.addMe(3,5) callTwo=canp.estocStn()
2
by: pythonnewb | last post by:
I am fairly new to programming but have some very basic Java background. I am just learning python and tried to make a module that would allow me to create a file containing an address book. I was...
1
by: Lalit | last post by:
Hi I am very new to web development. I started with Pylons. I am using http://www.rexx.com/~dkuhlman/pylons_quick_site.html as reference to create a sample web page using pylons. I got stuck...
3
by: Netwatcher | last post by:
im try to activate the tutorial code for wxPython, it is working properly when i tell it to import * from wxPython.wx but with an error about the package, i've done as i was told to change the...
2
by: Geared | last post by:
Hello I'm new to python (been using it all of 2 weeks) and I'm trying to write a program that compares the result of an equation using two different starting numbers that the user chooses. It also...
4
by: jorgejch | last post by:
Hello, I've started with python (3) recently. Initialy only for scripting. Now I'm trying the object oriented bit. I'm getting the following error message <Atom.Atom object at 0x7f0b09597fd0>...
1
by: Terry Fernandez | last post by:
Can you help me with the NameError? def addExample(self, klass, words): """ * TODO * Train your model on an example document with label klass ('pos' or 'neg') and * words, a...
3
by: dashtons7 | last post by:
I get the following message when I try to run the below program: builtins.NameError: global name 'gallons' is not defined def main(): square_feet = float(input("Enter the square feet of the...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.