Jump to content
Sign in to follow this  
Clément

Overlap JPG photo to oa background transparently

Recommended Posts

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 by Clément

Share this post


Link to post
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
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
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?

  • Haha 2

Share this post


Link to post

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
Sign in to follow this  

×