Skip to main content

Quick and efficient way to apply watermarks to images in Django

Project description

This project provides a simple way for you to apply custom watermarks to images on your django-powered website.

Authored by Josh VanderLinden, and some great contributors.

https://img.shields.io/pypi/v/django-watermark.svg https://img.shields.io/pypi/dm/django-watermark.svg https://img.shields.io/github/license/bashu/django-watermark.svg https://img.shields.io/travis/bashu/django-watermark.svg

Features

  • Opacity: the filter allows you to specify the transparency level for your watermark image

  • Watermark positioning: you have several options for positioning watermarks on your images

    • Absolute: you can specify exact pixel locations for your watermark

    • Relative: you can use percentages to place your watermark

    • Corners: you can position your watermark in the corners of your images

    • Random: you can tell the filter to randomly generate a position for your watermark

    • Center: you can place watermarks in the center of the target image

  • Scaling: the watermark can be scaled to cover your images or specify a scaling factor to use

  • Tiling: the watermark can be tiled across your images

  • Greyscale: you can convert the watermark to be greyscale before having it applied to the target image.

  • Rotation: you can rotate your watermark a certain number of degrees or have the rotation be random.

Installation

First install the module, preferably in a virtual environment. It can be installed from PyPI:

pip install django-watermark

Setup

First of all, you must add this project to your list of INSTALLED_APPS in settings.py :

INSTALLED_APPS += [
    "watermarker",
]

Run ./manage.py migrate. This creates the tables in your database that are necessary for operation.

Please see example application. This application is used to manually test the functionalities of this package. This also serves as a good example.

You need Django 1.4 or above to run that. It might run on older versions but that is not tested.

Upgrading from 0.1.6

Upgrading from 0.1.6 is likely to cause problems trying to apply a migration when the tables already exist. In this case a fake migration needs to be applied:

./manage.py migrate watermarker 0001 --fake

Configuration (optional)

While we’re in this section, I might as well mention a settings variable that you can override: WATERMARK_QUALITY. This should be an integer between 0 and 100. The default is 85.

By default, django-watermark obscures the original image’s file name, as the original requirements were to make it impossible to download the watermark-less image. As of version 0.1.6, you can set WATERMARK_OBSCURE_ORIGINAL to False in your setings.py to make the original image file name accessible to the user.

django-watermark also lets you configure how random watermark positioning should work. By default, a when a watermark is to be positioned randomly, only one watermarked image will be generated. If you wish to generate a random position for an image’s watermark on each request, set WATERMARK_RANDOM_POSITION_ONCE to False in your settings.py.

Usage

As mentioned above, you have several options when using django-watermark. The first thing you must do is load the filter for the template in which you wish to apply watermarks to your images.

{% load watermark %}

From the Django admin, go ahead and populate your database with some watermarks that you want to apply to your regular images. Simply specify a name for the watermark and upload the watermark image itself. It’s probably not a good idea to put commas in your watermark names. Watermarks should be transparent PNG files for best results. I can’t make any guarantees that other formats will work nicely.

The first parameter to the watermark filter _must_ be the name you specified for the watermark in the Django admin. You can then choose from a few other parameters to customize the application of the watermark. Here they are:

  • position - This one is quite customizable. First, you can plug your watermark into one corner of your images by using one of BR, BL, TR, and TL. These represent ‘bottom-right’, ‘bottom-left’, ‘top-right’, and ‘top-left’ respectively.

    Alternatively, you can use relative or absolute positioning for the watermark. Relative positioning uses percentages; absolute positioning uses exact pixels. You can mix and match these two modes of positioning, but you cannot mix and match relative/absolute with the corner positioning. When using relative/absolute positioning, the value for the position parameter is XxY, where X is the left value and Y is the top value. The left and top values must be separated with a lowercase x.

    If you wanted your watermark image to show up in the center of any image you want to watermark, you would use a position parameter such as position=50%x50% or even position=C. If you wanted the watermark to show up half-way between the left and right edges of the image and 100 pixels from the top, you would use a position parameter such as position=50%x100.

    Finally, you may tell the filter to generate a position for your watermark dynamically. To do this, use position=R.

  • opacity - This parameter allows you to specify the transparency of the applied watermark. The value must be an integer between 0 and 100, where 0 is fully transparent and 100 is fully opaque. By default, the opacity is set at 50%.

  • tile - If you want your watermark to tile across the entire image, you simply specify a parameter such as tile=1.

  • scale - If you’d like to have the watermark as big as possible on the target image and fully visible, you might want to use scale=F. If you want to specify a particular scaling factor, just use something like scale=1.43. Scale could also be a percentage of the smallest image, the one to be watermarked, dimension, for example scale=R20% would scale the watermark to be 20% of the smallest between width and height of the target image.

  • greyscale - If you want your watermark to be greyscale, you can specify the parameter greyscale=1 and all color saturation will go away.

  • rotation - Set this parameter to any integer between 0 and 359 (really any integer should work, but for your own sanity I recommend keeping the value between 0 and 359). If you want the rotation to be random, use rotation=R instead of an integer.

  • obscure - Set this parameter to 0 to make the original image’s filename visible to the user. Default is 1 (or True) to obscure the original filename.

  • noalpha - Set this to 1 to remove any alpha introduced with the watermark effect, useful to force a JPEG image to remain the same, saving a lot of space, setting to 1 effectively converts any RGBA color space to RGB. Defalt is 1 (or True).

  • quality - Set this to an integer between 0 and 100 to specify the quality of the resulting image. Default is 85.

  • random_position_once - Set this to 0 or 1 to specify the random positioning behavior for the image’s watermark. When set to 0, the watermark will be randomly placed on each request. When set to 1, the watermark will be positioned randomly on the first request, and subsequent requests will use the produced image. Default is True (random positioning only happens on first request).

Examples

{{ image_url|watermark:"My Watermark,position=br,opacity=35" }}

Looks for a watermark named “My Watermark”, place it in the bottom-right corner of the target image, using a 35% transparency level.

{{ image_url|watermark:"Your Watermark,position=tl,opacity=75" }}

Looks for a watermark named “Your Watermark”, place it in the top-left corner of the target image, using a 75% transparency level.

{{ image_url|watermark:"The Watermark,position=43%x80%,opacity=40" }}

Looks for a watermark named “The Watermark”, places it at 43% on the x-axis and 80% of the y-axis of the target image, at a transparency level of 40%.

{{ image_url|watermark:"The Watermark,position=R,opacity=10,rotation=45" }}

Looks for a watermark named “The Watermark”, randomly generates a position for it, at a transparency level of 10%, rotated 45 degrees.

{{ image_url|watermark:"w00t,opacity=40,tile=1" }}

Looks for a watermark called “w00t”, tiles it across the entire target image, at a transparency level of 40%.

Credits

I didn’t write any of the code that actually applies the watermark. I snagge fsd it from http://code.activestate.com/recipes/362879/ and turned it into a Django pluggable application. Props to Shane Hathaway.

License

django-watermark is released under the BSD license.

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

django-watermark-0.2.0.tar.gz (15.3 kB view details)

Uploaded Source

Built Distribution

django_watermark-0.2.0-py3-none-any.whl (17.8 kB view details)

Uploaded Python 3

File details

Details for the file django-watermark-0.2.0.tar.gz.

File metadata

  • Download URL: django-watermark-0.2.0.tar.gz
  • Upload date:
  • Size: 15.3 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/3.4.2 importlib_metadata/4.8.1 pkginfo/1.7.1 requests/2.26.0 requests-toolbelt/0.9.1 tqdm/4.62.3 CPython/3.10.0

File hashes

Hashes for django-watermark-0.2.0.tar.gz
Algorithm Hash digest
SHA256 fd846c08b3e674c3ba901cae832222c6e0e5be9e6f62e1b456fd153358f40c8f
MD5 1dfd02854fe3adf8d07d689cb2959d88
BLAKE2b-256 cb1993657244715e139839bc7736230e2186a9083710e9ed6d3f6293abc58d7b

See more details on using hashes here.

File details

Details for the file django_watermark-0.2.0-py3-none-any.whl.

File metadata

  • Download URL: django_watermark-0.2.0-py3-none-any.whl
  • Upload date:
  • Size: 17.8 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/3.4.2 importlib_metadata/4.8.1 pkginfo/1.7.1 requests/2.26.0 requests-toolbelt/0.9.1 tqdm/4.62.3 CPython/3.10.0

File hashes

Hashes for django_watermark-0.2.0-py3-none-any.whl
Algorithm Hash digest
SHA256 2a9af940964dc698fd3d9b7cb277f00e7feb23d80c25f15fd6cd3b263e520c66
MD5 8dce97073f1a8baa9271768169d28c29
BLAKE2b-256 2c36ac3d0ab8971c624199e6b4a61b897e8f0dff18527e1970925f2d3de6d817

See more details on using hashes here.

Supported by

AWS AWS Cloud computing and Security Sponsor Datadog Datadog Monitoring Fastly Fastly CDN Google Google Download Analytics Microsoft Microsoft PSF Sponsor Pingdom Pingdom Monitoring Sentry Sentry Error logging StatusPage StatusPage Status page