Пример #1
0
 def setBoudaries(self):
     with self.canvas:
         setColor("boundary")
         Rectangle(pos=(0,0), size=(PIX_W,LY))
         Rectangle(pos=(0,0), size=(LX,PIX_H))
         Rectangle(pos=(LX-PIX_W,0), size=(PIX_W,LY))
         Rectangle(pos=(0,LY-PIX_H), size=(LX,PIX_H))
Пример #2
0
def putContent2Image(content,
                     background_image_path,
                     font_path,
                     add_rectangle=0,
                     resize_rate=2):
    image = Image.open(background_image_path)

    image = image.resize((280, 32), Image.ANTIALIAS)

    # 确定字体的大小

    font_size = min(280 // len(content), 28)

    # 获得文字起始点
    left_center_point = (random.randint(
        0, max(0, image.size[0] - font_size * len(content))),
                         (random.randint(0, 32 - font_size) + font_size) // 2)

    # 计算文字颜色,我这里只需要灰白的,直接RGB相等然后随机就行了
    color = utils.setColor(image)

    for character in content:
        image, points = putOneCharacter2Image(character, image, font_path,
                                              font_size, left_center_point,
                                              color)
        left_center_point = (max(points[1][0],
                                 points[2][0]), left_center_point[1])

    return image
 def putContent2Image(self,
                      mulcontents,
                      background_image_path,
                      font_path,
                      add_rectangle=0,
                      resize_rate=2):
     try:
         image = Image.open(background_image_path)
         mulcontents_points = []
         font_size_max = image.size[0] / self.args[
             'characters_length_tuple'][1]
         # 根据image的长度来决定font_size_max(这里是当image长度值太小时,需要将image图片放大)
         # 文字最小是32, 这里确保font_size_max要大于font_size_min
         while font_size_max < self.args['font_size_min']:
             resize_rate = resize_rate * 2
             image = image.resize(
                 (image.size[0] * resize_rate, image.size[1] * resize_rate))
             font_size_max = image.size[0] / self.args[
                 'characters_length_tuple'][1]
         font_size = random.randint(int(self.args['font_size_min']),
                                    int(font_size_max))
         # 在图片可容纳文字的范围,随机选择一个坐标
         # 第一个坐标点:图片最左边~图片最右-文字最长宽度
         # 第二个坐标点:
         left_center_point = (random.randint(
             0,
             image.size[0] - font_size * max([len(i)
                                              for i in mulcontents])),
                              random.randint(
                                  font_size * len(mulcontents),
                                  image.size[1] -
                                  int(font_size * len(mulcontents) / 2)))
         color = utils.setColor(image)
         # 遍历每一行(一般是两行)
         for content in mulcontents:
             content_points = []
             self.txt_center_line = 0
             for character in content:
                 image, points = self.putOneCharacter2Image(
                     character, image, font_path, font_size,
                     left_center_point, color)
                 content_points.append(points)
                 left_center_point = (max(points[1][0], points[2][0]),
                                      left_center_point[1])
             left_center_point = utils.getNewLeftCenterPointByContentPoints(
                 content_points)
             mulcontents_points.append(content_points)
         if add_rectangle == 1:
             image = utils.drawMulContentsRectangle(image,
                                                    mulcontents_points)
         image_out = image
     except AssertionError:
         # print('MadeError, retry.')
         image_out, mulcontents_points = self.putContent2Image(
             mulcontents, background_image_path, font_path, resize_rate)
     return image_out, mulcontents_points
Пример #4
0
 def addApple(self):
     """
     Placer la pomme sur une position autre que l'une
     des positions occupees par le snake
     """
     body = self.snakeObj.body
     x, y = body[0].pos[0], body[0].pos[1]
     move_apple = True
     while move_apple:
         move_apple = False
         x = self.p_sw[0]+randint(1,NX-1)*SNAKE_W
         y = self.p_sw[1]+randint(1,NY-1)*SNAKE_H
         for part in body:
             if (x,y) == part.pos :
                 move_apple = True
                 break
     with self.canvas:
         setColor("apple")
         self.apple = Ellipse(pos=(x, y), size=(SNAKE_W,SNAKE_H))    
Пример #5
0
 def putContent2Image(self,
                      mulcontents,
                      background_image_path,
                      font_path,
                      add_rectangle=0,
                      resize_rate=2):
     try:
         image = Image.open(background_image_path)
         mulcontents_points = []
         # 通过背景图片的大小,计算每个字符最大的宽度。
         # 如果宽度小于最小的字体大小,则去扩展图片(我这里的图片背景都很大,不需要做什么)
         font_size_max = image.size[0] // self.args[
             'characters_length_tuple'][1]
         while font_size_max < self.args['font_size_min']:
             resize_rate = resize_rate * 2
             image = image.resize(
                 (image.size[0] * resize_rate, image.size[1] * resize_rate))
             font_size_max = image.size[0] // self.args[
                 'characters_length_tuple'][1]
         # 确定字体的大小
         font_size = random.randint(self.args['font_size_min'],
                                    font_size_max)
         # 获得文字起始点
         left_center_point = (random.randint(
             0,
             image.size[0] - font_size * max([len(i)
                                              for i in mulcontents])),
                              random.randint(
                                  font_size * len(mulcontents),
                                  image.size[1] -
                                  font_size * len(mulcontents) / 2))
         # 计算文字颜色,我这里只需要灰白的,直接RGB相等然后随机就行了
         color = utils.setColor(image)
         for content in mulcontents:
             content_points = []
             self.txt_center_line = 0
             for character in content:
                 image, points = self.putOneCharacter2Image(
                     character, image, font_path, font_size,
                     left_center_point, color)
                 content_points.append(points)
                 left_center_point = (max(points[1][0], points[2][0]),
                                      left_center_point[1])
             left_center_point = utils.getNewLeftCenterPointByContentPoints(
                 content_points)
             mulcontents_points.append(content_points)
         if add_rectangle == 1:
             image = utils.drawMulContentsRectangle(image,
                                                    mulcontents_points)
         image_out = image
     except AssertionError:
         # print('MadeError, retry.')
         image_out, mulcontents_points = self.putContent2Image(
             mulcontents, background_image_path, font_path, resize_rate)
     return image_out, mulcontents_points
 def putContent2Image(self,
                      mulcontents,
                      background_image_path,
                      font_path,
                      add_rectangle=0,
                      resize_rate=2):
     try:
         image = Image.open(background_image_path)
         mulcontents_points = []
         font_size_max = image.size[0] / self.args[
             'characters_length_tuple'][1]
         while font_size_max < self.args['font_size_min']:
             resize_rate = resize_rate * 2
             image = image.resize(
                 (image.size[0] * resize_rate, image.size[1] * resize_rate))
             font_size_max = image.size[0] / self.args[
                 'characters_length_tuple'][1]
         font_size = random.randint(self.args['font_size_min'],
                                    font_size_max)
         left_center_point = (random.randint(
             0,
             image.size[0] - font_size * max([len(i)
                                              for i in mulcontents])),
                              random.randint(
                                  font_size * len(mulcontents),
                                  image.size[1] -
                                  font_size * len(mulcontents) / 2))
         color = utils.setColor(image)
         for content in mulcontents:
             content_points = []
             self.txt_center_line = 0
             for character in content:
                 image, points = self.putOneCharacter2Image(
                     character, image, font_path, font_size,
                     left_center_point, color)
                 content_points.append(points)
                 left_center_point = (max(points[1][0], points[2][0]),
                                      left_center_point[1])
             left_center_point = utils.getNewLeftCenterPointByContentPoints(
                 content_points)
             mulcontents_points.append(content_points)
         if add_rectangle == 1:
             image = utils.drawMulContentsRectangle(image,
                                                    mulcontents_points)
         image_out = image
     except AssertionError:
         # print('MadeError, retry.')
         image_out, mulcontents_points = self.putContent2Image(
             mulcontents, background_image_path, font_path, resize_rate)
     return image_out, mulcontents_points
Пример #7
0
 def addPart(self):
     pos = self.snakeObj.last_positions[-1]
     with self.canvas:
         setColor("snake_body")
         snake_part = Rectangle(pos=pos, size=(SNAKE_W,SNAKE_H))
     self.snakeObj.body.append(snake_part)
Пример #8
0
 def addSnake(self, head_pos):
     with self.canvas:
         setColor("snake_head")
         snake_head = Rectangle(pos=head_pos, size=(SNAKE_W,SNAKE_H))    
     self.snakeObj.body.append(snake_head)