Skip to content

Devanagari font not working #2255

@manishp11

Description

@manishp11

input

I am printing text written in devanagari unicode to jpg and png

all i can see in images in boxes.

python 2 and 3 both, with pillow pip install

# Draw (Bitmap Font) Text to Image
# coding: utf-8
from PIL import Image, ImageDraw, ImageFont

def reverseColor(r, g, b):
    return (255 - r, 255 - g, 255 - b)
def grayscaleColor(r, g, b):
    a = (r + g + b) / 3
    return (a, a, a)

text = "तपःस्वाध्यायनिरतं तपस्वी वाग्विदां वरम्"
textColor = (0, 255, 0) # RGB lime
#textBackgroundColor = (255, 0, 0) # RGB Red
textX = 200 # text width in pixels
textY = 50 # text height in pixels
textTopLeftX = 100
textTopLeftY = 100

# create new image
# imgx = 1920 # image width in pixels
# imgy = 1080 # image height in pixels
# image = Image.new("RGB", (imgx, imgy))

# load image
image = Image.open("input.png")
(imgx, imgy) = image.size
image = image.resize((imgx, imgy), Image.BICUBIC)

font = ImageFont.truetype("Devnew.ttf", 15) # load default bitmap font
(width, height) = font.getsize(text)
textImage = font.getmask(text)
pixels = image.load()
for y in range(imgy):
    by = int(height * (y - textTopLeftY) / textY + 0.5)
    if by >= 0 and by < height:
        for x in range(imgx):
            bx = int(width * (x - textTopLeftX) / textX + 0.5)
            if bx >= 0 and bx < width:
                if textImage.getpixel((bx, by)) == 0: # text background
                    # pass # transparent background
                    # pixels[x, y] = textBackgroundColor
                    (r, g, b, a) = pixels[x, y]
                    #(r, g, b) = pixels[x, y]
                    #pixels[x, y] = grayscaleColor(r, g, b)
                else: # text foreground
                    pixels[x, y] = textColor                
                    (r, g, b, a) = pixels[x, y]
                    # (r, g, b) = pixels[x, y]
                    # pixels[x, y] = reverseColor(r, g, b)

image.save("output.png", "PNG")

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    Status

    Closed

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions