Python: Use Windows Api To Render Text Using A Ttf Font
What would be a full example, going from nothing to ending up with a bitmap in memory, of opening a particular .ttf file and rendering some text using that font, using the native W
Solution 1:
Done and done for rendering a font (requires PyWin32):
import ctypes
import struct
import win32con
import win32gui
import win32ui
from PIL import Image
defRGB(r, g, b):
return r | (g << 8) | (b << 16)
defnative_bmp_to_pil(hdc, bitmap_handle, width, height):
bmpheader = struct.pack("LHHHH", struct.calcsize("LHHHH"),
width, height, 1, 24) #w,h, planes=1, bitcount)
c_bmpheader = ctypes.c_buffer(bmpheader)
#3 bytes per pixel, pad lines to 4 bytes
c_bits = ctypes.c_buffer(" " * (height * ((width*3 + 3) & -4)))
res = ctypes.windll.gdi32.GetDIBits(
hdc, bitmap_handle, 0, height,
c_bits, c_bmpheader,
win32con.DIB_RGB_COLORS)
ifnot res:
raise IOError("native_bmp_to_pil failed: GetDIBits")
im = Image.frombuffer(
"RGB", (width, height), c_bits,
"raw", "BGR", (width*3 + 3) & -4, -1)
return im
classWin32Font:
def__init__(self, name, height, weight=win32con.FW_NORMAL,
italic=False, underline=False):
self.font = win32ui.CreateFont({
'name': name, 'height': height,
'weight': weight, 'italic': italic, 'underline': underline})
#create a compatible DC we can use to draw:
self.desktopHwnd = win32gui.GetDesktopWindow()
self.desktopDC = win32gui.GetWindowDC(self.desktopHwnd)
self.mfcDC = win32ui.CreateDCFromHandle(self.desktopDC)
self.drawDC = self.mfcDC.CreateCompatibleDC()
#initialize it
self.drawDC.SelectObject(self.font)
defrenderText(self, text):
"""render text to a PIL image using the windows API."""
self.drawDC.SetTextColor(RGB(255,0,0))
#create the compatible bitmap:
w,h = self.drawDC.GetTextExtent(text)
saveBitMap = win32ui.CreateBitmap()
saveBitMap.CreateCompatibleBitmap(self.mfcDC, w, h)
self.drawDC.SelectObject(saveBitMap)
#draw it
self.drawDC.DrawText(text, (0, 0, w, h), win32con.DT_LEFT)
#convert to PIL image
im = native_bmp_to_pil(self.drawDC.GetSafeHdc(), saveBitMap.GetHandle(), w, h)
#clean-up
win32gui.DeleteObject(saveBitMap.GetHandle())
return im
def__del__(self):
self.mfcDC.DeleteDC()
self.drawDC.DeleteDC()
win32gui.ReleaseDC(self.desktopHwnd, self.desktopDC)
win32gui.DeleteObject(self.font.GetSafeHandle())
def__del__(self):
win32gui.DeleteObject(self.font.GetSafeHandle())
usage:
>>>f = Win32Font("Arial", 15)>>>im = f.renderText("this is just a test")>>>im.save("c:/hope.png")
result:
brilliant!!!
To render a particular .ttf
file I'll need to dig around more.
UPDATE: Updated to calculate the bmp size:
Solution 2:
Here is updated version for Python 3 of the accepted answer (full credit to @Claudiu). I'm using "Segoe UI" as an example to show how we can perfectly match the native Windows user interface:
import ctypes, struct, win32con, win32gui, win32ui, PIL.Image
defnative_bmp_to_pil(hdc, bitmap_handle, width, height):
bmpheader = struct.pack("LHHHH", struct.calcsize("LHHHH"), width, height, 1, 24)
c_bmpheader = ctypes.c_buffer(bmpheader)
c_bits = ctypes.c_buffer(b" " * (height * ((width*3 + 3) & -4)))
res = ctypes.windll.gdi32.GetDIBits(hdc, bitmap_handle, 0, height, c_bits, c_bmpheader, win32con.DIB_RGB_COLORS)
ifnot res:
raise IOError("native_bmp_to_pil failed: GetDIBits")
im = PIL.Image.frombuffer("RGB", (width, height), c_bits, "raw", "BGR", (width*3 + 3) & -4, -1)
return im
classWin32Font:
def__init__(self, name, height, weight=win32con.FW_NORMAL, italic=False, underline=False):
self.font = win32ui.CreateFont({'name': name, 'height': height, 'weight': weight, 'italic': italic, 'underline': underline})
self.desktopHwnd = win32gui.GetDesktopWindow()
self.desktopDC = win32gui.GetWindowDC(self.desktopHwnd)
self.mfcDC = win32ui.CreateDCFromHandle(self.desktopDC)
self.drawDC = self.mfcDC.CreateCompatibleDC()
self.drawDC.SelectObject(self.font)
defrenderText(self, text):
self.drawDC.SetTextColor(0)
w,h = self.drawDC.GetTextExtent(text)
saveBitMap = win32ui.CreateBitmap()
saveBitMap.CreateCompatibleBitmap(self.mfcDC, w, h)
self.drawDC.SelectObject(saveBitMap)
self.drawDC.DrawText(text, (0, 0, w, h), win32con.DT_LEFT)
im = native_bmp_to_pil(self.drawDC.GetSafeHdc(), saveBitMap.GetHandle(), w, h)
win32gui.DeleteObject(saveBitMap.GetHandle())
return im
def__del__(self):
self.mfcDC.DeleteDC()
self.drawDC.DeleteDC()
win32gui.ReleaseDC(self.desktopHwnd, self.desktopDC)
win32gui.DeleteObject(self.font.GetSafeHandle())
f = Win32Font("Segoe UI", 15)
im = f.renderText("Hello World")
im.save("test.png")
Post a Comment for "Python: Use Windows Api To Render Text Using A Ttf Font"