Wednesday, November 4, 2015

How to compress an Image to JPG (w/o preview)

http://lazplanet.blogspot.co.id/2014/03/how-to-compress-image-to-jpg-wo-preview.html

Today we'll make a JPEG compressor. Sounds exciting, doesn't it? It can convert and compress from any other image formats too!


Very often we find out that we take a screenshot and we look for a tool to compress it in a snap. Because people can't load 1mb~ images in a website or blog. But I personally end up openingPhotoshop and saving the image from there and optionally cropping it. How if we could have our own program do it for us? It would be time-saving and fun.

Lazarus / Free Pascal has a handy TJPEGImage class which has the ability to open JPEG files as well as compress them. We can easily utilize that feature and use it to make our very own JPEG compressor. And may be we could add cropping feature in it and may be even pasting from clipboard. May be even add some custom code to add frame or watermark of a website/company logo. Cool right! We could forget photoshop for just this simple task!

Basic code


To compress any supported image formats to Compressed JPEG you can use this simple code snippet:

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
var
  pic: TPicture;
  jpg: TJPEGImage;
begin
  pic:=TPicture.Create;  jpg:=TJPEGImage.Create;
  try
    pic.LoadFromFile('C:\lena.jpg');
    jpg.Assign(pic.Bitmap);
    jpg.CompressionQuality:=70; // Enter desired quality (1~100)
    jpg.SaveToFile(Utf8ToSys('C:\test3.jpg'));
  finally
    FreeAndNil(jpg);  FreeAndNil(pic);
  end;
end;

It's very straightforward and simple. TJPEGImage cannot load any file other than .JPGs. If we want to load, for example, BMP, PNG, GIF, TIFF etc. then we need to use TPicture. We would have to load our image into the TPicture.

?
1
    pic.LoadFromFile('C:\lena.jpg');

We would also initialize the TJPEGImage and assign the Bitmap instance of our loaded picture from TPicture. (We could have chosen png or jpeg, but bitmap is uncompressed. This way we are taking the most crude image and then convert it to JPEG.) We also set the compression level for the quality. The quality should be a value between 1 and 100.

?
1
2
    jpg.CompressionQuality:=70; // Enter desired quality (1~100)
    jpg.Assign(pic.Bitmap);

After assigning the picture to TJPEGImage, when we try to SaveToFile or SaveToStream, it will give us the compressed jpeg image.


Now, I have tried to only use TJPEGImage to compress JPEG to JPEG. But it doesn't apply the compression. But the above code works perfectly. I don't know if its a bug. But anyways, let's get on with it...

Tutorial


Start Lazarus

Create a new Application Project (Project->New Project->Application->OK).

Form Design


Draw a TFileNameEdit in the form (from Misc tab). We will open our image file in this component. You can also add a TLabel and set its caption appropriately. Change the Filter property of the TFileNameEdit to:

?
1
Supported Files (*.bmp;*.xpm;*.png;*.pbm;*.ppm;*.ico;*.icns;*.cur;*.jpg;*.jpeg;*.jpe;*.jfif;*.tif;*.tiff;*.gif) |*.bmp;*.xpm;*.png;*.pbm;*.ppm;*.ico;*.icns;*.cur;*.jpg;*.jpeg;*.jpe;*.jfif;*.tif;*.tiff;*.gif|All Files|*.*

Name the TFileNameEdit as SourceFileEdit.

Draw a TGroupBox. Inside it draw a TTrackBar (Common Controls tab) and a Tlabel (as the compress level indicator). Name those 2 components QualitySlider and lblQuality respectively. Set the Min property of QualitySlider to 1 and Max to 100.

Draw 2 TButtons. Name one of them btnConvert and another one btnSave. Set their caption accordingly. Also, draw a TSaveDialog (from Dialogs tab). Set its Filter property to:

?
1
JPEG File (*.jpg)|*.jpg

Download Sample Code ZIP

You can download the above example tutorial project's source code from here:https://db.tt/dT6lFkab
Or here:  http://bit.ly/1hUpMwG
Size: 550 KB
The package contains compiled executable EXE file.



http://lazplanet.blogspot.co.id/2014/03/how-to-compress-image-to-jpg-wo-preview.html