Page 1 of 1

Draw Background for Transparent Image

Posted: 2009-08-04T07:18:10-07:00
by houseangel
Hi everybody,

I am very new to image processing and I am using JMagick to do some simple image processing in Java. Currently I have the following task:
- I got a PNG image including transparency (as far as I know transparency is not only 1 transparent color but also an area around the not transparent area which contains part transparency which will make the background shine through which makes a smooth edge)
- I want do resize this image, cut a piece out of it and store that piece with a given background color as JPEG

What works fine:
- crop
- scale

My problem:
- I do not know how to bring the background color to the image (i already tried to draw a new image with that color and compose with the original one but this takes a huge amount of time because the image is not that small and additionally the color did not show up (it was all black instead) and the edges around the original image were really bad)

Can anybody help me? Maybe just how to do it with ImageMagick - hopefully I can adapt this to the JMagick API.

Re: Draw Background for Transparent Image

Posted: 2009-08-04T09:23:42-07:00
by fmw42
please identify what you want to do step by step and provide a small image. we can then help you at least with the command line to get what you want if possible.

we don't understand your problem fully.

Re: Draw Background for Transparent Image

Posted: 2009-08-05T01:39:08-07:00
by houseangel
Sorry, I thought my description was good enough to make you understand. I am trying it better now:

This is an example image:
Image
As you can see it has a transparent background.

What I basically want to do is: bring a color to the background (lets say for example EAEAEA) instead of the transparency.

Re: Draw Background for Transparent Image

Posted: 2009-08-05T08:49:49-07:00
by fmw42
First of all this is still not clear. The image you provided inline is a jpg and therefore will not have transparency. (not supported by jpg). Fortunately, I accidentally clicked on the image and went to your png version.

identify -verbose shows:

identify -verbose testomn.png
Image: testomn.png
Format: PNG (Portable Network Graphics)
Class: DirectClass
Geometry: 392x332+0+0
Resolution: 118.11x118.11
Print size: 3.31894x2.81094
Units: PixelsPerCentimeter
Type: TrueColorMatte
Endianess: Undefined
Colorspace: RGB
Depth: 8-bit
Channel depth:
red: 8-bit
green: 8-bit
blue: 8-bit
alpha: 8-bit
Channel statistics:
red:
min: 15 (0.0588235)
max: 255 (1)
mean: 150.541 (0.590357)
standard deviation: 106.287 (0.416811)
kurtosis: -1.93389
skewness: -0.0722528
green:
min: 15 (0.0588235)
max: 255 (1)
mean: 150.852 (0.591578)
standard deviation: 106.091 (0.416043)
kurtosis: -1.93042
skewness: -0.0768443
blue:
min: 14 (0.054902)
max: 255 (1)
mean: 151.041 (0.592317)
standard deviation: 106.028 (0.415795)
kurtosis: -1.92622
skewness: -0.0819563
alpha:
min: 0 (0)
max: 255 (1)
mean: 123.969 (0.486153)
standard deviation: 127.139 (0.498586)
kurtosis: -1.9934
skewness: -0.0554016

This is an 8-bit alpha channel. Thus it has varying shades of transparency from fully opaque to fully transparent. To see this we can separate out the alpha channel

convert testomn.png -alpha extract testomn_alpha.png

So it looks like the left half (more or less uneven division) is fully transparent, the right half is fully opaque and the middle line is partially transparent to make a smooth transition.

To bring color (actually gray) to the background

convert testomn.png -background "#eaeaea" -flatten testomn_color.png

To bring real color to the background

convert testomn.png -background red -flatten testomn_red.png

Re: Draw Background for Transparent Image

Posted: 2009-08-06T03:00:37-07:00
by houseangel
Thank you very much. This is exactly what I wanted to know. Additionally you gave some interesting information for me on how transparency is stored here. That helps me a lot understanding the whole stuff.

However, due to the fact that I am trying to do the conversion with JMagick I have to convert that command line to JMagick API calls. My problem now is: JMagick has no flatten method as far as I can see ... Does anybody have an idea how to do that with JMagick?

Re: Draw Background for Transparent Image

Posted: 2009-08-06T07:28:14-07:00
by houseangel
For anybody who is interested: I finally got this (halfway) to work with JMagick using the following piece of code:

Code: Select all

        ImageInfo i = new ImageInfo("c:/a.png");
        MagickImage a = new MagickImage(i);        
        
        ImageInfo out = new ImageInfo();
        MontageInfo mi = new MontageInfo(i);
        mi.setBackgroundColor(PixelPacket.queryColorDatabase("#EAEAEA"));
        
        MagickImage outimage = a.montageImages(mi);
        outimage.setFileName("c:/d.png");
        
        outimage.writeImage(out);
The problem now is, that montage always results in a 128x128 image as described in the documentation of montage

I tried to fix this setting

Code: Select all

        MontageInfo mi = new MontageInfo(i);
        mi.setBackgroundColor(PixelPacket.queryColorDatabase("#EAEAEA"));
        mi.setGeometry("+0+0");
But this results in:
#
# An unexpected error has been detected by Java Runtime Environment:
#
# EXCEPTION_ACCESS_VIOLATION (0xc0000005) at pc=0x7c910f20, pid=8740, tid=10180
#
# Java VM: Java HotSpot(TM) Client VM (11.0-b16 mixed mode windows-x86)
# Problematic frame:
# C [ntdll.dll+0x10f20]
#
# An error report file with more information is saved as:
# D:\esuite\eserver3\workspace\test\hs_err_pid8740.log
#
# If you would like to submit a bug report, please visit:
# http://java.sun.com/webapps/bugreport/crash.jsp
# The crash happened outside the Java Virtual Machine in native code.
# See problematic frame for where to report the bug.
#

[error occurred during error reporting , id 0xc0000005]

Anybody got any idea what I am doing wrong? I am now just sooo close to the solution, I think ...

Re: Draw Background for Transparent Image

Posted: 2009-08-06T19:59:04-07:00
by anthony
houseangel wrote:However, due to the fact that I am trying to do the conversion with JMagick I have to convert that command line to JMagick API calls. My problem now is: JMagick has no flatten method as far as I can see ... Does anybody have an idea how to do that with JMagick?
The -flatten is actually a compose of the image onto a backgrand image of the same size but with the specified background color. there are other differences too but you are not using them.


An alternative to flatten is to use use -border 0!!!!

Code: Select all

   -bordercolor red -border 0x0
See IM Examples, Border
http://www.imagemagick.org/Usage/crop/#border

Re: Draw Background for Transparent Image

Posted: 2009-08-07T01:23:32-07:00
by houseangel
Thank you for that workaround. That works perfectly and without errors for JMagick. The source code:

Code: Select all

        ImageInfo i = new ImageInfo("c:/a.png");
        MagickImage a = new MagickImage(i);        
        
        ImageInfo out = new ImageInfo();
        a.setBorderColor(PixelPacket.queryColorDatabase("#EAEAEA"));
        MagickImage outimage = a.borderImage(new Rectangle(new Dimension(0,0)));

        outimage.setFileName("c:/d.png");
        
        outimage.writeImage(out);