Error when calling the metaclass bases function() argument 1 must be code, not str

S

sector119

I use python 2.4.1 and PIL 1.1.5 and when I execute my program I got
error:

../code.py
Traceback (most recent call last):
File "./code.py", line 7, in ?
class DrawPlus(ImageDraw.Draw):
TypeError: Error when calling the metaclass bases
function() argument 1 must be code, not str


Why I got this error? It work good on python 2.3 + PIL 1.1.4



#!/usr/bin/env python
# -*- coding: iso8859-1 -*-

import random
import Image, ImageFont, ImageDraw

class DrawPlus(ImageDraw.Draw):
def __init__(self, im, mode=None):
ImageDraw.Draw.__init__(self, im, mode=None)

def text(self, xy, text, fill=None, font=None, angle=None,
anchor=None):
ink, fill = self._getink(fill)
if font is None:
font = self.getfont()
if ink is None:
ink = fill
if ink is not None:
bitmap = font.getmask(text)
if angle is not None:
bitmap = bitmap.rotate(angle, Image.BILINEAR)
self.draw.draw_bitmap(xy, bitmap, ink)

class ImageRandomCode:
def __init__(self, filename, size=None):
# plain text code
self.code = ''
# code image file name
self.filename = filename
# fonts
import glob
dir = 'fonts/*.ttf'
self.fonts = glob.glob(dir)
# chars tuple
#self.chars = chars or
('0','1','2','3','4','5','6','7','8','9','A','B','C','D','E','F','G','H','I','J','K','L','M','N','O','P','Q','R','S','T','U','V','W','X','Y','Z')
self.chars = ('0','1','2','3','4','5','6','7','8','9')
# image width
self.img_width = 100
# image height
self.img_height = 30
# image size = (image width, image height)
self.img_size = size or (self.img_width, self.img_height)
# image background color (r,g,b)
self.img_background_color = (255, 255, 255)
# image font color (r,g,b)
self.img_font_color = (0, 0, 0)
# code length
self.length = 5
# minimum font size
self.font_size_min = 13 # 12
# maximum font size
self.font_size_max = 18
# maximum noise lines to draw
self.lines_noise_max = 3
# maximum noise pixels to draw
self.pixels_noise_percent = 3 # 5
# maximum char angle
self.angle_max = 15 # 20

img = Image.new('RGB', self.img_size, self.img_background_color)
self.draw = DrawPlus(img)

self.draw_chars()
self.draw_noise()

del self.draw
img.save(self.filename)

def draw_noise(self):
self.draw_noise_pixels()
self.draw_noise_lines()

def get_code(self):
return self.code

def get_filename(self):
return self.filename

def draw_chars(self, chars=None, length=None):
chars = chars or self.chars
length = length or self.length
for i in range(length):
font_size = random.randint(self.font_size_min, self.font_size_max)
font_name = random.choice(self.fonts)
font = ImageFont.truetype(font_name, font_size)
angle = random.randint(-self.angle_max, +self.angle_max)
text = random.choice(chars)
size = self.draw.textsize(text, font=font)
x = i*(self.img_width-2-2)/length
y = random.randint(2, self.img_height-size[1]-2)
self.draw.text((x,y), text, font=font, angle=angle,
fill=self.img_font_color)
self.code += text

def draw_noise_pixels(self, percent=None):
percent = percent or self.pixels_noise_percent
N = int(self.img_width*self.img_height*percent/100)
for i in range(N):
x = random.randint(0, self.img_width)
y = random.randint(0, self.img_height)
self.draw.point((x,y), fill=self.img_font_color)
for i in range(N):
x = random.randint(0, self.img_width)
y = random.randint(0, self.img_height)
self.draw.point((x,y), fill=self.img_background_color)

def draw_noise_lines(self, max=None):
max = max or self.lines_noise_max
N = random.randint(0, max)
for i in range(N):
x1 = random.randint(0, self.img_width)
y1 = random.randint(0, self.img_height)
x2 = random.randint(0, self.img_width)
y2 = random.randint(0, self.img_height)
self.draw.line((x1,y1,x2,y2), fill=(0,0,0))

code = ImageRandomCode('test.png')

print code.get_code()
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,755
Messages
2,569,536
Members
45,020
Latest member
GenesisGai

Latest Threads

Top