Pregunta

I am new, Hello so community

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

from PIL import *

I solve this code line with try-except

try:
    from PIL import *
except AttributeError: #module has no attribute ImageN
    import Image
    import ImageDraw
    import ImageFont

My orginal code part

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,

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.

¿Fue útil?

Solución

You said to us "Tkinter" module. If you are import the Tkinter module this like

from Tkinter import *

Generaly, this like use programmers. And use PIL module, We must know every module class names because its conflict.

Tkinter in Image module, PIL in Image module. Both .open attribute almost the same.

Solve,

import Tkinter as tk

if you are using Pillow or not, I advice to you, use to this like

from PIL import Image, ImageDraw, ImageFont

And lastly, You should String names to reference, for example,

class tryAgain(object):
    def __init__(self):
        self.referenceName = "Hell" + "o"

    def run(self):
        img = Image.open(self.referenceName)
        #this like
Licenciado bajo: CC-BY-SA con atribución
No afiliado a StackOverflow
scroll top