Python:TypeError:“模块”对象无法从子类中调用

时间:2018-10-23 21:23:06

标签: python

我有一个名为ImageWrapper的类和一个名为DDImage的子类,当我调用在父类ImageWrapper中实现的方法时,出现了错误:

  Traceback (most recent call last):
  File "/home/josecarlos/Workspace/python/process/aux.py", line 11, in <module>
    main()
  File "/home/josecarlos/Workspace/python/process/aux.py", line 8, in main
    i.set_text_title((20, i.get_height()-550), "Doble doble #1", (9,30,54))
  File "/home/josecarlos/Workspace/python/process/com/images/dd_image.py", line 19, in set_text_title
    d = ImageDraw(self.get_image())
TypeError: 'module' object is not callable

image.py

from constants import URL, FORMAT
from PIL import Image, ImageDraw
import abc


class ImageWrapper(metaclass=abc.ABCMeta):

    def __init__(self, id_player):
        self.set_image(Image.open(URL.ROUTE_TO_IMAGES + str(id_player) + ".jpg"))
        size = self.calculate_size(self.get_image().size[0], self.get_image().size[1])
        self.set_image(self.get_image().resize(size))
        self.get_image().paste(Image.open(URL.ROUTE_TO_FOOT), (0, self.get_height()-100))

    def calculate_size(self, width, height):
        '''Calculate the new size of the image when width = 1200px'''
        self.set_width(FORMAT.IMAGE_WIDTH)
        p = (FORMAT.IMAGE_WIDTH*100)/width
        h = (height*p)/100
        self.set_height(h)
        return (self.get_width(), self.get_height())

    @abc.abstractmethod
    def set_title_font(self):
        raise NotImplementedError("Message", "set_title_font", None,
                          "El usuario no ha implementado este método")

    def set_title_line(self, x1, y1, x2, y2, color):
        d = ImageDraw.Draw(self.get_image())
        d.line((x1, y1, x2, y2), fill=color, width=10)

    def set_image(self, img):
        self.img = img

    def get_image(self):
        return self.img

    def set_height(self, height):
        self.height = round(height)

    def get_height(self):
        return self.height

    def set_width(self, width):
        self.width = width

    def get_width(self):
        return self.width

    def get_size(self):
        return (self.get_width(), self.get_height())

dd_image.py

from com.images.image import ImageWrapper
from PIL import ImageFont, ImageDraw
from constants import FONTS


class DDImage(ImageWrapper):

    def __init__(self, id_player):
         super().__init__(id_player)

    def set_title_font(self):
        self.fnt = ImageFont.truetype(FONTS.DD_TITLE_FONT, FONTS.DD_TITLE_FONT_SIZE)

    def get_title_font(self):
        return self.fnt

    def set_text_title(self, position, txt, color):
        print("tamaño1: {0}".format(self.get_image()))
        d = ImageDraw(self.get_image())
        d.txt(position, txt, self.get_title_font(), fill=color)

在我的子类DDImage中,我在调用self.get_image()时遇到了错误 d = ImageDraw(self.get_image())。以前,我在父类中使用self.get_image()方法,它可以正常工作。如果我在子类中调用相同的方法,在以下指令中也可以正常工作:print(“tamaño1:{0}”。format(self.get_image()))我得到了以下消息:

tamaño1: <PIL.Image.Image image mode=RGB size=1200x1814 at 0x7F3E59CD2748>

但是当我在ImageDraw()中调用它时,出现了错误!!!

我的目录树是:

com / images / image.py com / images / dd_image.py

我正在使用python 3.6

我在做什么错了?

0 个答案:

没有答案