4

I want to create text to bitmap. the text is considered long string text.

so please tell me how to create a bitmap from a text.

I tried this :

from PIL import Image, ImageFont
img = Image.new('L', (500, 500), color=0)
img_w, img_h = img.size
font = ImageFont.truetype('arial.ttf', 20)
mask = font.getmask('some text related location that is going to write here.'
                    'this is watermark text', mode='L')
mask_w, mask_h = mask.size
print(mask_w,mask_h)
print(type(mask))
d = Image.core.draw(img.im, 0)
# d = d.rotate(40)
d.draw_bitmap(((img_w - mask_w)/2, (img_h - mask_h)/2), mask, 255)
img = img.rotate(40)
img.show()
img.save('op.jpg')

But text is cut from both side. here is what i get. enter image description here

3
  • 1
    Have you tried printing your image before rotating it? img = img.rotate(40) statement might be the reason behind this. Commented Sep 12, 2018 at 7:52
  • yes..ofcourse i tried.. before rotating text is cutting same way.. Commented Sep 12, 2018 at 8:30
  • 1
    haptik.ai/tech/… try this link Commented Sep 12, 2018 at 10:00

1 Answer 1

4

The text is cut from both sides because it was already cut by the horizontal boundary of the image before you rotate it. You should create an image with a width large enough to accommodate the entire text before you rotate it. That is to say, you should create an Image object with a width of mask_w after you obtain it.

Sign up to request clarification or add additional context in comments.

3 Comments

This is not a solution for me.. If the text length is very long so every time i have to create large width. Even i create large width and text length is more then width so it is not possible to create large width
If you want a long string in your output, you need a large width. Why would you expect otherwise?
Specifically, you should create an Image object with a width of mask_w after you obtain it.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.