Recent

Author Topic: Small utility to quickly make background transparent  (Read 1454 times)

RedOctober

  • Sr. Member
  • ****
  • Posts: 475
Small utility to quickly make background transparent
« on: May 06, 2023, 07:10:41 pm »
Platform: Windows Server 2016, Lazarus 2.0.10, FPC 3.2.0

Problem:  I have many small images I want to remove the background from, so that I can paste them onto other larger images, and not have the original background show up.  MS Paint is manual, I'd like to automate this. Plus the transparent copy refuses to work for me.

Condition: The background can be considered any color in the image that is the same as the color of the pixel in the top left corner.

Goal: I want to build a tiny Lazarus .exe that I can, in File Explorer, drag/drop a .jpg image file onto, and it will:

- Load the dropped image into some kind of control (I suspect TBGRAGraphicControl ?)
- Detect the color of the pixel in the top left corner
- Set all pixels in the image that match that color to transparent
- Save the image to a file with the same name, except with "_tbk.jpg" at the end.  Example: ("rose.jpg" becomes "rose_tbk.jpg")  "tbk" stands for "transparent background" [Note: this part might not be necessary, since I'm not sure if transparent pixels are saved to a file or if they are just set to white or black. If so, skip this step]
- Automatically copies the transparent image to the clipboard
- Closes itself

I cd then just paste the small image with a transparent background onto the larger image in the correct place.

Why am I doing this?

- MS Paint transparent copy function is not working (always includes the background)
- I'd like to automate this process, since I can always be sure the background color will be the same as the color of the pixel in the top left corner.

I have never used TBGRAGraphicControl before, and I'm not very familiar with image processing.  I need help with:


- Loading the file
- Detecting the top left pixel color
- Scanning the image pixels and changing them to transparent
- Saving the file [if this step is necessary or useful]
- Copying the image (including the transparent pixels) to clipboard

Any help would be greatly appreciated.  Thank you.

P.S.  I'm looking at LazPaint, and it appears to be way overkill, plus like MS Paint, it's manual, plus I can't see how to do what I want to do.  I'd rather have a tiny utility that does what I described above.



Roland57

  • Hero Member
  • *****
  • Posts: 613
    • msegui.net
Re: Small utility to quickly make background transparent
« Reply #1 on: May 06, 2023, 08:04:46 pm »
For such a tool you don't need any component. You can do it in a simple Pascal program, using the package bgrabitmappack4nogui.lpk.

Here is a simple example.
My projects are on Codeberg.

paweld

  • Hero Member
  • *****
  • Posts: 1676
Re: Small utility to quickly make background transparent
« Reply #2 on: May 06, 2023, 08:11:04 pm »
you can also do without the loop:
Code: Pascal  [Select][+][-]
  1. uses  BGRABitmap, BGRABitmapTypes;
  2.  
  3. procedure TForm1.ClearBackground(FileName: String);
  4. var
  5.   bmp: TBGRABitmap;
  6.   pb, pa: TBGRAPixel;
  7. begin
  8.   bmp := TBGRABitmap.Create(FileName);
  9.   pb := bmp.GetPixel(0, 0);
  10.   pa := pb;
  11.   pa.alpha := 0;
  12.   bmp.ReplaceColor(pb, pa);
  13.   bmp.SaveToFile(ChangeFileExt(FileName, '') + '_tbk.png');
  14.   bmp.Free;
  15. end;          
Best regards / Pozdrawiam
paweld

wp

  • Hero Member
  • *****
  • Posts: 13625
Re: Small utility to quickly make background transparent
« Reply #3 on: May 06, 2023, 08:23:00 pm »
You will have to convert the images to png format since jpg does not support an alpha channel.

There is no need to go to BGRABitmap since fcl-image can do the specified background removal, too:

Code: Pascal  [Select][+][-]
  1. program make_transparent;
  2. uses
  3.   SysUtils, fpImage, fpReadBMP, fpReadJPEG, fpWritePNG;
  4. var
  5.   img: TFPMemoryImage;
  6.   x, y: Integer;
  7.   transparentColor: TFPColor;
  8.   color: TFPColor;
  9.   srcFileName: String;
  10.   destFileName: String;
  11.   writer: TFPWriterPNG;
  12. begin
  13.   srcFileName := ParamStr(1);
  14.   destFileName := ChangeFileExt(srcFileName, '') + '_tbk.png';
  15.   img := TFPMemoryImage.Create(0, 0);
  16.   try
  17.     img.LoadFromFile(ParamStr(1));
  18.     transparentColor := img.Colors[0, 0];
  19.     for y := 0 to img.Height-1 do
  20.       for x := 0 to img.Width-1 do
  21.       begin
  22.         color := img.Colors[x, y];
  23.         if color = transparentColor then
  24.           color.Alpha := alphaTransparent;
  25.         img.Colors[x, y] := color;
  26.       end;
  27.  
  28.     writer := TFPWriterPNG.Create;
  29.     try
  30.       writer.UseAlpha := true;
  31.       img.SaveToFile(destFileName, writer);
  32.     finally
  33.       writer.Free;
  34.     end;
  35.   finally
  36.     img.Free;
  37.   end;
  38. end.

RedOctober

  • Sr. Member
  • ****
  • Posts: 475
Re: Small utility to quickly make background transparent
« Reply #4 on: May 06, 2023, 09:18:41 pm »
Perfect.  Thanks all!

TRon

  • Hero Member
  • *****
  • Posts: 4377
Re: Small utility to quickly make background transparent
« Reply #5 on: May 06, 2023, 09:24:30 pm »
@RedOctober
Do note that using jpg as source can be problematic. Depending on the used compression there is not a single background color but rather a "range of colors" that represent the background color.

Also note that picking a color (or range of colors to become transparent) that this can have side-effect that are not foreseen, such as the 'transparant' color being part of the "inner part" of your image (for example some white dots in a picture of a person at the eye).

So depending on the used compression and type of image used, your mileage may vary using the currently presented technique(s).
Today is tomorrow's yesterday.

 

TinyPortal © 2005-2018