Bulk watermark images in python
New here? Learn about Bountify and follow @bountify to get notified of new bounties! x

I have a directory of images that i need to watermark. The watermark is a png (alpha png) and all of the directory images are jpg. We need a python script that can handle adding the watermark image to the jpg images at a specific starting place on the image. the script has to be really fast.

python should be able to have a config for settings for the following

-path to the image directory (will be about 10K images)

-path to the watermark png

-coordinates of where to start the watermark (like 10,10 is 10px right and 10px down)

-the saved images should output to the same directory with the suffix of "_watermarked"

-final image should be png so its lossless and not re encoded back to jpg

this will run on a windows machine with a big cpu, we have about 10K image batches to run this against.

for proving the script works for everyone we can use repl.it and here is a sample unrelated project to show how to use repl.it https://repl.it/@husman/Image-Rotate-Resize

Hey QDev, any word on this Bounty?
bevan 2 months ago
Hi QDev, any update on this bounty ? Did you had time to pick a "winner" ?
kerncy 1 month ago
Hi QDev, any update on this bounty ?
kerncy 14 days ago
awarded to kerncy
Tags
python3

Crowdsource coding tasks.

4 Solutions

Winning solution

Hi,

please try my solution :
https://repl.it/@CyrilKern/watermark

It uses the pillow library. Do not hesitate if you need modifications.

EDIT : all the parameters are available at the beginning of the script, I tried to made it as dynamic as possible

Best regards

hey there, thanks for this. can you help me understand if there is a way to run this without touching all off the other pixels? for example if i run your code and then do a diff on the original and output i can see here that a ton of neighboring pixels got manipulated. I was hoping the jump from jpg to png would leave these relatively losselss https://online-image-comparison.com/uploads/2020-02-28/170cce8b2b15dfbca2589ef0e502f2f7.Ane9NBrrUtfJtwGMTQ9P7GvppCJetGRQ.jpg
Qdev 4 months ago
Hi, I have updated the code by changing the PNG output parameter with optimize=False and compression_level = 9. This will result to bigger file but it should be better in terms of differences. Here it is with a fuzz at 10 (default value) : https://online-image-comparison.com/uploads/2020-02-28/10da41e5eae439ad989c1fccd27e969c.uBI0cxcOCNbJ4rLSyMRXdzbsagNV3pdN.png
kerncy 4 months ago
Awesome I’ll check it out in a few hours!
Qdev 4 months ago
Hi, I have made new tests this morning, in fact the differences between optimized and not optimized pngs are very minimal. I think that wehn you made your online comparision, you set a very little fuzz. As JPG process works, I don't think that it you will have a better result than the one you show. You can try it by setting the same image with no modification, as a JPG and as a PNG (I did it with photoshop) with a fuzz at 1, you will have differences https://online-image-comparison.com/uploads/2020-02-29/34549098f89b470fab9be409ddb7b946.mRGKRZ4pANpFBZJ25dFvFSxsR1etgfOc.jpg
kerncy 4 months ago
Did you have time to check the final result ?
kerncy 4 months ago

Just my 5 cents here. I use korkut for this kind of processes all the time.

img


Here's a solution with multiprocessing support that uses ImageMagick underneath.

Solution: https://drive.google.com/open?id=1Mrm3eTpm5HJy4aqcJRsII74dZWuz6J7_

I couldn't use repl.it because the script requires ImageMagick.


Here is my solution that I wrote in Python using PIL. It is very configurable and can achieve everything you need with some optional extra goodies (like resizing the watermark and setting its transparency as well). You can find it on REPL here: https://repl.it/@ivankahl/WatermarkImages

Below is an example of the config file where you can specify the input and output directories, specify patterns for input file names, specify the suffix for outputted files as well as control various settings for the watermark like transparency, size and position. The config file below should achieve the results you specified in the question but it's very easy to modify if necessary

config.yml

# Specify the settings for the source directory
source:
  # Folder where the source images are
  directory: "images"

  # File pattern can be optionally provided as an array to
  # filter what files to select in the array.
  # If you want all files, just specify *.*
  filePatterns: 
  - "*.jpg"

# Specify the settings for the destination directory where
# the files will be outputted
destination:
  # Folder where the processed images should be saved
  directory: "images"

  # Settings for the output file name
  fileName:
    # The suffix to assign to the outputted file name
    suffix: "_watermarked"

# Specify the settings for the watermark
watermark:
  # The path to the watermark file that should be used
  image: "watermark.png"

  # Specify if the watermark should be transparent. Can be
  # any integer between 0 and 100
  # 0 = Transparent, 100 = Opaque
  transparency: 100

  # You can resize the watermark below:
  # - If you do not want to resize the watermark, 
  #   leave both values as 0.
  # - If you want to only resize by width or height and
  #   maintain the original aspect ratio, specify only
  #   the width or height and leave the other property 
  #   set to 0
  # - If you don't mind losing the original aspect ratio,
  #   specify both width and height
  resize:
    width: 0
    height: 0

  # Specify the position of the watermark on the image in pixels
  # using the top-left of the image as an anchor
  position:
    top: 10
    left: 10
View Timeline