Recent

Author Topic: [SOLVED] - Cropping an Image to non-Transparent Area  (Read 943 times)

zxandris

  • Full Member
  • ***
  • Posts: 101
[SOLVED] - Cropping an Image to non-Transparent Area
« on: March 07, 2024, 03:13:01 pm »
I've got an image, generated by BgraThumbs, and the area around it is now transparent with the thumbnail not being transparent.  How do I crop the image to only leave the non transparent (The actual) image.  Because I have to admit I have no idea how to do this.

Any help would be most appreciated, I think the image will always be some kind of rectangle area.


CJ
« Last Edit: March 08, 2024, 02:27:11 am by zxandris »

circular

  • Hero Member
  • *****
  • Posts: 4356
    • Personal webpage
Re: Cropping an Image to non-Transparent Area
« Reply #1 on: March 07, 2024, 03:35:50 pm »
Hello zxandris,

You're in luck, there is a function called GetImageBoundsWithin that will tell you what do keep and a function called GetPart that will retrieve it for you.

 :)
Conscience is the debugger of the mind

Roland57

  • Sr. Member
  • ****
  • Posts: 475
    • msegui.net
Re: Cropping an Image to non-Transparent Area
« Reply #2 on: March 07, 2024, 04:57:02 pm »
Hello!

I made for myself (with help of circular) a screenshot cropper, who removes the shadow around screenshots created by Spectacle (the screenshot utility who comes with my Linux distribution) and, after that, crops the transparent area. I attach it here.
My projects are on Gitlab and on Codeberg.

zxandris

  • Full Member
  • ***
  • Posts: 101
Re: Cropping an Image to non-Transparent Area
« Reply #3 on: March 08, 2024, 02:26:55 am »
For the sake of simplicity and for my purposes, I've turned that into a procedure helper. Which I post below in the hopes that it'll help others.  Big thanks to Roland57, it's his code I ended up using, but thanks to you all on this.

Code: Pascal  [Select][+][-]
  1. procedure CropBGRA(var LBitmap : TBgraBitmap);
  2. const
  3.      CThreshold = 162;
  4. var
  5.      LData: PBGRAPixel;
  6.      LRect: TRect;
  7.      LIndex: integer;
  8. begin
  9.      LData := LBitmap.Data;
  10.      for LIndex := LBitmap.NBPixels - 1 downto 0 do
  11.      begin
  12.        if LData^.alpha < CThreshold then
  13.          LData^.alpha := 0;
  14.        Inc(LData);
  15.      end;
  16.      LRect := LBitmap.GetImageBounds(cAlpha);
  17.      BGRAReplace(LBitmap, LBitmap.GetPart(LRect));
  18. end;
  19.  

That seems to crop out the transparent area nicely.

CJ

circular

  • Hero Member
  • *****
  • Posts: 4356
    • Personal webpage
Re: [SOLVED] - Cropping an Image to non-Transparent Area
« Reply #4 on: March 08, 2024, 09:16:50 am »
Hi!

The fonction you propose does the cropping indeed.

Note that in the modified image, if pixels had an alpha value lower than 162, they become fully transparent. So it is not just cropping, it also removes semi-transparent pixels.

To keep the semi-transparent pixels, you can adjust the code like this:
Code: Pascal  [Select][+][-]
  1. procedure CropBGRA(var LBitmap : TBgraBitmap);
  2. const
  3.      CThreshold = 162;
  4. var
  5.      LMask: TGrayscaleMask;
  6.      LData: PByte;
  7.      LRect: TRect;
  8.      LIndex: integer;
  9. begin
  10.      // make a copy of the alpha channel
  11.      LMask := TGrayscaleMask.Create(LBitmap, cAlpha);
  12.      LData := LMask.Data;
  13.      for LIndex := LMask.NbPixels - 1 downto 0 do
  14.      begin
  15.        if LData^ < CThreshold then
  16.          LData^ := 0;
  17.        Inc(LData);
  18.      end;
  19.      // get bounds of the modified mask
  20.      LRect := LMask.GetImageBounds;
  21.      LMask.Free;
  22.      BGRAReplace(LBitmap, LBitmap.GetPart(LRect));
  23. end;

Also you may want to adjust this value for example to 32 to keep most pixels, including shadows.

Regards
Conscience is the debugger of the mind

Roland57

  • Sr. Member
  • ****
  • Posts: 475
    • msegui.net
Re: [SOLVED] - Cropping an Image to non-Transparent Area
« Reply #5 on: March 08, 2024, 01:51:52 pm »
Thank you for the tip. I didn't know TGrayscaleMask.  8-)
My projects are on Gitlab and on Codeberg.

 

TinyPortal © 2005-2018