From 1da4924c7ca7d22c3bd2e055ba2952dbb8b7fbc8 Mon Sep 17 00:00:00 2001 From: Jax Date: Fri, 17 Jun 2022 12:50:23 +0800 Subject: [PATCH] fix text not align center issue --- main.py | 18 +++++++++++------- text2image.py | 6 ++++-- 2 files changed, 15 insertions(+), 9 deletions(-) diff --git a/main.py b/main.py index c0d234a..bb66cd0 100644 --- a/main.py +++ b/main.py @@ -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 @@ -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, diff --git a/text2image.py b/text2image.py index 9364f77..0847c50 100644 --- a/text2image.py +++ b/text2image.py @@ -6,6 +6,7 @@ import langid import numpy as np from PIL import Image, ImageDraw, ImageFont +import time from bmp2hex import bmp2hex @@ -71,7 +72,8 @@ 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)) @@ -83,7 +85,7 @@ def generate(self, text, font_family=None, font_size=None): if index == 0: w, h = font.getsize(item) x = (self.width - w)/2 - top = self.height * 0.25 // len(text) + top = (self.height - len(text)*h)/2 draw.text((x, h*index + top), item, font=font, fill=(0)) # Wrap the `text` string into a list of `CHAR_LIMIT`-character strings