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

 

ImageEn Forum
Profile    Join    Active Topics    Forum FAQ    Search this forumSearch
Forum membership is Free!  Click Join to sign-up
Username:
Password:
Save Password
Forgot your Password?

 All Forums
 ImageEn Library for Delphi, C++ and .Net
 ImageEn and IEvolution Support Forum
 Replace a known color automatically
 New Topic  Reply to Topic
Author Previous Topic Topic Next Topic  

AndyColmes

USA
351 Posts

Posted - Mar 04 2016 :  01:00:32  Show Profile  Reply
I have to replace the gray background color in an image in code to create a transparent file. The gray background color is fixed and is always the same value. What is the best and most efficient way to do this with ImageEn?

Thanking you in advance.

Andy


w2m

USA
1990 Posts

Posted - Mar 04 2016 :  09:20:26  Show Profile  Reply
The background color is not all (100%) gray or "fixed" as you state. If you zoom your image so that individual pixels are visible you will find there are over 160 colors of gray in the background. Because there are so many "shades of gray" it is difficult to use available ImageEn tools to render the shades of gray as transparent. The problem is determining which pixels have a "shade of gray". There is no code that I can find that can find the pixels with a "shade of gray" with ImageEn or with any other pascal code that I can find.

You can try to select a range of gray colors with SelectColors(TColor2TRGB(iMinRGB), TColor2TRGB(iMaxRGB)) then call convert selected pixels to transparent with a call to ImageEnView1.SetSelectedAreaAlpha(0);

So with your image try this:
procedure TForm1.Button1Click(Sender: TObject);
var
iMinRed: integer;
iMinGreen: integer;
iMinBlue: integer;
iMaxRed: integer;
iMaxGreen: integer;
iMaxBlue: integer;
iMinColor: TColor;
iMaxColor: TColor;
iMinRGB: TRGB;
iMaxRGB: TRGB;
begin
  iMinRed := 1;
  iMinGreen := 1;
  iMinBlue := 1;
  iMaxRed := 166;
  iMaxGreen := 166;
  iMaxBlue := 166;
  iMinColor := RGB(iMinRed, iMinGreen, iMinBlue);
  iMaxColor := RGB(iMaxRed, iMaxGreen, iMaxBlue);
  iMinRGB := TColor2TRGB(iMinColor);
  iMaxRGB := TColor2TRGB(iMaxColor);
  { Select the shades of gray pixels }
  ImageEnView1.SelectColors(TColor2TRGB(iMinColor), TColor2TRGB(iMaxColor));
  { Make the selection transparent }
  ImageEnView1.SetSelectedAreaAlpha(0);
  ImageEnView1.Update;
end;

Adjust the values of the iMinRed, iMinGreen, iMinBlue, iMaxRed, iMaxGreen and iMaxBlue values to adjust the selection. If all background colors were clGray then making the background transparent would be much simpler. but scanned images rarely have solid background colors.

Bill Miller
Adirondack Software & Graphics
Email: w2m@hughes.net
EBook: http://www.imageen.com/ebook/
Custom Commercial ImageEn Development
Go to Top of Page

AndyColmes

USA
351 Posts

Posted - Mar 06 2016 :  00:20:21  Show Profile  Reply
Hi Bill, thanks very much for the code snippet.

What I did was I used CastColorRange to remove the gray background and replace it with white using the values $00525252 to $00989898. Then I get an image to like this one. But I would like to add this image to a layer in a TImageEnVect, but I need to make the white background with transparent so I can merge with the other layers. What is your recommendation on how to turn this white background to transparent?

Thanks in advance, Bill.

Andy


Go to Top of Page

w2m

USA
1990 Posts

Posted - Mar 06 2016 :  07:43:33  Show Profile  Reply
var
  iRGB: TRGB;
begin
  ImageEnView1.IO.Params.BitsPerSample := 8;
  ImageEnView1.IO.Params.SamplesPerPixel := 4;
  { Get the transparent color from bottom left pixel }
  iRGB := ImageEnView1.IEBitmap.Pixels[0, ImageEnView1.IEBitmap.Height - 1];
  ImageEnView1.Proc.SetTransparentColors(iRGB, iRGB, 0);
end;

or
begin
  ImageEnView1.IO.Params.BitsPerSample := 8;
  ImageEnView1.IO.Params.SamplesPerPixel := 4;
  { Make clWhite pixels transparent }
  ImageEnView1.Proc.SetTransparentColors( TColor2TRGB(clWhite), TColor2TRGB(clWhite), 0);
end;

Bill Miller
Adirondack Software & Graphics
Email: w2m@hughes.net
EBook: http://www.imageen.com/ebook/
Custom Commercial ImageEn Development
Go to Top of Page

AndyColmes

USA
351 Posts

Posted - Mar 07 2016 :  12:06:32  Show Profile  Reply
Hi Bill, thank you for the response again. The second code snippet worked reasonably well. Thanks again!

Andy
Go to Top of Page
  Previous Topic Topic Next Topic  
 New Topic  Reply to Topic
Jump To: