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), Lets use the below image to write a “Good Morning” message using OpenCV putText() method.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 () The default value is False.Īdding text on an image using OpenCV – cv2.putText() method bottomLeftOrigin (Optional): When true, the image data origin is at the bottom-left corner.lineType (Optional): It denotes the type of line you want to use.thickness (Optional): It represents the thickness of the lines used to draw a text.It takes the value in BGR format, i.e., first blue color value, then green color value, and the red color value all in range 0 to 255. color: It represents the color of the text that you want to give.The font scale factor is multiplied by the font-specific base size. fontScale: It is used to increase/decrease the size of your text. OpenCV supports only a subset of Hershey Fonts.
0 Comments
Leave a Reply. |