Created a class for drawing text on the image.

from PIL import Image, ImageDraw, ImageFont class Blank(): def __init___(self): self.loadImage() self.txt = Image.new('RGBA', self.img.size, (255,255,255,0)) self.draw = ImageDraw.Draw(self.txt) def loadImage(self): try: self.img = Image.open('sts.png').convert("RGBA") except IOError: print("Unable to load image") sys.exit(1) def setSerialNumber(self, serial_number): (x, y) = (900, 50) message = serial_number font_serial = ImageFont.truetype('2057.ttf', size=43) self.draw.text((x, y), message, fill=(200, 0, 0, 255), font=font_serial) def saveImage(self): combined = Image.alpha_composite(self.img, self.txt) combined.save('sts_new.png') 

that's how I call him

  sts = Blank() if request.method == 'POST': sts.setSerialNumber(request.form.get("serial_number")) sts.saveImage() 

But an error is issued

 AttributeError: 'Blank' object has no attribute 'draw' 

Why he does not see this attribute, because it is set when creating a class?

    1 answer 1

    Class initialization is not performed because there are too many underscores in the name in the __init___(self) method, namely, there are three backs. That's right:

     class Blank: def __init__(self):