Newer
Older
stockTray / stock_new / python_ver / Txt2Png.py
bdapp on 27 Feb 2019 1 KB python version
import os
from PIL import Image, ImageDraw, ImageFont
# import pygame
# from pygame.locals import *

class Price2Png:
    def __init__(self, sort, price, pre_close):
        self.sort = sort
        self.price = float(price)
        self.pre_close = float(pre_close)
        result = self.price - self.pre_close
        # price = u'22.30'
        l = len(price)
        y = 0
        if l == 3:
            y = 3
        elif l == 4:
            y = 8
        elif l == 5:
            y = 13
        elif l == 6:
            y = 18
        else:
            y = 0

        # pygame.init()
        # font = pygame.font.SysFont('Ubuntu', 20)
        # ftext = font.render(text, True, (200, 200, 130), (72, 72, 72))
        # pygame.image.save(ftext, './a.png')

        im = Image.new('RGB', (l*15, l*15), (83, 81, 73))
        dr = ImageDraw.Draw(im)
        font = ImageFont.truetype(os.path.join("fonts", "Ubuntu-C"), 40)

        fill = ''
        if result > 0:
            fill = '#CF9A91'
        elif result < 0:
            fill = '#82C79F'
        else:
            fill = '#EEEEEE'
        # r = dr.text((0, y), text, font=font, fill=)
        dr.text((0, y), str(self.price), font=font, fill=fill)
        # r = dr.text((0, y), text, font=font, fill='#82C79F')
        # im.show()
        im.save(str(sort) + '.png')

# if __name__ == "__main__":
#     Price2Png(9, '12.3')