Clément 148 Posted November 24, 2022 (edited) Hi, My customer has a database with a lot of employee picture ( for smartcard tags). He wants to overlap each photo to a custom background. The idea is to trim out as much as possible the white rectangular background of the employee photo, leaving only his face, and then apply it over a custom background. What are my options? (All pictures and backgrounds are JPG) Edited November 24, 2022 by Clément Share this post Link to post
PeterBelow 238 Posted November 24, 2022 3 minutes ago, Clément said: Hi, My customer has a database with a lot of employee picture ( for smartcard tags). He wants to overlap each photo to a custom background. The idea is to trim out as much as possible the white rectangular background of the employee photo, leaving only his face, and then apply it over a custom background. What are my options? If the background is indeed a single color you can treat that as transparent while drawing, using the TransparentBlt API function. You may have to convert the JPG to a TBitmap first, though. TBitmap has TransparentColor and Transparent properties, and a TImage with a TBitmap loaded also has a Transparent property that makes use of the TBitmap properties when rendering the image. If the background is not a single color you may have to preprocess the Tbitmap to turn pixels with colors "close enough" to clWhite to clWhite first. Share this post Link to post
Clément 148 Posted November 24, 2022 4 minutes ago, PeterBelow said: If the background is indeed a single color you can treat that as transparent while drawing, using the TransparentBlt API function. You may have to convert the JPG to a TBitmap first, though. TBitmap has TransparentColor and Transparent properties, and a TImage with a TBitmap loaded also has a Transparent property that makes use of the TBitmap properties when rendering the image. If the background is not a single color you may have to preprocess the Tbitmap to turn pixels with colors "close enough" to clWhite to clWhite first. Well.... It's cool enough! Here is the code I wrote. unit Unit40; interface uses Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms, Dialogs, jpeg, ExtCtrls, StdCtrls; type TForm40 = class(TForm) imgBackground: TImage; imgEmployee: TImage; imgOverlap: TImage; btnOverlap: TButton; procedure btnOverlapClick(Sender: TObject); private { Private declarations } public { Public declarations } end; var Form40: TForm40; implementation {$R *.dfm} type PRGB = ^TRGB; TRGB = record B : byte; G : Byte; R : Byte; end; function ColorBetween( Color : TRGB; I1, I2 : Byte ) : Boolean; begin result := (Color.R>=I1) and (Color.R<=I2) and (Color.G>=I1) and (Color.G<=I2) and (Color.B>=I1) and (Color.R<=I2); end; function IsWhitish(Color: TRGB): TRGB; begin if ColorBetween( Color, 210,255 ) then begin Color.R := 255; Color.G := 255; Color.B := 255; end; result := Color; end; procedure NormalizeWhite(Bitmap: TBitmap); var Row : integer; p : PRGB; Col : integer; begin for Row := 0 to Bitmap.Height-1 do begin p := PRGB(Bitmap.ScanLine[Row]); Col := Bitmap.Width; while (Col > 0) do begin p^ := IsWhitish(p^); inc(p); dec(Col); end; end; end; procedure TForm40.btnOverlapClick(Sender: TObject); var ldstBmp, lsrcBmp : TBitmap; i: Integer; j: Integer; begin lsrcBmp := TBitmap.Create; ldstBmp := TBitmap.Create; try // Loading employee picture with rectangular white background lsrcBmp.Assign(imgEmployee.Picture.Graphic); // Converting Whitish to White NormalizeWhite(lsrcBmp); // Loading destination background ldstBmp.Assign(imgBackground.Picture.Graphic); // Drawing employee picture over background transparently TransparentBlt( ldstBmp.Canvas.Handle, 50, 70 , lsrcBmp.Width, lsrcBmp.Height, lsrcBmp.Canvas.handle, 0, 0 , lsrcBmp.Width, lSrcBmp.Height, clWhite ); imgOverlap.Picture.Bitmap.Assign(ldstBmp); finally lsrcBmp.Free; ldstBmp.Free; end; end; end. The result is very cool. Thanks Share this post Link to post
Fr0sT.Brutal 900 Posted November 25, 2022 16 hours ago, Clément said: The result is very cool. So those who were wearing white T-shirts will wear " custom background " T-Shirts? 2 Share this post Link to post
Attila Kovacs 629 Posted November 25, 2022 okay everybody, we make the photoshooting today.. remember, don't smile, and close your eyes! by the way, if the background isn't just white and you don't have to automatize https://www.photoroom.com/white-background/ Share this post Link to post