473,320 Members | 1,839 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,320 software developers and data experts.

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

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()

Jul 19 '05 #1
0 3714

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

4
by: Paul Morrow | last post by:
One of the beautiful things about Python is its clear, minimal syntax. So we must resist adding new syntax to the language, especially where there is a reasonable alternative. I believe that...
1
by: Antoine Pitrou | last post by:
Hi, I've been looking at writing parameterized metaclasses and here are the two solutions I've come to: (my goal was to build a way to automatically add a hash function that would take into...
9
by: Christian Eder | last post by:
Hi, I think I have discovered a problem in context of metaclasses and multiple inheritance in python 2.4, which I could finally reduce to a simple example: Look at following code: class...
5
by: alainpoint | last post by:
Hi, I have what in my eyes seems a challenging problem. Thanks to Peter Otten, i got the following code to work. It is a sort of named tuple. from operator import itemgetter def...
3
by: Daniel Nogradi | last post by:
I used to have the following code to collect all (old style) class names defined in the current module to a list called reg: def meta( reg ): def _meta( name, bases, dictionary ): reg.append(...
0
by: Clarence | last post by:
I'm having problems with JPype and am trying to change the way it creates Python classes as proxies for Java classes and interfaces. I'm trying to get around "inconsistent mro" problems, but in...
10
by: =?iso-8859-1?B?QW5kcuk=?= | last post by:
In my application, I make use of the Borg idiom, invented by Alex Martelli. class Borg(object): '''Borg Idiom, from the Python Cookbook, 2nd Edition, p:273 Derive a class form this; all...
4
by: thomas.karolski | last post by:
Hi, I would like to create a Decorator metaclass, which automatically turns a class which inherits from the "Decorator" type into a decorator. A decorator in this case, is simply a class which...
0
by: Miles | last post by:
On Mon, Sep 15, 2008 at 6:06 AM, Harish K Vishwanath <harish.shastry@gmail.comwrote: "built-in type" generally means "implemented in C", also sometimes called "extension type". Both the...
0
by: DolphinDB | last post by:
The formulas of 101 quantitative trading alphas used by WorldQuant were presented in the paper 101 Formulaic Alphas. However, some formulas are complex, leading to challenges in calculation. Take...
0
by: DolphinDB | last post by:
Tired of spending countless mintues downsampling your data? Look no further! In this article, you’ll learn how to efficiently downsample 6.48 billion high-frequency records to 61 million...
0
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: Vimpel783 | last post by:
Hello! Guys, I found this code on the Internet, but I need to modify it a little. It works well, the problem is this: Data is sent from only one cell, in this case B5, but it is necessary that data...
1
by: PapaRatzi | last post by:
Hello, I am teaching myself MS Access forms design and Visual Basic. I've created a table to capture a list of Top 30 singles and forms to capture new entries. The final step is a form (unbound)...
0
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
0
by: Defcon1945 | last post by:
I'm trying to learn Python using Pycharm but import shutil doesn't work
1
by: Shællîpôpï 09 | last post by:
If u are using a keypad phone, how do u turn on JavaScript, to access features like WhatsApp, Facebook, Instagram....
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...

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.