How can I draw text with different stroke and fill colors on images with python?

后端 未结 6 601
忘了有多久
忘了有多久 2020-12-09 10:33

How can I draw text with different stroke and fill colors on images with python?

Here is some text with red stroke and gray fill.

6条回答
  •  慢半拍i
    慢半拍i (楼主)
    2020-12-09 11:09

    Using imagemagick:

    import subprocess
    
    args = {
        'bgColor': 'transparent',
        'fgColor': 'light slate grey',
        'fgOutlineColor': 'red',
        'text': 'Example',
        'size': 72,
        'geometry': '350x100!',
        'output': '/tmp/out.png',
        'font': 'helvetica'
    }
    
    cmd = ['convert', 'xc:{bgColor}', '-resize', '{geometry}', '-gravity', 'Center', 
           '-font', '{font}', '-pointsize', '{size}', '-fill', '{fgColor}', 
           '-stroke', '{fgOutlineColor}', '-draw', "text 0,0 '{text}'", '-trim', '{output}']
    cmd = [item.format(**args) for item in cmd]
    
    proc = subprocess.Popen(cmd)
    proc.communicate()
    

    enter image description here

提交回复
热议问题