PNG with a transparent background #141
-
|
Hello, A straightforward question: how to fill the transparent background of a PNG image with a given colour (say white)? Current code: trying to create a 100x100 thumbnail. local_path = "/tmp/dinamo.png"
logo_path = "/tmp/logo.png"
local_path
|> Image.thumbnail!(100)
|> Image.embed!(100, 100, background_color: :white, extend_mode: :white)
|> Image.write!(logo_path)Thanks a lot! |
Beta Was this translation helpful? Give feedback.
Replies: 2 comments 4 replies
-
|
Thanks for the prompt to get the {:ok, flattened_image} = Image.flatten(image, background_color: :green)This will return a 3-band image with the alpha band applied and the transparent areas set to Enhancements
Bug Fixes
|
Beta Was this translation helpful? Give feedback.
-
|
Looks very cool. Just one suggestion, which is to reorder some of the operations. One key thing to know about local_path
|> Image.thumbnail!(100)
|> Image.flatten!(background_color: :white)
|> Image.embed!(100, 100, background_color: :white, extend_mode: :white)
|> Image.write!(logo_path)would be the canonical way to go. |
Beta Was this translation helpful? Give feedback.


Thanks for the prompt to get the
:background_coloroption added toImage.flatten/2. Assuming your image is already a PNG with an alpha band, the best way is:This will return a 3-band image with the alpha band applied and the transparent areas set to
:green. You'll need to upgrade to Image 0.52.0 which I release just a few minutes ago. It has the following changelog entry:Enhancements
Adds
:background_coloroption toImage.flatten/2.Add
Image.Nxto host someNxconvenience functions.Bug Fixes
Fix
Image.delta_e/2to return a float (not an integer)Fix
Image.k_means/2to work with images in color spaces oth…