1

I want to write a python program that converts given text into an image. There are tutorials for it online, but I want to curve the text over a circle.

Let's say I have the text "YOUR CURVED TEXT".

I want to draw this on an image and end up with something like following:

Final result after processing the image

I checked Pillow and OpenCV. I might be making a mistake but I don't think they have any functionality for curving the given text?

What would be the best way to do this?

Thanks in advance.

np3228
  • 85
  • 6
  • i think it's helpfull [click here](https://stackoverflow.com/a/44521963/9885352) – Sandeep Pareek Aug 30 '21 at 05:12
  • If you want vector files, you might want some kind of SVG solution. [SVGWrite](https://github.com/mozman/svgwrite) is not being actively developed, but it does have a [TextPath](https://svgwrite.readthedocs.io/en/latest/classes/text.html#textpath) method which will do what you want. [Inkscape](https://inkscape.org/) has an [extension system](https://inkscape-extensions-guide.readthedocs.io/en/latest/) which is based on Python. – bfris Aug 30 '21 at 15:53

2 Answers2

4

You can do something like that in ImageMagick using -distort arc 360.

convert -font Arial -pointsize 20 label:' Your Curved Text  Your Curved Text ' -virtual-pixel Background  -background white -distort Arc 360  -rotate -90  arc_circle_text.jpg

enter image description here

You can do that also in Python Wand, which uses ImageMagick as follows:

from wand.image import Image
from wand.font import Font
from wand.display import display

with Image() as img:
    img.background_color = 'white'
    img.font = Font('Arial', 20)
    img.read(filename='label: Your Curved Text  Your Curved Text ')
    img.virtual_pixel = 'white'
    # 360 degree arc, rotated -90 degrees
    img.distort('arc', (360,-90))
    img.save(filename='arc_text.png')
    img.format = 'png'
    display(img)

enter image description here

Thanks to Eric McConville for helping with the label: code.

fmw42
  • 46,825
  • 10
  • 62
  • 80
4

thanks to fmw42 for the answer above. I slightly modified this to allow for a transparent background and to do negative angles as well. My first submission to give back to this wonderful community :)

import wand

def curved_text_to_image(
text: str,
font_filepath: str,
font_size: int,
color: str,  #assumes hex string
curve_degree: int):
"""
Uses ImageMagik / wand - so have to ensure its installed.
"""
with wand.image.Image(width=1, height=1, resolution=(600, 600)) as img: # open an image
    with wand.drawing.Drawing() as draw:   # open a drawing objetc
        # assign font details
        draw.font = font_filepath
        draw.font_size = font_size
        draw.fill_color = wand.color.Color(color)
        # get size of text
        metrics = draw.get_font_metrics(img, text)
        height, width = int(metrics.text_height), int(metrics.text_width)
        # resize the image
        img.resize(width=width, height=height)
        # draw the text
        draw.text(0, height, text)
        draw(img)
        img.virtual_pixel = 'transparent'
        # curve_degree arc, rotated 0 degrees - ie at the top
        if curve_degree >= 0:
            img.distort('arc', (curve_degree, 0))
        else:
            # rotate it 180 degrees, then distory and rotate back 180 degrees
            img.rotate(180)
            img.distort('arc', (abs(curve_degree), 180))
        img.format = 'png'
        wand.display.display(img)
return img
Sue Donhym
  • 101
  • 5