แบบอักษรและยูนิโค้ด - PIL ใน python

ฉันต้องการใช้แบบอักษรจาก LaTeX หรือแบบอักษรที่คล้ายกัน เหตุใดแบบอักษรนี้จึงใช้งาน Unicode ไม่ได้ ฟอนต์บางตัวเหมาะสมกับ Unicode และบางตัวไม่เหมาะกับ Unicode หรือไม่ ฉันควรใช้แบบอักษรใด ขอบคุณมาก

from PIL import Image, ImageDraw, ImageFont, ImageFilter

font_size=36
width=500
height=100
back_ground_color=(255,255,255)
font_size=36
font_color=(0,0,0)
unicode_text = u"\u03B2 Hello"

im  =  Image.new ( "RGB", (width,height), back_ground_color )
draw  =  ImageDraw.Draw ( im )
unicode_font = ImageFont.truetype("DejaVuSans.ttf", 40)
font = ImageFont.truetype('lmroman7-regular.otf', 40)

draw.text ( (10,10), unicode_text, font=unicode_font, fill=font_color )
draw.text ( (20,40), unicode_text, font=font, fill=font_color )

im.save("text.jpg")

ฉันมีไฟล์ที่มีแบบอักษรอยู่ในไดเร็กทอรีเดียวกันกับโค้ดหลาม ฉันกำลังมองหาแบบอักษร LM Roman สำหรับดาวน์โหลดที่สามารถแสดงเบต้าได้


person Elena Greg    schedule 06.10.2020    source แหล่งที่มา
comment
ตามฟอนต์จาก LaTeX คุณหมายถึง LM Roman หรือเปล่า? ตามนามสกุลไฟล์ (.otf) เป็นฟอนต์ Open Type ไม่ใช่ True Type (.ttf) เนื่องจาก ImageFont มีเมธอด truetype แต่ไม่มีเมธอด opentype จึงเป็นไปได้ว่า PIL จะไม่รองรับวิธีหลัง   -  person lenz    schedule 06.10.2020
comment
ใช่ ฉันหมายถึง LM Roman ฉันดาวน์โหลดแบบอักษรนี้ fontsquirrel.com/fonts/latin-modern-roman ซึ่งมีนามสกุล .ttf ผลลัพธ์ที่ได้คือสี่เหลี่ยมแทนที่จะเป็นเบต้า   -  person Elena Greg    schedule 06.10.2020
comment
หากคุณเรียกใช้สคริปต์ในคำตอบนี้ คุณจะพบแบบอักษรทั้งหมดที่สามารถแสดงเบต้านั้นได้... stackoverflow.com/a/60057237 /2836621   -  person Mark Setchell    schedule 06.10.2020
comment
ขอบคุณ ฉันควรเขียนอะไรถึงเทอร์มินัล? ฉันลอง python3 font_find.py u03B2 และ python3 font_find.py \u03B2   -  person Elena Greg    schedule 06.10.2020
comment
ไม่ ใช้ font_find β   -  person Mark Setchell    schedule 07.10.2020


คำตอบ (2)


CMU-serif เป็นแบบอักษรที่เหมาะสมซึ่งคล้ายกับ LM Roman ที่มี มีตัวอักษรกรีก

person Elena Greg    schedule 06.10.2020

เกิดข้อผิดพลาดอะไร? ฉันรันโค้ดของคุณ มันทำให้ฉันมีข้อผิดพลาด OSError: can not open resource หากเป็นกรณีนี้ คุณควรระบุที่อยู่แบบอักษรแทนชื่อแบบอักษร PIL สามารถรองรับ .oft แบบอักษรได้เช่นกัน แต่ฉันไม่แน่ใจว่า Unicode (\u03B2) นี้จะทำให้คุณเป็นรูปสี่เหลี่ยมผืนผ้า นี่คือรหัสของคุณ (หวังว่าจะช่วยได้):

from PIL import Image, ImageDraw, ImageFont, ImageFilter
import os
# __file__ is the path for your code file
directory,filename = os.path.split(__file__)

font_size=36
width=500
height=100
back_ground_color=(255,255,255)
font_size=36
font_color=(0,0,0)
unicode_text = "\u03B2 Hello"

im  =  Image.new ( "RGB", (width,height), back_ground_color )
draw  =  ImageDraw.Draw ( im )
# if the font is installed in your computer use this code
unicode_font = ImageFont.truetype("C:\\Windows\\Fonts\\Arial.ttf", 40)
#if it's in the same folder as the code, use this code
font = ImageFont.truetype(directory + "\\lmroman10-regular.otf", 40)
draw.text ( (10,10), unicode_text, font=unicode_font, fill=font_color )
draw.text ( (20,40), unicode_text, font=font, fill=font_color )

im.save("text.jpg")
person Shoaib Mirzaei    schedule 06.10.2020
comment
ฉันมีแบบอักษรอยู่ในโฟลเดอร์เดียวกันกับโค้ด ฉันสับสนไปหมดจากโฟลเดอร์ทั้งหมดที่มีฟอนต์ที่พบในคอมพิวเตอร์ อย่างไรก็ตาม ฉันไม่พบนามสกุล .ttf หรือ LM Roman เลยตั้งแต่แรกเห็น ดังนั้นฉันจึงดาวน์โหลดอันอื่นและมอบให้กับไดเร็กทอรีพร้อมโค้ดด้วยกัน - person Elena Greg; 06.10.2020
comment
เฮ้ @ElenaGreg ฉันเพิ่งอัปเดตคำตอบ แต่ฉันยังคงสับสนเกี่ยวกับสถานการณ์ของคุณ หากรหัสของคุณไม่รู้จักแบบอักษรคุณสามารถใช้คำตอบได้ หากปัญหาของคุณเกี่ยวกับการพิมพ์สี่เหลี่ยมบนรูปภาพ ฉันคิดว่าคุณค้นหาแบบอักษรอื่นหรือยูนิโค้ดอื่น ๆ แต่คุณยังไม่ตอบคำถามของฉัน ข้อผิดพลาดคืออะไร? - person Shoaib Mirzaei; 07.10.2020