<p>How do you insert invisible watermarks in images for copyright purposes? I'm looking for a python library.</p>

<p>What algorithm do you use? What about performance and efficiency?</p>

[{'answer_id': 44132, 'author': 'Joel Coehoorn', 'author_id': 3043, 'author_profile': 'https://Stackoverflow.com/users/3043', 'pm_score': 1, 'selected': False, 'text': "<p>What about Exif?  It's probably not as secure as what you're thinking, but most users don't even know it exists and if you make it that easy to read the watermark information those who care will still be able to do it anyway.</p>\n"}, {'answer_id': 44172, 'author': 'dF.', 'author_id': 3002, 'author_profile': 'https://Stackoverflow.com/users/3002', 'pm_score': 1, 'selected': False, 'text': '<p>I don\'t think there is a library that does this out of the box. If you want to implement your own, I would definitely go with the <a href="http://effbot.org/zone/pil-index.htm" rel="nofollow noreferrer">Python Imaging Library</a> (PIL).</p>\n\n<p>This is <a href="http://code.activestate.com/recipes/362879/" rel="nofollow noreferrer">a Python Cookbook recipe</a> that uses PIL to add a <em>visible</em> watermark to an image. If it\'s enough for your needs, you could use this to add a watermark with enough transparency that it is only visible if you know what you are looking for.</p>\n'}, {'answer_id': 44217, 'author': 'Community', 'author_id': -1, 'author_profile': 'https://Stackoverflow.com/users/-1', 'pm_score': 3, 'selected': False, 'text': "<p>You might want to look into Steganography; that is hiding data inside of images. There are forms that won't get lost if you convert to a lossier format or even crop parts of the image out.</p>\n"}, {'answer_id': 44221, 'author': 'Thomas Owens', 'author_id': 572, 'author_profile': 'https://Stackoverflow.com/users/572', 'pm_score': 0, 'selected': False, 'text': '<p>I was going to post an answer similar to Ugh. I would suggest putting a small TXT file describing the image source (and perhaps a small copyright statement, if one applies) into the image in a manner that is difficult to detect and break.</p>\n'}, {'answer_id': 44291, 'author': 'Grant', 'author_id': 30, 'author_profile': 'https://Stackoverflow.com/users/30', 'pm_score': 0, 'selected': False, 'text': '<p>I\'m not sure how important it is to be unbreakable, but a simple solution might just be to append a text file to the end of the image. Something like "This image belongs to ...". </p>\n\n<p>If you open the image in a viewer/browser, it looks like a normal jpeg, but if you open it in a text editor, the last line would be readable.</p>\n\n<p>The same method allows you include an actual file into an image. (<a href="http://www.online-tech-tips.com/computer-tips/hide-file-in-picture/" rel="nofollow noreferrer">hide a file inside of an image</a>) I\'ve found that it\'s a bit hit-or-miss, but 7-zip files seem to work. You could hide all sorts of copywrite goodies inside the image.</p>\n\n<p>Again, it\'s not unbreakable by any stretch of the imagination, but it\'s completely invisible to the naked eye.</p>\n'}, {'answer_id': 44331, 'author': 'lilserf', 'author_id': 4545, 'author_profile': 'https://Stackoverflow.com/users/4545', 'pm_score': 0, 'selected': False, 'text': '<p>Some image formats have headers where you can store arbitrary information as well.<br>\nFor example, the <a href="http://en.wikipedia.org/wiki/Portable_Network_Graphics#Ancillary_chunks" rel="nofollow noreferrer">PNG specification</a> has a chunk where you can store text data.  This is similar to the answers above, but without adding random data to the image data itself.</p>\n'}, {'answer_id': 44510, 'author': 'Grégoire Cachet', 'author_id': 1044, 'author_profile': 'https://Stackoverflow.com/users/1044', 'pm_score': 3, 'selected': False, 'text': '<p>I\'m looking for "unbreakable" watermarks, so data stored in exif or image metadata are out.</p>\n\n<p>I have found some interesting stuff on the web while waiting for replies here:\n<a href="http://www.cosy.sbg.ac.at/~pmeerw/Watermarking/" rel="noreferrer">http://www.cosy.sbg.ac.at/~pmeerw/Watermarking/</a></p>\n\n<p>There is a master thesis that\'s fairly exhaustive about algorithms and their caracteristics (what they do and how unbreakable they are). I haven\'t got any time to read it in depth, but this stuff looks serious. There are algorithms that support JPEG compression, cropping, gamma correction or down scaling in some way. It\'s C, but I can port it to Python or use C libraries from Python.</p>\n\n<p>However, it\'s from 2001 and I guess 7 years are a long time in this field :( Does anybody have some similar and more recent stuff?</p>\n'}, {'answer_id': 45232, 'author': 'tghw', 'author_id': 2363, 'author_profile': 'https://Stackoverflow.com/users/2363', 'pm_score': 2, 'selected': False, 'text': '<p>I use the following code.  It requires PIL:</p>\n\n<pre><code>def reduceOpacity(im, opacity):\n    """Returns an image with reduced opacity."""\n    assert opacity &gt;= 0 and opacity &lt;= 1\n    if im.mode != \'RGBA\':\n        im = im.convert(\'RGBA\')\n    else:\n        im = im.copy()\n    alpha = im.split()[3]\n    alpha = ImageEnhance.Brightness(alpha).enhance(opacity)\n    im.putalpha(alpha)\n    return im\n\ndef watermark(im, mark, position, opacity=1):\n    """Adds a watermark to an image."""\n    if opacity &lt; 1:\n        mark = reduceOpacity(mark, opacity)\n    if im.mode != \'RGBA\':\n        im = im.convert(\'RGBA\')\n    # create a transparent layer the size of the image and draw the\n    # watermark in that layer.\n    layer = Image.new(\'RGBA\', im.size, (0,0,0,0))\n    if position == \'tile\':\n        for y in range(0, im.size[1], mark.size[1]):\n            for x in range(0, im.size[0], mark.size[0]):\n                layer.paste(mark, (x, y))\n    elif position == \'scale\':\n        # scale, but preserve the aspect ratio\n        ratio = min(float(im.size[0]) / mark.size[0], float(im.size[1]) / mark.size[1])\n        w = int(mark.size[0] * ratio)\n        h = int(mark.size[1] * ratio)\n        mark = mark.resize((w, h))\n        layer.paste(mark, ((im.size[0] - w) / 2, (im.size[1] - h) / 2))\n    else:\n        layer.paste(mark, position)\n    # composite the watermark with the layer\n    return Image.composite(layer, im, layer)\n\nimg = Image.open(\'/path/to/image/to/be/watermarked.jpg\')\n\nmark1 = Image.open(\'/path/to/watermark1.png\')\nmark2 = Image.open(\'/path/to/watermark2.png\')\n\nimg = watermark(img, mark1, (img.size[0]-mark1.size[0]-5, img.size[1]-mark1.size[1]-5), 0.5)\nimg = watermark(img, mark2, \'scale\', 0.01)\n</code></pre>\n\n<p>The watermark is too faint to see.  Only a solid color image would really show it.  I can use it to create an image that doesn\'t show a watermark, but if I do a bit-by-bit subtraction using the original image, I can demonstrate that my watermark is there.</p>\n\n<p>If you want to see how it works, go to <a href="http://tylergriffinphotography.com/" rel="nofollow noreferrer">TylerGriffinPhotography.com</a>.  Each image on the site is watermarked twice: once with the watermark in the lower right corner at 50% opacity (5px from the edge), and once over the whole image at 1% opacity (using "scale", which scales the watermark to the whole image).  Can you figure out what the second, low opacity watermark shape is?</p>\n'}, {'answer_id': 541170, 'author': 'Community', 'author_id': -1, 'author_profile': 'https://Stackoverflow.com/users/-1', 'pm_score': 2, 'selected': False, 'text': "<p>Well, invisible watermarking is not that easy. Check digimarc, what money did they earn on it. There is no free C/Python code that a lonely genius has written a leave it for free usage. I've implemented my own algorithm and the name of the tool is SignMyImage. Google it if interested ... F></p>\n"}, {'answer_id': 692200, 'author': 'JeffH', 'author_id': 73826, 'author_profile': 'https://Stackoverflow.com/users/73826', 'pm_score': 1, 'selected': False, 'text': '<p>There is a newer (2005) digital watermarking FAQ at <a href="http://www.watermarkingworld.org/faq.html" rel="nofollow noreferrer">watermarkingworld.org</a></p>\n'}, {'answer_id': 4063844, 'author': 'tzot', 'author_id': 6899, 'author_profile': 'https://Stackoverflow.com/users/6899', 'pm_score': 2, 'selected': False, 'text': '<p>If you\'re talking about <em>steganography</em>, here\'s an old not too-fancy module I did for a friend once (Python 2.x code):</p>\n\n<h3>the code</h3>\n\n<pre><code>from __future__ import division\n\nimport math, os, array, random\nimport itertools as it\nimport Image as I\nimport sys\n\ndef encode(txtfn, imgfn):\n    with open(txtfn, "rb") as ifp:\n        txtdata= ifp.read()\n    txtdata= txtdata.encode(\'zip\')\n\n    img= I.open(imgfn).convert("RGB")\n    pixelcount= img.size[0]*img.size[1]\n##  sys.stderr.write("image %dx%d\\n" % img.size)\n\n    factor= len(txtdata) / pixelcount\n    width= int(math.ceil(img.size[0]*factor**.5))\n    height= int(math.ceil(img.size[1]*factor**.5))\n\n    pixelcount= width * height\n    if pixelcount &lt; len(txtdata): # just a sanity check\n        sys.stderr.write("phase 2, %d bytes in %d pixels?\\n" % (len(txtdata), pixelcount))\n        sys.exit(1)\n##  sys.stderr.write("%d bytes in %d pixels (%dx%d)\\n" % (len(txtdata), pixelcount, width, height))\n    img= img.resize( (width, height), I.ANTIALIAS)\n\n    txtarr= array.array(\'B\')\n    txtarr.fromstring(txtdata)\n    txtarr.extend(random.randrange(256) for x in xrange(len(txtdata) - pixelcount))\n\n    newimg= img.copy()\n    newimg.putdata([\n        (\n            r &amp; 0xf8 |(c &amp; 0xe0)&gt;&gt;5,\n            g &amp; 0xfc |(c &amp; 0x18)&gt;&gt;3,\n            b &amp; 0xf8 |(c &amp; 0x07),\n        )\n        for (r, g, b), c in it.izip(img.getdata(), txtarr)])\n    newimg.save(os.path.splitext(imgfn)[0]+\'.png\', optimize=1, compression=9)\n\ndef decode(imgfn, txtfn):\n    img= I.open(imgfn)\n    with open(txtfn, \'wb\') as ofp:\n        arrdata= array.array(\'B\',\n            ((r &amp; 0x7) &lt;&lt; 5 | (g &amp; 0x3) &lt;&lt; 3 | (b &amp; 0x7)\n            for r, g, b in img.getdata())).tostring()\n        findata= arrdata.decode(\'zip\')\n        ofp.write(findata)\n\nif __name__ == "__main__":\n    if sys.argv[1] == \'e\':\n        encode(sys.argv[2], sys.argv[3])\n    elif sys.argv[1] == \'d\':\n        decode(sys.argv[2], sys.argv[3])\n</code></pre>\n\n<h3>the algorithm</h3>\n\n<p>It stores a byte of data per image pixel using: the 3 least-significant bits of the blue band, the 2 LSB of the green one and the 3 LSB of the red one.</p>\n\n<p>encode function: An input text file is compressed by zlib, and the input image is resized (keeping proportions) to ensure that there are at least as many pixels as compressed bytes. A <strong>PNG</strong> image with the same name as the input image (so don\'t use a ".png" filename as input if you leave the code as-is :) is saved containing the steganographic data.</p>\n\n<p>decode function: The previously stored zlib-compressed data are extracted from the input image, and saved uncompressed under the provided filename.</p>\n\n<p>I verified the old code still runs, so here\'s an example image containing steganographic data:</p>\n\n<p><img src="https://i.stack.imgur.com/9ss4V.png" alt="contains steganographic data"></p>\n\n<p>You\'ll notice that the noise added is barely visible.</p>\n'}]
['https://Stackoverflow.com/questions/44101', 'https://Stackoverflow.com', 'https://Stackoverflow.com/users/1044/']