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.

Looks like no one’s replied in a while. To start the conversation again, simply ask a new question.

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

Posted on May 24, 2019 7:41 AM

Reply
Question marked as Top-ranking reply

Posted on May 24, 2019 7:54 PM

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


12 replies
Question marked as Top-ranking reply

May 24, 2019 7:54 PM in response to MattJayC

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


May 25, 2019 9:55 AM in response to VikingOSX

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'


May 25, 2019 7:52 PM in response to VikingOSX

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.

Can image events add a layer to a png file?

Welcome to Apple Support Community
A forum where Apple customers help each other with their products. Get started with your Apple Account.