PDA

View Full Version : Mass PNG to JPEG



Markus
02-11-2008, 05:51 PM
Made this in two hours for friend (taking a few cups of coffee included (h)).
It will convert all PNGs in a folder to JPEG. You can set the compression factor yourself. The name of the output will be .jpg instead of .png, everything before will remain the same.
For learning purposes, source is included, it is a bit messy, contains a little bit for Dutch (the guy I made this for is Dutch), but the interface is fully translated.

Uses code from the PNGDelphi (http://pngdelphi.sourceforge.net/) project, ©Gustavo Daud.

Markus

Floor66
02-12-2008, 04:28 PM
Uhh. soz for noobness, but why would someone need this :$

Freddy1990
02-12-2008, 04:29 PM
Doesn't look too bad, though your coding style scares the crap out of me :)

If you don't mind, I've modified the main unit a bit, I've fortunately been able to refrain myself from renaming controls and such :p so you can just replace the unit... There was a lot of stuff in there that didn't serve any purpose and I also renamed some variables to names that make somewhat more sense :)

[Removed Code]

I'd also recommend changing the label with "Compression factor" to "JPEG Quality" as compression would suggest that a higher number results in a higher compression / less quality.

EDIT:
So I was looking trough this again and I noticed you weren't freeing your resources, that's wrong in so many ways :) Also changed the opening of the directory a bit so it won't erase the box if you don't select one and it'll go to the directory in the textbox ;)

unit pngTroep;

interface

uses
Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
Dialogs, JPeg, StdCtrls, Pngimage, FileCtrl, ComCtrls;

type
TForm1 = class(TForm)
Button1: TButton;
Edit1: TEdit;
Button2: TButton;
Memo1: TMemo;
Edit2: TEdit;
Label1: TLabel;
UpDown1: TUpDown;
Label2: TLabel;
Label3: TLabel;
procedure Button1Click(Sender: TObject);
procedure Button2Click(Sender: TObject);
procedure UpDown1Click(Sender: TObject; Button: TUDBtnType);
procedure Edit2Change(Sender: TObject);
end;

var
Form1: TForm1;

implementation

{$R *.dfm}

procedure TForm1.Button1Click(Sender: TObject);
var
s: string;
begin
if DirectoryExists(Form1.Edit1.Text) then s := Form1.Edit1.Text;
if SelectDirectory('Choose the folder containing the PNG files.', '', s) then
Form1.Edit1.Text := s;
end;

procedure TForm1.Button2Click(Sender: TObject);
var
SearchRec: TSearchRec;
Png: TPNGObject;
i: Integer;
Jpeg: TJPEGImage;
s: string;
tBmp: TBitmap;
begin
Png := TPNGObject.Create;
Jpeg := TJPEGImage.Create;
tBmp := TBitmap.Create;
try
i := StrToInt(Edit2.Text);
except
Memo1.Lines.Add('Now enter a number, PL0X!!!');
Exit;
end;
Jpeg.CompressionQuality := i;
SetCurrentDir(Edit1.Text);
if FindFirst('*.png', faAnyFile, SearchRec) = 0 then
begin
s := Trim(GetCurrentDir + '\' + SearchRec.Name);
memo1.lines.Add(s);
try
Application.ProcessMessages;
Png.LoadFromFile(s);
tBmp.Assign(Png);
Jpeg.Assign(tBmp);
s[Length(s) - 2] := 'j';
s[Length(s) - 1] := 'p';
Jpeg.SaveToFile(s);
except
memo1.lines.Add('An error occurred trying to convert: ' + s);
end;
end;
repeat
if FindNext(SearchRec) <> 0 then Break;
Jpeg.CompressionQuality := i;
s := Trim(GetCurrentDir + '\' + SearchRec.Name);
memo1.lines.Add(s);
try
Application.ProcessMessages;
Png.LoadFromFile(s);
tBmp.Assign(Png);
Jpeg.Assign(tBmp);
s[Length(s) - 2] := 'j';
s[Length(s) - 1] := 'p';
Jpeg.SaveToFile(s);
except
memo1.lines.Add('ERROR WITH FILE: ' + s);
end;
until False;
Memo1.Lines.Add('Done!');
FreeAndNil(Png);
FreeAndNil(Jpeg);
FreeAndNil(tBmp);
end;

procedure TForm1.UpDown1Click(Sender: TObject; Button: TUDBtnType);
begin
Edit2.Text := IntToStr(UpDown1.Position);
end;

procedure TForm1.Edit2Change(Sender: TObject);
begin
if Length(edit2.Text) > 0 then
if (StrToIntDef(edit2.Text, -1) < 0) then
begin
MessageBox(0, 'Please enter a number here!', 'No Number', MB_ICONERROR);
Edit2.Text := '40';
UpDown1.Position := 40;
end else
UpDown1.Position := StrToInt(edit2.Text);
end;

end.

R0b0t1
02-14-2008, 01:56 AM
I've never messed with Delphi much...


But still I don't understand why one would need this?

bullzeye95
02-14-2008, 04:20 AM
I've never messed with Delphi much...


But still I don't understand why one would need this?

Because his friend is too much of a n00b to open his PNGs up in MSPaint and save them as JPEGs :p

Freddy1990
02-14-2008, 12:08 PM
Because his friend is too much of a n00b to open his PNGs up in MSPaint and save them as JPEGs :p

That'll reduce the quality of the images to crap

bullzeye95
02-14-2008, 12:26 PM
That'll reduce the quality of the images to crap

I'll fix that then...
Because his friend is too much of a n00b to open up his PNGs in PhotoShop and save them as JPEGs :p

Markus
02-14-2008, 02:06 PM
I'll fix that then...
Because his friend is too much of a n00b to open up his PNGs in PhotoShop and save them as JPEGs :p

He needed to do thousands of images, and this will convert them all in a few minutes :)

Lee Lok Hin
02-14-2008, 02:15 PM
I'll fix that then...
Because his friend is too much of a n00b to open up his PNGs in PhotoShop and save them as JPEGs :p

I laughed out loud at this

Yeah. Not very useful to me, but the code seemt very raw.

red eyes 20
02-15-2008, 10:25 PM
markus that's totally pwnage!!!
i wander, will you make it fully convert other things like bmp's to jpg and jps to gic visa versa?
any way's, that's very cool =D

Lalaji
02-24-2008, 12:36 PM
Tried it on a 16X16 PNG. Lost major quality on jpeg.

Markus
02-24-2008, 06:34 PM
Try messing with the quality a bit.