pythonpython-imaging-libraryjpegwatermark

Drawing text in PythonMagick?


How can I drawing text in PythonMagick? I need add watermark to JPG files, but I can't find any example on Python.


Solution

  • It looks like you need to use the Image.composite() method to layer the watermark image on top of the target image:

    import PythonMagick
    
    sample = PythonMagick.Image('sample.jpg')
    watermark = PythonMagick.Image('watermark.jpg')
    
    sample.composite(watermark, 50, 50,
                     PythonMagick.CompositeOperator.AtopCompositeOp)
    
    sample.write('output.jpg')
    

    EDIT

    After looking more closely at PythonMagick, I would strongly advise against using it, as its API is quite hard to use and there is no python-friendly documentation. Instead, I would recommend using the Python Imaging Library. Here's a basic watermarking example:

    from PIL import Image, ImageDraw, ImageFont, ImageEnhance
    
    image = Image.open('sample.jpg'))
    font = ImageFont.truetype('/usr/share/fonts/TTF/DejaVuSans.ttf', 20)
    watermark = Image.new('RGBA', (120, 25), (255, 255, 255))
    draw = ImageDraw.Draw(watermark)
    draw.text((5, 0), 'Watermark', (0, 0, 0), font)
    mask = ImageEnhance.Brightness(watermark).enhance(0.2)
    image.paste(watermark, (25, 25), mask)
    image.save('output.jpg')