ImageEn for Delphi and C++ Builder ImageEn for Delphi and C++ Builder
ImageEn Forum
Profile    Join    Active Topics    Forum FAQ    Search this forumSearch
Forum membership is Free!  Click Join to sign-up
Username:
Password:
Save Password
Forgot your Password?

 All Forums
 ImageEn Library for Delphi, C++ and .Net
 ImageEn and IEvolution Support Forum
 Help with Automated Watermarking
 New Topic  Reply to Topic
 Printer Friendly
Author Previous Topic Topic Next Topic  

skippix

USA
50 Posts

Posted - May 21 2020 :  10:13:34  Show Profile  Reply
I've almost got this worked out, but am stuck on sizing.

I have two png watermarks, one a small bullet that I place at the bottom right and another a full-image mark.

This basically works for the first one:
function BulletMark (sFile: String): Boolean;
var s: String;
  x: Integer;
begin
  Result := False;

  ImageEnView1.IO.Params.PSD_LoadLayers:=true;
  try
  with ImageEnView1.IO do
    begin
      LoadFromFile(sFile);
      LayersAdd;
      IO.LoadFromFile(sWaterMark);
      Layers[0].Selected := True;
      Layers[1].Selected := True;
      RefreshControls;

      LayersRepositionAll (IELayer_Pos_Right, IELayer_Pos_Bottom, True, False, False);
      RefreshControls;
      RefreshLayerViewer;
      LayersMerge();
      RefreshControls;
      RefreshLayerViewer;

      x := System.Pos('.', sFile);
      s := System.Copy(s, 1, x-1) + '-bulletmark.jpg';
      IO.SaveToFile(s);
      Result := True;
    end;
  except on e:Exception do
    raise;
  end;
end;


The second one is giving me fits. I can't figure out how to size the watermark to fit. It needs to maintain it's aspect ratio and either be as tall as a horizontal image or as wide as a vertical image.

Here is the code that is not exactly working:
function CrossMark (sFile, sWaterMar: String): Boolean;
var s: String;
  x: Integer;
begin
  Result := False;

  ImageEnView1.IO.Params.PSD_LoadLayers:=true;
  try
  with ImageEnView1.IO do
    begin
      LoadFromFile(sFile);
      LayersAdd;
      IO.LoadFromFile(sWaterMark);
      LockUpdate();
      Layers[1].Opacity := 0.35;
      Layers[0].Selected := True;
      Layers[0].GroupIndex := 1;
      Layers[1].Selected := True;
      Layers[1].GroupIndex := 1;
      RefreshControls;
      UnlockUpdate();
      
      LayersRepositionAll (IELayer_Pos_HCenter, IELayer_Pos_VCenter, True, True, True);

      RefreshControls;
      RefreshLayerViewer;
      LayersMerge();
      RefreshControls;
      RefreshLayerViewer;

      x := System.Pos('.', sFile);
      s := System.Copy(s, 1, x-1) + '-protected.jpg';
      IO.SaveToFile(s);
      Result := True;
    end;
  except on e:Exception do
    raise;
  end;
end;



Also, if anyone wants to suggest some code optimization, that will be appreciated as well.

Thanks! (even if it's only a matter of pointing out the obvious)

xequte

5227 Posts

Posted - May 21 2020 :  15:48:11  Show Profile  Reply
How about these two (untested):

function BulletMark (sFile: String): Boolean;
begin
  Result := False;

  ImageEnView1.IO.Params.PSD_LoadLayers := true;
  try
  with ImageEnView1.IO do
    begin
      LoadFromFile(sFile);
      LayersAdd;
      IO.LoadFromFile(sWaterMark);
      CurrentLayer.PosX := IELayer_Pos_Right;
      CurrentLayer.PosY := IELayer_Pos_Bottom;
      // Sizing needed ?

      LayersMerge();

      RefreshControls;
      RefreshLayerViewer;

      IO.SaveToFile( ChangeFileExt( sFile, '-bulletmark.jpg' );
      Result := True;
    end;
  except on e:Exception do
    raise;
  end;
end;


function CrossMark (sFile, sWaterMar: String): Boolean;
begin
  Result := False;

  ImageEnView1.IO.Params.PSD_LoadLayers := true;
  try
  with ImageEnView1.IO do
    begin
      LoadFromFile(sFile);
      LayersAdd(sWaterMark);
      Layers[1].Opacity := 0.35;
      
      // Size layer to background
      CurrentLayer.Width  := Layers[0].Width;
      CurrentLayer.Height := Layers[0].Height;
      CurrentLayer.RestoreAspectRatio();

      CurrentLayer.PosX := IELayer_Pos_HCenter;
      CurrentLayer.PosY := IELayer_Pos_VCenter;

      LayersMerge();

      RefreshControls;
      RefreshLayerViewer;

      IO.SaveToFile( ChangeFileExt( sFile, '-bulletmark.jpg' );
      Result := True;
    end;
  except on e:Exception do
    raise;
  end;
end;


Nigel
Xequte Software
www.imageen.com
Go to Top of Page

xequte

5227 Posts

Posted - May 21 2020 :  15:51:35  Show Profile  Reply
Though if speed is a huge factor (you are processing dozens at a time), I wouldn't use layers for this task at all. I would draw the watermark directly onto the image.

See example at:

https://www.imageen.com/help/ImageEn/GetImageRectWithinArea.html

Use RenderToTIEBitmapEx to support opacity:

https://www.imageen.com/help/ImageEn/TIEBitmap.RenderToTIEBitmapEx.html



Nigel
Xequte Software
www.imageen.com
Go to Top of Page

skippix

USA
50 Posts

Posted - May 21 2020 :  17:00:10  Show Profile  Reply
Thanks for your replies!

I haven't tried the layers solution yet, as speed may be an issue (this is part of a batch routine that could involve anywhere from 5-500 images).

I need a little more help, though. I just can't figure out the examples.

I have an image file - I guess that's the destination file - and
I have a watermark file - I guess that's the source file

I want to write the source on the destination.

If it's the bullet mark, I want to write it in the lower right hand corner, 100% opacity, without changing its size.

If it's the cross mark, I want it to fit the width of a vertical image or the height of a horizontal image and I need to reduce its opacity to 35%.

Would you mind providing some code snippets that specifically address these two scenarios?

Thanks!
Go to Top of Page

xequte

5227 Posts

Posted - May 21 2020 :  21:31:23  Show Profile  Reply
Hi

It would be something like:

// Add watermark in bottom left corner of image at 30% opacity
bmpImg := TIEBitmap.create;
bmpWM := TIEBitmap.create;
bmpImg.Read('D:\image.jpeg');
bmpWM.Read('D:\Watermark.png');
wmOpacity := 0.30;
bmpWM.RenderToTIEBitmapEx( bmpImg,  
                           0, bmpImg.Height - bmpWM.Height, bmpWM.Width,  bmpWM.Height,
                           0, 0, bmpWM.Width,  bmpWM.Height,
                           True, 255, rfFastLinear, ielNormal,
                           wmOpacity );
bmpImg.Write('D:\image-marked.jpeg');
bmpImg.Free;
bmWM.Free;

// Overlay entire image with watermark at 30% opacity (maintaining aspect ratio)
bmpImg := TIEBitmap.create;
bmpWM := TIEBitmap.create;
bmpImg.Read('D:\image.jpeg');
bmpWM.Read('D:\Watermark.png');
aRect := GetImageRectWithinArea( wmBMP.Width, wmBMP.Height, bmpImg.Width, bmpImg.Height);
wmOpacity := 0.30;
bmpWM.RenderToTIEBitmapEx( bmpImg,  
                           aRect.Left, aRect.Top, aRect.Right - aRect.Left, aRect.Bottom - aRect.Top,
                           0, 0, bmpWM.Width,  bmpWM.Height,
                           True, 255, rfFastLinear, ielNormal,
                           wmOpacity );
bmpImg.Write('D:\image-marked.jpeg');
bmpImg.Free;
bmWM.Free;



Nigel
Xequte Software
www.imageen.com
Go to Top of Page

skippix

USA
50 Posts

Posted - May 22 2020 :  10:36:28  Show Profile  Reply
PERFECT! that was EXACTLY what I needed. Thanks!
Go to Top of Page
  Previous Topic Topic Next Topic  
 New Topic  Reply to Topic
 Printer Friendly
Jump To: