qq_41558758
HynemanKan
采纳率0%
2018-11-10 09:10

python3.x PIL模块 ImageDraw 什么都没画出来

初学PIL,遇到了一些问题,为什么ImageDraw什么都没画出来

from PIL import Image, ImageDraw, ImageFont


class BackGround:#根对象
    def __init__(self, draw,im):
        self.l_x = 0
        self.l_y = 0
        self.im = draw
        self.s_x, self.s_y = im.size

    def get_postion(self):
        return self.l_x, self.l_y

    def get_size(self):
        return self.s_x, self.s_y


class DrawElement:#父类
    def __init__(self, parent, l_x, l_y):
        self.im = parent.im
        self.parent = parent
        self.l_y = l_y
        self.l_x = l_x

    def get_postion(self):
        l_x = self.l_x + self.parent.get_postion()[0]
        l_y = self.l_y + self.parent.get_postion()[1]
        return l_x, l_y

    def draw(self):
        pass

    def get_size(self):
        pass


class Text(DrawElement):#单行文字

    def __init__(self, parent, l_x, l_y, text, myfont):
        self.im = parent.im
        self.text = text
        self.parent = parent
        self.l_y = l_y
        self.l_x = l_x
        self.front = myfont

    def get_size(self):
        size_x, size_y = self.im.textsize(self.text, self.front)
        return size_x, size_y

    def draw(self):
        self.im.text(self.get_postion(), self.text, font=self.front, fill=(255, 0, 0))


class MultipleText(DrawElement):#多行文字
    def __init__(self, parent, l_x, l_y, text, myfont, lineSpacing):
        self.parent = parent
        self.im = parent.im
        self.l_x = l_x
        self.l_y = l_y
        self.texts = text.split("\n")
        self.font = myfont
        self.lineSpacing = lineSpacing
        self.childrens = []
        l_t_x = 0
        l_t_y = 0
        s_t_x = 0
        for i in range(len(self.texts)):
            tl = Text(self, l_t_x, l_t_y, self.texts[i], self.font)
            s_x, s_y = tl.get_size()
            if s_x > s_t_x:
                s_t_x = s_x
            l_t_y += (s_y + self.lineSpacing)
            self.childrens.append(tl)
        for item in self.childrens:
            item.l_x = int((s_t_x - item.get_size()[1]) / 2)
        self.s_x = s_t_x
        self.s_y = l_t_x - self.lineSpacing

    def get_size(self):
        return self.s_x, self.s_y

    def draw(self):
        for item in self.childrens:
            item.draw()


class Rectangle(DrawElement):
    def __init__(self, parent, l_x, l_y, s_x, s_y):
        self.parent = parent
        self.im = parent.im
        self.l_x = l_x
        self.l_y = l_y
        self.s_x = s_x
        self.s_y = s_y

    def get_size(self):
        return self.s_x, self.s_y

    def draw(self):
        self.im.rectangle((self.get_postion(), (self.l_x, self.l_y)))


class TextBox(DrawElement):
    def __init__(self, parent, l_x, l_y, text, frameWidth, myfont,lineSpacing):
        self.im = parent.im
        self.parent = parent
        self.font = myfont
        self.l_x = l_x
        self.l_y = l_y
        self.text = text
        self.frameWidth = frameWidth
        self.mtext = MultipleText(self, frameWidth, frameWidth, text, self.font,lineSpacing)
        x, y = self.mtext.get_size()
        self.s_x = x + self.frameWidth * 2
        self.s_y = y + self.frameWidth * 2
        self.rect = Rectangle(self, 0, 0, self.s_x, self.s_y)

    def draw(self):
        self.mtext.draw()
        self.rect.draw()

    def get_size(self):
        return self.s_x, self.s_y

    def get_connect_point(self, side):
        l_g_x, l_g_y = self.get_postion()
        s_x, s_y = self.get_size()
        if side == "LEFT":
            return l_g_x, l_g_x + s_y / 2
        elif side == "RIGHT":
            return l_g_x + s_x, l_g_y + s_y / 2
        elif side == "TOP":
            return l_g_x + s_x / 2, l_g_y
        else:
            raise Exception("wrong parameter, can only accept 'LEFT','RIGHT'")


class Line(DrawElement):
    def __init__(self, parent, a_point, b_point):
        self.parent = parent
        self.im = parent.im
        if a_point[0] < b_point[0]:
            self.l_x = a_point[0]
        else:
            self.l_x = b_point[0]
        if a_point[1] < b_point[1]:
            self.l_y = a_point[1]
        else:
            self.l_y = b_point[1]
        self.s_x = abs(a_point[0] - b_point[0])
        self.s_y = abs(a_point[1] - b_point[1])
        self.lines = [[[(a_point[0] + b_point[0]) / 2, a_point[1]], a_point]
                      [[(a_point[0] + b_point[0]) / 2, a_point[1]], [(a_point[0] + b_point[0]) / 2, b_point[1]]],
                      [[(a_point[0] + b_point[0]) / 2, b_point[1]], b_point]]

    def draw(self):
        for line in self.lines:
            self.im.line(line)


myfont = ImageFont.truetype("temp.ttf",20)
newim = Image.new("RGB", (1280, 1280), "#00ffff")
draw = ImageDraw.ImageDraw(newim)
bg = BackGround(draw, newim)
t = Text(bg, 10, 10, "test", myfont)
newim.show()
newim.save("out.png")

  • 点赞
  • 写回答
  • 关注问题
  • 收藏
  • 复制链接分享
  • 邀请回答

1条回答

相关推荐