Add fonts and use one that makes text pretty clear
This commit is contained in:
parent
b9284d9116
commit
2aec642b89
2 changed files with 40 additions and 24 deletions
BIN
fonts/TsunagiGothic.ttf
Normal file
BIN
fonts/TsunagiGothic.ttf
Normal file
Binary file not shown.
|
|
@ -59,13 +59,18 @@ def handleImages(filepath, estimate):
|
||||||
|
|
||||||
translatedData = openFiles(filepath)
|
translatedData = openFiles(filepath)
|
||||||
|
|
||||||
# Convert Strings to Images
|
# Convert Strings to Images
|
||||||
for i in range(len(translatedData[0][0])):
|
if not ESTIMATE:
|
||||||
translatedList = translatedData[0][0]
|
for i in range(len(translatedData[0][0])):
|
||||||
originalList = translatedData[0][1]
|
try:
|
||||||
dimensionsList = translatedData[0][2]
|
translatedList = translatedData[0][0]
|
||||||
image = stringToImage(translatedList[i], dimensionsList[i][0], dimensionsList[i][1])
|
originalList = translatedData[0][1]
|
||||||
image.save(f'translated/{originalList[i]}.png', quality=100)
|
dimensionsList = translatedData[0][2]
|
||||||
|
image = stringToImage(translatedList[i], dimensionsList[i][0], dimensionsList[i][1])
|
||||||
|
image.save(rf'translated/{originalList[i]}.png', quality=100)
|
||||||
|
except Exception as e:
|
||||||
|
PBAR.write(f'{originalList[i]}: {str(e)}')
|
||||||
|
#Ignore Error
|
||||||
|
|
||||||
# Print File
|
# Print File
|
||||||
end = time.time()
|
end = time.time()
|
||||||
|
|
@ -125,7 +130,7 @@ def getFontSize(text, image_width, image_height, font_path):
|
||||||
font = ImageFont.truetype(font_path, font_size)
|
font = ImageFont.truetype(font_path, font_size)
|
||||||
text_bbox = ImageDraw.Draw(Image.new('RGB', (1, 1))).textbbox((0, 0), text, font=font)
|
text_bbox = ImageDraw.Draw(Image.new('RGB', (1, 1))).textbbox((0, 0), text, font=font)
|
||||||
text_width = text_bbox[2] - text_bbox[0]
|
text_width = text_bbox[2] - text_bbox[0]
|
||||||
text_height = text_bbox[3] - text_bbox[1]
|
text_height = text_bbox[3] - text_bbox[1] + 5
|
||||||
|
|
||||||
if text_width <= image_width and text_height <= image_height:
|
if text_width <= image_width and text_height <= image_height:
|
||||||
return font_size
|
return font_size
|
||||||
|
|
@ -133,33 +138,38 @@ def getFontSize(text, image_width, image_height, font_path):
|
||||||
|
|
||||||
return font_size
|
return font_size
|
||||||
|
|
||||||
def stringToImage(text, width, height, font_path='arial.ttf'):
|
def stringToImage(text, width, height, font_path='fonts/TsunagiGothic.ttf', scale_factor=4):
|
||||||
# Find the appropriate font size
|
# Increase the resolution
|
||||||
font_size = getFontSize(text, width, height, font_path)
|
scaled_width = int(width * scale_factor)
|
||||||
|
scaled_height = int(height * scale_factor)
|
||||||
|
|
||||||
|
# Find the appropriate font size for the scaled up image
|
||||||
|
font_size = getFontSize(text, scaled_width, scaled_height, font_path)
|
||||||
if font_size == 0:
|
if font_size == 0:
|
||||||
raise ValueError("Text is too long to fit in the supplied dimensions.")
|
raise ValueError("Text is too long to fit in the supplied dimensions.")
|
||||||
|
|
||||||
# Create a new image with the specified width and height and a transparent background
|
# Create a new image with the scaled width and height and a transparent background
|
||||||
image = Image.new('RGBA', (width, height), (255, 255, 255, 0))
|
image = Image.new('RGBA', (scaled_width, scaled_height), (255, 255, 255, 0))
|
||||||
|
|
||||||
# Create a drawing context
|
# Create a drawing context
|
||||||
draw = ImageDraw.Draw(image)
|
draw = ImageDraw.Draw(image)
|
||||||
|
|
||||||
# Load the appropriate font
|
# Load the appropriate font
|
||||||
font = ImageFont.truetype(font_path, font_size)
|
font = ImageFont.truetype(font_path, font_size)
|
||||||
|
|
||||||
# Calculate the size of the text to center it
|
# Calculate the size of the text to center it
|
||||||
text_bbox = draw.textbbox((0, 0), text, font=font)
|
text_bbox = draw.textbbox((0, 0), text, font=font)
|
||||||
text_width = text_bbox[2] - text_bbox[0]
|
text_width = text_bbox[2] - text_bbox[0]
|
||||||
text_height = text_bbox[3] - text_bbox[1]
|
text_height = text_bbox[3] - text_bbox[1]
|
||||||
|
x = (scaled_width - text_width) // 2
|
||||||
|
y = (scaled_height - text_height) // 2
|
||||||
|
|
||||||
x = (width - text_width) // 2
|
|
||||||
y = (height - text_height) // 2
|
|
||||||
|
|
||||||
# Draw the text on the image
|
# Draw the text on the image
|
||||||
draw.text((x, y), text, font=font, fill=(255, 255, 255, 255))
|
draw.text((x, y), text, font=font, fill=(255, 255, 255, 255))
|
||||||
|
|
||||||
|
# Resize back to the original dimensions to get a clearer text rendering
|
||||||
|
image = image.resize((width, height), Image.LANCZOS,)
|
||||||
|
|
||||||
return image
|
return image
|
||||||
|
|
||||||
def getImageDimensions(file_path):
|
def getImageDimensions(file_path):
|
||||||
|
|
@ -173,14 +183,20 @@ def getImageDimensions(file_path):
|
||||||
|
|
||||||
def processImagesDir(directory_path, imageList):
|
def processImagesDir(directory_path, imageList):
|
||||||
for file_name in os.listdir(directory_path):
|
for file_name in os.listdir(directory_path):
|
||||||
if '.png' in file_name:
|
# .png and Japanese
|
||||||
|
if '.png' in file_name and re.search(r'[一-龠ぁ-ゔァ-ヴー]+', file_name):
|
||||||
file_path = os.path.join(directory_path, file_name)
|
file_path = os.path.join(directory_path, file_name)
|
||||||
if os.path.isfile(file_path):
|
if os.path.isfile(file_path):
|
||||||
# Check if the file is an image
|
# Check if the file is an image
|
||||||
try:
|
try:
|
||||||
width, height = getImageDimensions(file_path)
|
width, height = getImageDimensions(file_path)
|
||||||
if width is not None and height is not None:
|
if width is not None and height is not None:
|
||||||
imageList[0].append(file_name.replace('.png', ' '))
|
placeholders = {
|
||||||
|
'.png': '',
|
||||||
|
}
|
||||||
|
for target, replacement in placeholders.items():
|
||||||
|
file_name = file_name.replace(target, replacement)
|
||||||
|
imageList[0].append(file_name)
|
||||||
imageList[1].append([width, height])
|
imageList[1].append([width, height])
|
||||||
except Exception as e:
|
except Exception as e:
|
||||||
print(f"Error processing {file_name}: {e}")
|
print(f"Error processing {file_name}: {e}")
|
||||||
|
|
@ -192,7 +208,7 @@ def translateImages(imageList):
|
||||||
totalTokens = [0,0]
|
totalTokens = [0,0]
|
||||||
with tqdm(bar_format=BAR_FORMAT, position=POSITION, leave=LEAVE, desc='Images', total=len(imageList[0])) as PBAR:
|
with tqdm(bar_format=BAR_FORMAT, position=POSITION, leave=LEAVE, desc='Images', total=len(imageList[0])) as PBAR:
|
||||||
# Translate GPT
|
# Translate GPT
|
||||||
response = translateGPT(imageList[0], 'Keep the Translation brief', True)
|
response = translateGPT(imageList[0], 'Keep the Translation as brief as possible', True)
|
||||||
translatedList = response[0]
|
translatedList = response[0]
|
||||||
totalTokens[0] += response[1][0]
|
totalTokens[0] += response[1][0]
|
||||||
totalTokens[1] += response[1][1]
|
totalTokens[1] += response[1][1]
|
||||||
|
|
|
||||||
Loading…
Reference in a new issue