1

I want to set watermark text on images... so I tried using PIL library

def watermark_text(input_image,
                   output_image,
                   text, pos):
    photo = Image.open(input_image)
    drawing = ImageDraw.Draw(photo)

    color = (255, 180, 80)
    font = ImageFont.truetype("arial.ttf", 40)
    drawing.text(pos, text, fill=color, font=font)
    photo.show()
    photo.save(output_image)

if __name__ == '__main__':
    img = 'cat.jpg'
    watermark_text(img, 'cats.jpg',
                   text='Sample Location Text',
                   pos=(180, 200))

But i want text in a cross and transparent color this type:

Jessica
  • 205
  • 2
  • 6
  • 14
  • 1
    Refer this question. You can try answers in that https://stackoverflow.com/questions/245447/how-do-i-draw-text-at-an-angle-using-pythons-pil – Nuwan Sep 10 '18 at 03:55

2 Answers2

7

Here is how you can make a semi-opacity watermark appear of the color you requested:

from PIL import Image, ImageDraw, ImageFont
base = Image.open('cats.jpg').convert('RGBA')
width, height = base.size

# make a blank image for the text, initialized to transparent text color
txt = Image.new('RGBA', base.size, (255,255,255,0))

# get a font
fnt = ImageFont.truetype('arial.ttf', 40)
# get a drawing context
d = ImageDraw.Draw(txt)

x = width/2
y = height/2

# draw text, half opacity
d.text((x,y), "Hello", font=fnt, fill=(255,255,255,128))
txt = txt.rotate(45)

out = Image.alpha_composite(base, txt)
out.show()
Sumanth Rao
  • 368
  • 1
  • 7
0

You can try rotating your text as follows:

import ImageFont, ImageDraw, ImageOps

def watermark_text(input_image,
                   output_image,
                   text, pos):
    photo = Image.open(input_image)
    drawing = ImageDraw.Draw(photo)

    color = (255, 180, 80)
    font = ImageFont.truetype("arial.ttf", 40)
    drawing.text(pos, text, fill=color, font=font)
    angle=txt.rotate(17.5,  expand=1)
    photo.paste(ImageOps.colorize(w, (0,0,0), (255, 180, 80)), (242,60),  w)
    photo.show()
    photo.save(output_image)

You can use the right color in your color variable via corresponding RGB code.

A. Ner
  • 71
  • 3