Recent

Author Topic: Transparency...again  (Read 5372 times)

loaded

  • Hero Member
  • *****
  • Posts: 825
Re: Transparency...again
« Reply #15 on: March 25, 2021, 01:54:39 pm »
In the link, https://forum.lazarus.freepascal.org/index.php?topic=29498.0 The answer given by derek.john.eva is working.
Check out  loaded on Strava
https://www.strava.com/athletes/109391137

circular

  • Hero Member
  • *****
  • Posts: 4220
    • Personal webpage
Re: Transparency...again
« Reply #16 on: April 25, 2021, 08:19:16 am »
I am surprised by the result you get with BGRABitmap. Seems like black becomes transparent.

I suggest try:
Code: [Select]
procedure TMainForm.MakePng(screen:TBitmap;FileName: String; Trans: TColor);
var
  c: TBGRABitmap;
  xcolor: TBGRAPixel;
begin
  xcolor:= ColorToBGRA(trans,255);
  c := TBGRABitmap.Create(screen);
  c.ReplaceColor(color, BGRAPixelTransparent);
  c.ReplaceColor(clBlack, clBlackOpaque); // to fix black being transparent
  c.SaveToFile(FileName);
  c.free;
end;
Conscience is the debugger of the mind

Eager

  • New Member
  • *
  • Posts: 14
Re: Transparency...again
« Reply #17 on: April 08, 2022, 05:50:59 pm »
Hi all. New user here, learning the ropes as I go along.  :)

I found myself having a similar need as the OP: I have a .bmp with a bunch of images against a fuchsia background, and I need to replace the fuchsia with transparency to produce a PNG file. 

I've been looking and searching over the past few days and finally got to BGRABitmap, but now I'm stuck with the issue of black getting replaced by a weird clear color.  Unfortunately the last suggestion by circular -replacing black with blackopaque- didn't work. Moreover, all my attempts to replace black with ANY color using ReplaceColor were fruitless.

But then I saved as a .bmp and it looked correct, only with no transparency. So I think I managed to narrow the problem - it happens when saving to a filename with the extension '.png'. So my guess is the issue may lie within the call to "savetofile"? That's as far as I got based on my current skill level. I'd like to keep using BGRABitmap as it seems pretty straightforward but I wonder if there's any easy solution to this behavior.

TIA for your help!

winni

  • Hero Member
  • *****
  • Posts: 3197
Re: Transparency...again
« Reply #18 on: April 08, 2022, 06:57:42 pm »
Hi!

Maybe what you think is black in not pure black.

Pure black is 0/0/0 but the human eye cant distinguish it from 0/0/1 or even 5/5/5.

Load your original file into the Gimp and look what the color pipette tells you.

Winni

winni

  • Hero Member
  • *****
  • Posts: 3197
Re: Transparency...again
« Reply #19 on: April 08, 2022, 11:01:32 pm »
Hi!

I made a little demo how to convert all colors near black to another color - here cssLawnGreen.

Play around with the constant trigger. The more you increase it the more you filter out some dark greys. The range is 0..255.
0 filters only pure black = 0/0/0. 255 replaces all pixels.

Code: Pascal  [Select][+][-]
  1. procedure TForm1.Button10Click(Sender: TObject);
  2. const trigger = 20;
  3. var i: Integer;
  4.   bmp : TBgraBitmap;
  5.   p: PBGRAPixel;
  6.   kindOfBlack: Boolean;
  7. begin
  8.   bmp := TBGRAbitmap.create ('MyPicture.png');
  9.   p := bmp.data;
  10.   for i := 0 to bmp.NbPixels - 1 do
  11.     begin
  12.     KindOfBlack := (p^.red <= trigger) and (p^.green <= trigger) and (p^.blue <= trigger);
  13.     if kindOfBlack then p^ := CSSLawnGreen;
  14.     inc(p);
  15.     end;
  16.   bmp.invalidateBitmap;
  17.   bmp.draw (Image1.canvas,0,0);
  18.   bmp.saveToFile('LawnGreen.png');
  19.   bmp.free;
  20. end;
  21.  
  22.  

Perhaps this helps you.

Winni
« Last Edit: April 08, 2022, 11:11:55 pm by winni »

Eager

  • New Member
  • *
  • Posts: 14
Re: Transparency...again
« Reply #20 on: April 09, 2022, 02:36:36 pm »
Winni, you were right, there were "near black" pixels. The problem was right in front of me... *facepalm*

Thanks so much!

 

TinyPortal © 2005-2018