In today's digital world, protecting your images is more important than ever. With the rise of online content, it's crucial to ensure your work is protected from unauthorized use. An effective way to do this is by applying watermarks to your images.
But what happens when you have hundreds, or even thousands of images that need to be protected? That's where automation comes into play.
With the following bash script, we simplify the process of applying large-scale watermarks. Let's see how it works.
The script loops through each image in the specified directory, obtaining its size and automatically calculating the proportional size of the watermark.
It then resizes the watermark to perfectly fit each image, removing any embedded color profiles to ensure a clean, consistent application.
Finally, apply the watermark to the bottom of each image, with a centered bottom margin for an elegant and professional presentation.
With this automated process, you can protect your images efficiently and reliably, keeping your work safe and secure at all times.
aplicar_marca_agua.sh -b [white_watermark] -n [black_watermark] -d [image_directory] -s [scale_factor] -o [output_directory]
This script automates the process of applying a watermark to all images in a specified directory. Here's a step-by-step breakdown of what the script does:
-
Verification of ImageMagick commands:
- Checks if the commands 'identify', 'convert', and 'composite' are available.
-
Definition of parameters:
MARCA_DE_AGUA_BLANCA
: Path to the image used as the white watermark.MARCA_DE_AGUA_NEGRA
: Path to the image used as the black watermark.DIRECTORIO_IMAGENES
: Directory containing the images to which the watermark will be applied.FACTOR_ESCALA
: Scale factor determining the proportional size of the watermark relative to the original image width.DIRECTORIO_SALIDA
: Directory where the images with the applied watermark will be saved.
-
Verification of parameters and paths:
- Ensures all necessary parameters are provided.
- Verifies that watermark paths and the image directory exist.
- Creates the output directory if it doesn't exist.
-
Loop to process each image:
- Iterates over each file in the specified image directory.
- Checks if the current file in the loop is a regular file using the condition
if [ -f "$IMAGE" ]
.
-
Obtaining the image size:
- Uses the
identify
command to get the size of the image in pixels.
- Uses the
-
Calculation of watermark size:
- Calculates the size of the watermark proportional to the original image size using the defined scale factor.
-
Resizing the watermark:
- Resizes the watermark to the calculated size and removes any embedded color profile to avoid warnings.
-
Applying the watermark:
- Applies the watermark to the bottom of the image with a centered bottom margin using the
composite
command.
- Applies the watermark to the bottom of the image with a centered bottom margin using the
-
Removal of temporary files:
- Deletes the temporary file of the resized watermark after applying it to the image.
clasificar_imagen_v5.py <image_path> <scale_factor>
This script loads an image and defines the bottom region of the image. It performs color-based segmentation in the bottom region and calculates the average luminosity of the bottom region. imagemagick
- Imagemagick: software suite for manipulating and editing images.
- Install using:
sudo apt-get install imagemagick
- Install using:
- bc: is a Unix-like arbitrary precision calculator.
- Install using:
sudo apt-get install bc
- Install using:
- OpenCV (
cv2
): A popular open-source library designed for computer vision and image processing.- Install using:
sudo apt-get install python3-opencv
- Install using:
- NumPy: Fundamental library in Python for scientific and numerical computing. It is used to calculate statistics such as mean and standard deviation of luminosity for color detection in the bottom region of the image.
- Install using:
sudo apt-get install python3-numpy
- Install using:
"Watermark" reaches v1.0 status. https://github.com/DrCalambre/watermark/releases/tag/v1.0
"Watermark" reaches v1.1.0 status. https://github.com/DrCalambre/watermark/releases/tag/v1.1.0
Uses YAD (Yet Another Dialog)
- ImageMagick Command Verification: Ensures that
identify
,convert
, andcomposite
commands from ImageMagick are available. - Script and Image Paths: Determines the script's directory and sets paths for a required Python script and a default image.
- File Existence Checks: Verifies the existence of necessary files before proceeding.
- User Input Prompts: Uses YAD (Yet Another Dialog) dialogs to prompt the user to select watermark images, input and output directories, and a scaling factor.
- Image Processing: Processes each image, determines the appropriate watermark color using a Python script, resizes the watermark, and applies it to the image.
- Progress and Summary: Displays a progress bar and a summary of the process using YAD.
aplicar_marca_agua.sh
- Install Dependencies:
Ensure
ImageMagick
andyad
are installed on your system.
- Install using:
sudo apt-get install imagemagick yad
2.- bc: is a Unix-like arbitrary precision calculator.
sudo apt-get install bc
3.- OpenCV (cv2
): A popular open-source library designed for computer vision and image processing.
- Install using:
sudo apt-get install python3-opencv
4.- NumPy: Fundamental library in Python for scientific and numerical computing. It is used to calculate statistics such as mean and standard deviation of luminosity for color detection in the bottom region of the image.
- Install using:
sudo apt-get install python3-numpy
Dialog to select the white and black watermarks in png format (previously created with a graphic editor such as GIMP)
Dialog to select the destination folder where the new images are placed with their corresponding watermark (white or black)
And finally it shows a summary with the files, the luminosity and the color of the watermark based on its luminosity
These scripts provide a convenient solution for batch watermarking images with customizable options for watermark color and size.
More info: https://drcalambre.blogspot.com/2024/06/automatizacion-de-marcas-de-agua-en.html
Although my blog is in Spanish, there is a language translator for a better understanding.
Best regards ;)