Link to home
Start Free TrialLog in
Avatar of esmondbridgeman
esmondbridgeman

asked on

How can I add a visual banner to the bottom a jpeg without re-compressing the jpeg and loosing quality?

I am wanting to put a banner on existing jpegs which shows a caption visually. I guess one tactic would be to create a bitmap and write the caption on it. Then convert this bitmap to a jpeg using the same parameters and quantization table as the original jpeg and join them together. But how can I do this?
Avatar of kretzschmar
kretzschmar
Flag of Germany image

like

var
  jpg : TJpegImage;
  bmp : TBitmap;
begin
  jpg := TJpegImage.Create;
  bmp := TBitmap.Create;
  try
    jpg.LoadFromFile('PathFileNameHere');
    bmp.assign(jpg);
    bmp.Canvas.Font.Name := 'WhatFontYouWantHere';
    bmp.Canvas.Font.Color := clYourTColor;
    bmp.Canvas.TextOut(xCoord,yCoord,'YourText');
    jpg.assign(bmp);
    jpg.savetofile('PathFileNameHere');
  finally
    jpg.free;
    bmp.free;
  end;
end;

just from head

meikl ;-)

   
Avatar of esmondbridgeman
esmondbridgeman

ASKER

Problem with this is that the jpeg data needs to be re-compressed and that is what I'm trying to avoid as I don't want to loose any quality in the original jpeg.

Esmond
??
if you will have your banner on your jpg permanent,
then you have to recompress/create a new jpg

if you need it just to visualize it in your own app,
then you could show the created bmp from above instead


meikl ;-)
The image is being created to email to someone and so it wont be seen inside my app. I have seen this done in a program called 'Jpeg Wizard' so it looks like it can be done but I'm wanting to incorporate it inside my delphi app.

Esmond
i dont think you will need the jpg datas

if you take an existing jpg and convert it to a bitmap you will have the quality loss in
the bitmap. if you would apply the same settings a second time on the bitmap you
would get the quality loss two times and i think you would notice that.

that means you only need to convert the jpg to a bitmap, copy your
caption into it and then convert it back into a jpg with the best quality.

you need the unit jpeg in you uses clause.

procedure ConvertToBitmap;
var TempBmp:TBitmap;
     TempImage:TImage;
    TempJpgImage:TJPEGImage;
begin
  TempImage := TImage.create(nil);
  TempImage.picture.loadfromfile( PathToYourJPGFile);

  TempBmp := TBitmap.create;

  // convert any graphic to bitmap
  tempBmp.Assign(tempImage.Picture.Graphic);

end;

the same is for jpgs just in the other direction


sorry meikl
i had opened this explorer a long time ago and it took me some time to write the comment
doesn't matter, gandalf, happens sometimes to me too  ;-)

about 'Jpeg Wizard'

this is a pegasus-software, where pegasus includes a new technology
(if it is real), so i guess pegasus will offer its new technology

about (if it is real)
usual a jpegimage is a compressed binary format, so i don't think that pegasus do its modifications wíthout de-/recompressing the initial file

meikl ;-)
> The image is being created to email to someone and so it wont be seen inside my app.
??
do you mean that you want to encode your caption inside the jpg without that it is visible to the viewer ?
at the moment I'm adding a caption in the file header using the iptc standard but I want to make sure people read this as it contains copyright info (I'm working on this for a photo library). It seems that adding a visible banner to the actual image is the best way to ensure that whoever receives the image also sees the caption info etc.
then you only need to copy the caption into the jpg
like meikl and i suggested
so it looks like its not possible to avoid re-compressing the image. Had another look at the pegasus-software and they do seem to sell a dellphi component which does it but at $4000 its a bit out of my price range. Thanks for the suggestions,

esmond
have you tried to paint directly in the canvas of the tjpgimage ?
stretchdraw is a canvas property so it should be possible to
paint directly into the jpg canvas.
but may youll suffer the compression loss, so maybe you should set the
compressionquality to 100

There's a new, losless JPG format, btw. JPG2000 I guess. Or something... Basically, it offers a lossless compression, just like the GIF format. I think it's related to the PNG format. However, I'm not sure if older applications can decompress these JPG files. (And the jpg unit doesn't support it...)
just a note
it is possible to get to the JPEG compressed image data and manipulate it ( I guess to draw on it, , but that seems Unlikely to me) however, you could add a Section of added jpeg image to the bottom or top (like a banner at the bottom), but  this is a "NEW" programming development I beleive, and not very much availible, , , and if availible, it is expensive (or so I gather), if you don't want to pay lots to get this tech, then you may just use the standard Jpeg methods of Delphi, and although there is Some image quality lost, much of the bitmap quality is lost on the very first JPEG compression of the bitmap, subsequent decompression and recompression (to about the same compression ratio) may not take much noticable quality from the image. . .

you can test this out, just decompress a Jpeg and recompress it (to about the same ratio) 20 times and see if there is great difference from the first compression
Slick, decompressing and then compressing a JPG will result in a lot more loss of detail. However, you could specify a loss percentage of 0% instead of the default 15% in which case you would still have most of the details.
Problem is, every time you compress the JPG, the method will remove a small amount of the details. No matter if the image had been decompressed before. But if you set the percentage of loss to 0, it won't lose too much. Which property was this again, that you had to set for the TJPeGImage class?
I really tried to say that there will be image loss in any JPEG compression. I was only trying to imply that a recompression will not lose as much detail as the first compression (assuming you use the same compression ratio). . I have done some testing of the loss of detail (bitmap pixel information) with repeated decompression and recompression  of jpeg images, I did most of the testing at the TJPEGImage CompressionQuality of 80, the first  compression does scatter the bitmap detail into the jpeg average "Block" compression (which you can see in high magification in a bitmap editor), but the next compressions only lose a very small amount of detail (compared to the first), I guess because the jpeg "Block" averages already exist and it seems that the jpeg compression methods just stack the same average Blocks again for the next compression, so I could not see much diference after 20 cycles (yes there is a small diference). .  

If you are trying to preserve image quality, you should start with the Bitmap image not a Jpg, but  if the jpg is all you can get. . .
ASKER CERTIFIED SOLUTION
Avatar of modulo
modulo

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial