Can image events add a layer to a png file?
I have a png file that has a layer that has a transparent background can image events add a background layer with the color E8E8E8? and then same as a jpg and a flattened png file?
Mac Pro, macOS 10.13
You can make a difference in the Apple Support Community!
When you sign up with your Apple Account, you can provide valuable feedback to other community members by upvoting helpful replies and User Tips.
When you sign up with your Apple Account, you can provide valuable feedback to other community members by upvoting helpful replies and User Tips.
I have a png file that has a layer that has a transparent background can image events add a background layer with the color E8E8E8? and then same as a jpg and a flattened png file?
Mac Pro, macOS 10.13
One option is ImageMagick which you can run from Terminal or use in a script. For example, to add the background color #e8e8e8 to a PNG image with a transparent background and output a flattened PNG image file you can use the following command:
magick image_in.png -background "#e8e8e8" -flatten image_out.png
image_in.png:
image_out.png:
To output a flattened JPEG image file you can use the following command:
magick image_in.png -background "#e8e8e8" -flatten image_out.jpg
One option is ImageMagick which you can run from Terminal or use in a script. For example, to add the background color #e8e8e8 to a PNG image with a transparent background and output a flattened PNG image file you can use the following command:
magick image_in.png -background "#e8e8e8" -flatten image_out.png
image_in.png:
image_out.png:
To output a flattened JPEG image file you can use the following command:
magick image_in.png -background "#e8e8e8" -flatten image_out.jpg
Roote,
I wondered about ImageMagick, but did not have it installed to test, so good find. The Inkscape command-line can also output png with a background argument, but I also removed XQuartz and Inkscape, so couldn't test that either.
Hi Viking. Here's something you can test since macOS comes bundled with PHP. I'm not that familiar with PHP but I recall that PHP contains the GD library of image functions. I did some research on php.net and cobbled a solution together by mashing a couple of scripts from comments on Stack Overflow. The script:
#!/usr/bin/env php
<?php
/*
* Modification of a script by Mark Setchell
* from https://stackoverflow.com/a/39272442
* Hex to RGB conversion by John
* from https://stackoverflow.com/a/15202130
*/
// Get source PNG image with transparency
$src = imagecreatefrompng('/Users/<short username>/Desktop/image_in.png');
// Get width and height of source PNG image
$w = imagesx($src);
$h = imagesy($src);
// Convert hex color values to RGB values
$hex = "#e8e8e8";
list($r, $g, $b) = sscanf($hex, "#%02x%02x%02x");
// Create a background canvas, same size, to overlay onto
$im = imagecreatetruecolor($w, $h);
$bg = imagecolorallocate($im, $r, $g, $b);
imagefill($im, 0, 0, $bg);
// Overlay source PNG image on top of canvas
imagecopyresampled($im, $src, 0, 0, 0, 0, $w, $h, $w, $h);
// Save flattened source and background image file
imagepng($im, '/Users/<short username>/Desktop/image_out.png', 0);
?>
I tested by running the script from Terminal:
php -f '/Users/<short username>/Desktop/add_background.php'
Or...
./add_background.php ./image_in.png ./image_out.png
Changes:
// $src = imagecreatefrompng('/Users/<short username>/Desktop/image_in.png');
$src = imagecreatefrompng($argv[1]);
$out_png = $argv[2];
// imagepng($im, '/Users/<short username>/Desktop/image_out.png', 0);
imagepng($im, $out_png, 0);
Good suggestion, makes running the script practical.
However, there are issues with the output from GD as implemented in the script that would need to be resolved to move from an exploratory to a viable solution. For instance, one issue is the ballooning file size. The PNG source image is 1.6 MB approx. ImageMagick outputs 1.6 MB while GD outputs 8.6 MB. GD also strips the color profile and DPI value. ImageMagick retains both from the source image.
There is nothing in the Image Events AppleScript dictionary that supports layers, or background color capability.
That’s a shame. I’m trying to move away from scripting in photoshop. To something running in the back ground so that it frees up time to work on photoshop. Any thoughts?
Nothing else comes to mind without GUI application activity.
Yes, the PHP script works just fine. Because you are using the shebang within the script, you can run it from the command-line as:
./add_background.php
Even better, thanks.
Thanks for all the stuff to try, definitely some stuff try. Color profile will etc would be a problem though, so ImageMagik could be the one.
on holiday at the moment. If appreciate the help.
That seamed to work well my image got smaller. It appears to have stripped some of its metadata. That said I think the important stuff is still there.
Can image events add a layer to a png file?