-
I have some code that takes a few fonts from INT10h.org and outputs them as 64x4 character images from PIL import Image, ImageDraw, ImageFont
FONT_NAME = "Px437_EverexME_7x8.ttf"
FONT_SIZE = 8
ROWS = 4
COLUMNS = 256 // ROWS
font = ImageFont.truetype(FONT_NAME, FONT_SIZE, encoding='utf-8')
def draw_bitmap() -> Image:
for row in range(ROWS):
text = ''.join(chr(i + COLUMNS * row) for i in range(COLUMNS))
if row == 0:
_, _, width_image, height_row = font.getbbox(text)
image = Image.new("RGB", (width_image, height_row * ROWS), "black")
draw = ImageDraw.Draw(image)
draw.text((0, height_row * row), text, "white", font, allow_multiline=False)
return image
draw_bitmap().save(f"{FONT_NAME}.png") But because ascii 10 is being interpreted as a newline, I get this: The docs for the text argument in
How do I bypass this and force it to ignore the newline? I feel like I'm missing something really simple. For now, I've resorted to modifying |
Beta Was this translation helpful? Give feedback.
Replies: 2 comments
-
There is currently no way to do this with I'd suggest you just copy the relevant code from def text_force_singleline(draw, xy, text, font, fill, **kwargs):
class FakeBitmap:
def load():
pass
bitmap = FakeBitmap()
mask, offset = font.getmask2(text, "L", **kwargs)
bitmap.im = mask
coord = xy[0] + offset[0], xy[1] + offset[1]
draw.bitmap(coord, bitmap, fill) (Note that your use of |
Beta Was this translation helpful? Give feedback.
-
I think it's much easier to omit |
Beta Was this translation helpful? Give feedback.
There is currently no way to do this with
ImageDraw
, and I'm not sure how much interest there would be in adding support for it.I'd suggest you just copy the relevant code from
ImageDraw.text
:(Note that your use of
font.getbbox
does not actually give you the size, but that is probably fine with your font. For y…