### Bookstore

 Computer Math and Games in Pascal (preview) Lazarus Handbook

### Author Topic: Resize jpg and apply watermark  (Read 775 times)

#### DEN1983

• New member
• Posts: 8
##### Resize jpg and apply watermark
« on: July 22, 2024, 03:42:12 pm »
Hello

There is an original image "test.jpg "
We need to reduce it proportionally to a size of 1200 by 1200 pixels, apply a watermark "watermark.png" with a transparency of 60.
Save the result in a new file "result.jpg" with compressingquality=85.
The watermark must be applied to the entire image, it is necessary to adjust the size of the watermark to the size of the image

I got confused in the code:
Code: Pascal  [Select][+][-]
1.    JPG:=TJPGImage.Create;
2.    IMG:=TImage.Create(Nil);
4.    if JPG.Width>JPG.Height then
5.    begin
6.       scale:=1200/JPG.Width;
7.       IMG.Height:=round(JPG.Height*scale);
8.       IMG.Width:=1200;
9.    end
10.    else
11.    begin
12.       scale:=1200/JPG.Height;
13.       IMG.Width:=round(JPG.Width*scale);
14.       IMG.Height:=1200;
15.    end;
16.    IMG.Canvas.StretchDraw(rect(0,0,IMG.Width,IMG.Height),JPG);
17.    watermark:=TBGRABitmap.Create('watermark.png');
18.    watermark.Draw(IMG.picture.bitmap.canvas,0,0, false);
19.    IMG.Picture.SaveToFile('rezult.jpg');
20.    jpg.Free;
21.    IMG.Free;
22.    watermark.Free;
23.

#### circular

• Hero Member
• Posts: 4303
##### Re: Resize jpg and apply watermark
« Reply #1 on: July 23, 2024, 06:16:18 pm »
Hi Den,

If you're going to use BGRABitmap, I suggest to used it for all images. Also TImage is a visual component, it is not meant to be used to draw things internally.

To load the JPG image, you can declare it as TBGRABitmap and do:
Code: Pascal  [Select][+][-]
1. JPG := TBGRABitmap.Create('test.jpg');
2. // if you resize it to NewWidth * NewHeight:
3. BGRAReplace(JPG, JPG.Resample(NewWidth, NewHeight));

To draw the watermark:
Code: Pascal  [Select][+][-]
1. // applying a transparency of 60%
2. JPG.PutImage(0, 0, watermark, dmDrawWithTransparency, 60 * 255 div 100);

To resize the watermark, you can use StretchPutImage instead.
Conscience is the debugger of the mind

#### DEN1983

• New member
• Posts: 8
##### Re: Resize jpg and apply watermark
« Reply #2 on: July 23, 2024, 10:19:03 pm »
Thanks a lot circular!!!
This is what you need.
I am processing jpg in a console program.

Please tell me how you can then save jpg with the desired jpg-compression quality, for example 85, using TBGRABitmap?

« Last Edit: July 24, 2024, 05:51:43 am by DEN1983 »

#### circular

• Hero Member
• Posts: 4303
##### Re: Resize jpg and apply watermark
« Reply #3 on: July 24, 2024, 08:44:34 am »
You're welcome

Yes. To set the compression quality, you'll need to create an instance of TBGRAWriterJPEG from BGRAWriteJpeg unit and set the adequate properties. Then pass it as a parameter to the SaveToFile function.
Conscience is the debugger of the mind

#### DEN1983

• New member
• Posts: 8
##### Re: Resize jpg and apply watermark
« Reply #4 on: July 25, 2024, 08:40:26 am »
You're welcome

Yes. To set the compression quality, you'll need to create an instance of TBGRAWriterJPEG from BGRAWriteJpeg unit and set the adequate properties. Then pass it as a parameter to the SaveToFile function.
Thanks.