ImageEn for Delphi and C++ Builder ImageEn for Delphi and C++ Builder

 

ImageEn Forum
Profile    Join    Active Topics    Forum FAQ    Search this forumSearch
 All Forums
 ImageEn Library for Delphi, C++ and .Net
 ImageEn and IEvolution Support Forum
 Turning background of an image transparent

Note: You must be registered in order to post a reply.
To register, click here. Registration is FREE!

View 
UserName:
Password:
Format  Bold Italicized Underline  Align Left Centered Align Right  Horizontal Rule  Insert Hyperlink   Browse for an image to attach to your post Browse for a zip to attach to your post Insert Code  Insert Quote Insert List
   
Message 

 

Emoji
Smile [:)] Big Smile [:D] Cool [8D] Blush [:I]
Tongue [:P] Evil [):] Wink [;)] Black Eye [B)]
Frown [:(] Shocked [:0] Angry [:(!] Sleepy [|)]
Kisses [:X] Approve [^] Disapprove [V] Question [?]

 
Check here to subscribe to this topic.
   

T O P I C    R E V I E W
EricNat Posted - Jan 28 2014 : 20:41:20
Does anyone have example code (C++ Builder preferably) of a way to use TImageEnView/Proc to make the background of an image with a colored background transparent. I would like to look at the pixels in the four corners and see what color they are. If they are all the same (or at least 3 are the same) then assume that color is the background color. Then take that color and make it the alpha channel color to be transparent. Then save as PNG. Is there any routine already built in to do this sort of thing or would I need to build one to do it?

Thanks for any help you can offer.
4   L A T E S T    R E P L I E S    (Newest First)
EricNat Posted - Feb 02 2014 : 22:47:23
Yes, what I am trying to do is take an image that does not have a transparent background and "remove" the background. The original image can be any format, including JPG. I know if I was in control of the original, I would not use JPG. But I need it to support JPG as most of the images will be in JPG format to start. Most have a "white" background (recognizing it may not be truly white throughout due to the JPEG compression). So what I was trying to achieve is a method to determine the background color using the four pixels in the corners and then remove that color or anything close to it. Ideally using a threshold setting to determine how far away from the true color it goes. I was hoping there might be a built-in method for this since I assume it's a somewhat common thing to want to do, but doesn't look like one exists.

OverlayImage is a TImageEnView component.

I will email you an image as an example right now. If you could give me any ideas on how to go about building such a process, I would greatly appreciate it!

Thanks again.
Eric


w2m Posted - Jan 29 2014 : 14:49:21
What is OverlayImage?... and you really should not use JPG... JPeg does not support 32-bit with transparency... It only can handle 24-bit not transparent images.... but anyway in your case if the jpg has a white border and the pixel at the bottom left is white then the resulting png should be transparent, if all the colors in the image adjacent to the bottom-left white pixel are actually white.

If your background image in layer 0 has a lot of white around the border then you do not have to get the pixel color. Just set the transparent color to clWhite.
iColor := clWhite;
OverlayImage->Proc->SetTransparentColors(TColor2TRGB(ClWhite), TColor2TRGB(ClWhite), 0);

Another thing that may be going on is that the background has various off shades of "white" so the pixel color at the bottom left is different than the other "white" shades around it. So when you set the transparent color as white all of the other white shades remain opaque.

In this situation it may be very difficult to make everything transparent. Just remember that when you set the transparent color every pixel that matches that "precise color" in this case white, will be become transparent, but any other pixels even if they are very close to white, but not actually white will not be transparent.

If you want... send me the background image along with any other images you are using for the bitmap objects and
I'll look at it.

William Miller
Adirondack Software & Graphics
Email: w2m@frontiernet.net
EBook: http://www.imageen.com/ebook/
Apprehend: http://www.frontiernet.net/~w2m/index.html
EricNat Posted - Jan 29 2014 : 14:10:30
Hmm. Thanks for the input. Unfortunately, it doesn't work for some reason. I was using this to just try it by looking at the bottom-left pixel --

OverlayImage->IO->LoadFromFile(PicFileName);
OverlayImage->EnableAlphaChannel=true;
OverlayImage->IO->Params->BitsPerSample=8;
OverlayImage->IO->Params->SamplesPerPixel=4;
OverlayImage->Proc->SetTransparentColors(OverlayImage->IEBitmap->Pixels[0][picheight-1],OverlayImage->IEBitmap->Pixels[0][picheight-1],0);
OverlayImage->IO->SaveToFilePNG(NewPicFileName);


The resulting PNG doesn't have the background removed/transparent. It's still a solid white background. Is there something I am missing? The starting image is a JPG in this case, if that matters.

Thanks!
w2m Posted - Jan 28 2014 : 22:01:00
ImageEnView.EnableAlphaChannel := True;

{ Make 32-bit }
ImageEnView.IO.Params.BitsPerSample := 8;
ImageEnView.IO.Params.SamplesPerPixel := 4;

{Get the colors in each corner}
iColor := TRGB2TColor(ImageEnView.IEitmap.Pixels[0,0);// top left
iColor := TRGB2TColor(ImageEnView.IEitmap.Pixels[0,iImageHeight - 1);// bottom left
iColor := TRGB2TColor(ImageEnView.IEitmap.Pixels[iImageWidth-1,0);// top right
iColor := TRGB2TColor(ImageEnView.IEitmap.Pixels[iImageWidth-1,iImageHeight - 1);// bottom right

{ Set transparent color baised on the color in the bottom left }
ImageEnView.Proc.SetTransparentColors(ImageEnView.IEBitmap.Pixels[0,
              iImageHeight - 1], ImageEnView.IEBitmap.Pixels[0,
              iImageHeight - 1], 0);

This is untested or "off the top of my head".

William Miller
Adirondack Software & Graphics
Email: w2m@frontiernet.net
EBook: http://www.imageen.com/ebook/
Apprehend: http://www.frontiernet.net/~w2m/index.html