This project contains two Bash scripts designed to batch process .jpg images by converting them to have a white background. The scripts are compatible with different versions of ImageMagick (v6 and v7), allowing flexibility based on the installed version.
In a project I worked on, WordPress was generating thumbnails with a black background, which often looked unappealing since the theme used a white background.
After investigating the cause of this issue, I discovered that the client was using PrestaShop, which doesn't support WebP images. To work around this limitation, the client was renaming WebP images to .jpg. As a result, many of the images we encountered had a .jpg extension but retained transparency.
To solve this problem, I developed this script, which can be run directly from the terminal in cPanel. It processes all the images, converting them to have a white background.
Once the script has been executed, you can regenerate the thumbnails, and the issue is resolved. Since implementing this solution, I no longer encountered thumbnails with black backgrounds.
- Converts all
.jpgimages to have a white background. - Allows specifying folders to omit from processing.
- Skips thumbnail images based on their naming pattern (
-[0-9]+x[0-9]+.jpg). - Provides a real-time progress bar indicating the percentage of images processed.
- Logs errors to an
error.logfile if any images fail to process.
For systems with ImageMagick v7, use the script:
convert_images_to_white_background.shFor systems with ImageMagick v6, use the script:
convert_images_to_white_background_imagemagic_v6.sh- ImageMagick: Ensure the appropriate version (v6 or v7) is installed on your system.
- Bash shell environment.
-
Clone the repository:
git clone https://github.com/your-username/image-background-converter.git cd image-background-converter -
Ensure that ImageMagick is installed:
On Ubuntu/Debian:
sudo apt-get install imagemagick
On macOS (via Homebrew):
brew install imagemagick
-
Make the script executable:
chmod +x convert_images_to_white_background.sh chmod +x convert_images_to_white_background_imagemagic_v6.sh
-
Open the relevant script for your version of ImageMagick in a text editor.
-
Edit the
omit_foldersarray at the top of the script to list any directories you want to exclude from the conversion process.Example:
omit_folders=("backup" "other_folder_to_omit")
-
Run the appropriate script in the directory containing the images:
For ImageMagick v7:
./convert_images_to_white_background.sh
For ImageMagick v6:
./convert_images_to_white_background_imagemagic_v6.sh
-
Check the
error.logfile for any issues with individual images.
- Progress will be displayed in the terminal with a visual progress bar.
- The final output will summarize how many images were processed, converted, and how many remain unconverted.
The script allows you to define folders that should be excluded from the image conversion. You can add or remove folder names in the omit_folders array.
Images with filenames that match the pattern -[0-9]+x[0-9]+.jpg are assumed to be thumbnails and are excluded from processing.
A progress bar is shown in the terminal as images are processed, showing the current percentage of completion.
Any images that fail to convert are logged in an error.log file for later review.
This project is licensed under the MIT License. See the LICENSE file for details.