

Position = ((int) (image.shape/2 - 268/2), (int) (image.shape/2 - 36/2)) from PIL import Image, ImageFont, ImageDraw, ImageEnhance sourceimg Image.open ('input.jpg').convert ('RGBA') font uetype ('arial') text 'very loooooooooooooooooong text' get text size textsize font. You may have to find the shape of your specific text this using Paint or other application. The approximate shape of the text in the above example is (268, 36). If you know the shape (width, height) of the text you are writing on the image, then you can place at center aligned on the image. Input Image: sample.png Output Image: output.png Python – Write Text at the center of the image Following are the input and output images. We have written the output image to a file. In the above example, we have provided a sample image, and wrote some text on the image. position: distance along horizontal and vertical axis from top left corner of the image.Position, #position at which writing has to startĪs you can see from the above example, you can provide Image, #numpy array on which text is written

Image = cv2.imread('sample.png',cv2.IMREAD_UNCHANGED) The usage of putText() function is provided in the following example. Im_draw.To write text on image with OpenCV library of Python, use putText() method. # add some margin to avoid touching bordersīox_width = text_box - text_box - (2*MARGIN) While i < len(words) and (line_width + FONT.getsize(words)) < width_limit: # get text which can fit in one line, remains is list of words left over The function get_line returns the current line and remaining words, which can again be used in loop, as in draw_lines function below. But for most cases it should be OK.Ī minimal example, keep adding words until it exceeds the maximum width limit. It's probably not super-fast, because it renders whole text word by word, to determine words width. ' '.join()Įxpected_width = word_width if not buf else \īuf_width + self.space_width + word_widthĮxample usage: wrapper = TextWrapper(text, image_font_intance, 800) """ Helper class to wrap text in lines, based on given text, fontĭef _init_(self, text, font, max_width): I've wrote simple helper class to wrap text regarding to real font letters sizing: from PIL import Image, ImageDraw #n('main()') # if you want to do some profilingĪll recommendations about textwrap usage fail to determine correct width for non-monospaced fonts (as Arial, used in topic example code).

Text2 = "You could use textwrap.wrap to break text into a list of strings, each at most width characters long"ĭraw_multiple_line_text(image, text1, font, text_color, text_start_height)ĭraw_multiple_line_text(image, text2, font, text_color, 400) Text1 = "I try to add text at the bottom of image and actually I've done it, but in case of my text is longer then image width it is cut from both sides, to simplify I would like text to be in multiple lines if it is longer than image width." Line_width, line_height = font.getsize(line)ĭraw.text(((image_width - line_width) / 2, y_text), For a complete working example using unutbu's trick (tested with Python 3.6 and Pillow 5.3.0): from PIL import Image, ImageDraw, ImageFontĭef draw_multiple_line_text(image, text, font, text_color, text_start_height):įrom unutbu on ()
