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
 Replace a known color automatically

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
AndyColmes Posted - Mar 04 2016 : 01:00:32
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


4   L A T E S T    R E P L I E S    (Newest First)
AndyColmes Posted - Mar 07 2016 : 12:06:32
Hi Bill, thank you for the response again. The second code snippet worked reasonably well. Thanks again!

Andy
w2m Posted - Mar 06 2016 : 07:43:33
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
AndyColmes Posted - Mar 06 2016 : 00:20:21
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


w2m Posted - Mar 04 2016 : 09:20:26
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