0

How do I put text in the center of a pixel using Pillow library?

draw.text((227, 15), 'Foo', (0, 0, 0) ,font=font)

The thing that I get right now is kinda like this (Red point is the pixel):

enter image description here

What I want is (again red point is the pixel):

enter image description here

I hope this explains well.
How do I achieve my target?

Note that: The question isn't about putting the text in the middle of a picture, its about putting it in the center of a pixel.

Nouman
  • 6,947
  • 7
  • 32
  • 60
  • The question isn't about putting the text in the middle of a picture, its about putting it in the center of a pixel. Please reopen – Nouman Jun 21 '20 at 20:25
  • What's the difference between the middle and the center? – pppery Jun 22 '20 at 01:33
  • @pppery You are right. There is no difference between the center and the middle. But do you know if there is a difference between *center of an image* and *center of a pixel*?? Pixel can be a point anywhere in the image, whereas center of an image is always going to be center of image. Read again the question. – Nouman Jun 22 '20 at 05:42
  • It's the same as here... https://stackoverflow.com/a/59887152/2836621 You draw your letter on a separate, blank canvas and trim it to its bounding box. Then you average the x coordinates and the y coordinates to get the centre pixel. – Mark Setchell Jun 22 '20 at 06:45

1 Answers1

-1

Some steps to paste text on center of image.

  1. create one bigger image which can fit all text.
im = Image.new(...)
  1. draw text on it
draw.text(...)
  1. check the size of text
box = im.getbbox()
  1. Then you can calculate where you should draw you text on your image.
Jason Yang
  • 11,284
  • 2
  • 9
  • 23