[NameError] global name 'ImageDraw' is not defined

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • fatihmert95
    New Member
    • Apr 2014
    • 3

    [NameError] global name 'ImageDraw' is not defined

    Hi guys,

    I am devoloping with Pillow and (Tkinter,BmpIma gePlugin,cStrin gIO,subprocess, ctypes,re .. n) modules

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

    Code:
    from PIL import *
    I solve this code line with try-except

    Code:
    try:
    	from PIL import *
    except AttributeError: #module has no attribute ImageN
    	import Image
    	import ImageDraw
    	import ImageFont
    My orginal code part

    Code:
    class capsGen(object):
    	def __init__(self):
    		pass
    
    	def videoGen(self,path):
    		iv = InputVideoStream()
    		iv.open(path)
    		self.videoHead(path)
    		print self.topDuration
    		frameDiff = list(enumerate(iv.readframe())) #bmp -> [io][1]
    		self.totalFrame = frameDiff [-1][0]
    		#16 imgs
    		self.genImgs = []
    		curImg = 0
    		while True:
    			if curImg < 16:
    				self.genImgs.append(randint(1,self.totalFrame))
    				curImg = curImg + 1 
    			else:
    				break
    		
    		
    		try:
    			src = PIL.Image.open("src.png")
    			ciz = PIL.ImageDraw.draw(src)
    			ft = PIL.ImageFont.truetype("arial.ttf",32)
    			ciz.text((190,15),self.fileName,font=ft) #fileName
    			src.save("1.png")
    			
    			
    			#print frameDiff[5][0]
    			for i in self.genImgs:
    				imj = PIL.Image.open(StringIO.StringIO(frameDiff[i][1])) #base io -> [capsNo][1]
    				imj.save("%s.png"%i)
    		except NameError:
    			src = Image.open("src.png")
    			ciz = ImageDraw.draw(src)
    			ft = ImageFont.truetype("arial.ttf",32)
    			ciz.text((190,15),self.fileName,font=ft) #fileName
    			src.save("1.png")
    			
    			
    			#print frameDiff[5][0]
    			for i in self.genImgs:
    				imj = Image.open(StringIO.StringIO(frameDiff[i][1])) #base -> [io][1]
    				imj.save("%s.png"%i)
    My error,

    Code:
    Traceback (most recent call last):
      File "o.py", line 275, in (module)
        run = capsGen()
      File "o.py", line 42, in __init__
        self.videoGen() #for developers
      File "o.py", line 157, in videoGen
        ciz = ImageDraw.draw(src)
    NameError: global name 'ImageDraw' is not defined
    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.
    Last edited by fatihmert95; Apr 22 '14, 07:01 AM. Reason: invalid words. I did
  • dwblas
    Recognized Expert Contributor
    • May 2008
    • 626

    #2
    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.
    Code:
    import Image, ImageDraw
    
    image_name="chairs.png"
    im = Image.open(image_name)
    draw = ImageDraw.Draw(im)
    draw.line((0, 0) + im.size, fill=255)
    draw.line((0, im.size[1], im.size[0], 0), fill=128)
    
    im.show()
    Attached Files

    Comment

    • fatihmert95
      New Member
      • Apr 2014
      • 3

      #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 *

      Code:
      import Tkinter as tk
      And, my project file in some .open functions, I changed to these names.

      Thank for interest.
      Good works.

      Comment

      Working...