Skip to content

Commit

Permalink
fix text not align center issue
Browse files Browse the repository at this point in the history
  • Loading branch information
Jax-Lifu committed Jun 17, 2022
1 parent b527256 commit e647af5
Show file tree
Hide file tree
Showing 2 changed files with 16 additions and 9 deletions.
18 changes: 11 additions & 7 deletions main.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
from PyQt5 import QtGui
from PyQt5.QtCore import Qt, QStandardPaths
from PyQt5.QtGui import QPixmap, QFont, QFontInfo, QFontDatabase
from PyQt5.QtWidgets import QApplication, QMainWindow, QFileDialog, QFontDialog
from PyQt5.QtWidgets import QApplication, QMainWindow, QFileDialog, QFontDialog, QMessageBox

from bmp2hex import bmp2hex
from text2image import ImageGenerate
Expand Down Expand Up @@ -97,12 +97,16 @@ def text_to_image(self):

logging.debug(
f'message {self.message} fontsize {self.font_size} fontFamily {self.font_family}')

image_name = self.imageGenerate.generate(
self.message, self.font_family, self.font_size)

self.display_dist_image(image_name)
self.image_to_lcd(image_name)
try:
image_name = self.imageGenerate.generate(
self.message, self.font_family, self.font_size)

self.display_dist_image(image_name)
self.image_to_lcd(image_name)
except:
QMessageBox(QMessageBox.NoIcon, 'Open File failed',
'Unable to open the file').exec()
pass

def image_to_lcd(self, image_name):
result = bmp2hex(image_name, 8, 0,
Expand Down
7 changes: 5 additions & 2 deletions text2image.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
import langid
import numpy as np
from PIL import Image, ImageDraw, ImageFont
import time

from bmp2hex import bmp2hex

Expand Down Expand Up @@ -71,19 +72,21 @@ def generate(self, text, font_family=None, font_size=None):
print(f'{len(text)} {font_family} {font_size}')

font = ImageFont.truetype(font_family, font_size)
image_name = f'res/image_{text}.bmp'
# image_name = f'res/image_{text}.bmp'
image_name = f'res/image_{text}_{int(time.time())}.bmp'
image = Image.new('1', (self.width, self.height), (255))
draw = ImageDraw.Draw(image)
# draw.text((0, 8), text, font=font, fill=(0))
w = 0
h = 0
x = 0
top = 0
length = len(text)
for index, item in enumerate(text):
if index == 0:
w, h = font.getsize(item)
x = (self.width - w)/2
top = self.height * 0.25 // len(text)
top = (self.height - length*h - length)/2

draw.text((x, h*index + top), item, font=font, fill=(0))
# Wrap the `text` string into a list of `CHAR_LIMIT`-character strings
Expand Down

0 comments on commit e647af5

Please sign in to comment.