Author |
Topic |
brm121966
USA
64 Posts |
Posted - Nov 29 2021 : 20:39:58
|
Hello. Help me please. How can I load 800x800 jpg 16M color file to resize 200x200px and change palette 256color and write png using only one obj TImageEnView. I got the code, but there is a problem. The code does not change the palette to 256color.
var
i, w, h, nKey: integer;;
NewExtension, OutputFilename: string;
DestImageEnView, InImageEnView : TImageEnView;
begin
InputFilename := 'cover.jpg';
InImageEnView := TImageEnView.Create(Owner);
with InImageEnView do begin
Clear;
IO.LoadFromFile( InputFilename );
end;
DestImageEnView := TImageEnView.Create(Owner);
with DestImageEnView do begin
Clear;
Assign(InImageEnView.IEBitmap);
LockUpdate;
Fit;
Stretch;
w := 200;
h := 200;
Proc.Resample(w, h, rfLanczos3, True);
Invalidate;
Proc.Update;
Proc.ConvertTo(256, ieThreshold);
IO.Params.BitsPerSample := 8;
IO.Params.SamplesPerPixel := 1;
IO.Update;
Invalidate;
IO.Params.PNG_Compression := 8;
IO.Params.Width := IEBitmap.Width;
IO.Params.Height := IEBitmap.Height;
IO.Update;
Update;
UnLockUpdate;
OutputFilename := ReplaceStr(InputFilename,'Cover','Folder');
OutputFilename := ReplaceStr(OutputFilename,'.jpg','.png');
Log('ConvertImage: save: ' + OutputFilename);
IO.SaveToFile(OutputFilename, ioPNG);
end;
end;
Thanks. |
|
xequte
38608 Posts |
|
brm121966
USA
64 Posts |
Posted - Nov 30 2021 : 18:23:30
|
Hi. Sorry. I try different options, but it doesn't work. Where would you advise to put ImageEnView1.IEBitmap.PixelFormat := ie8p and IEGlobalSettings().AutoSetBitDepth := True. Which object to control DestImageEnView or InImageEnView. Thanks.
Rudolf |
|
|
xequte
38608 Posts |
Posted - Nov 30 2021 : 22:49:42
|
Hi Rudolf
Here's an example using a TImageEnView:
var
w, h: integer;
InputFilename, OutputFilename: string;
ImageEnView1: TImageEnView;
begin
InputFilename := 'D:\im.jpg';
ImageEnView1 := TImageEnView.Create(Owner);
ImageEnView1.IO.LoadFromFile( InputFilename );
ImageEnView1.LockUpdate();
// Resize to 200x200
w := 200;
h := 200;
ImageEnView1.Proc.Resample(w, h, rfLanczos3, True);
// Reduce colors
ImageEnView1.Proc.ConvertTo(256, ieThreshold);
// Set PixelFormat to 8bit
ImageEnView1.IEBitmap.PixelFormat := ie8p;
// Use PixelFormat as authoritative source of bit-depth
IEGlobalSettings().AutoSetBitDepth := True;
ImageEnView1.UnLockUpdate();
// Save it
OutputFilename := ReplaceStr(InputFilename,'.jpg','.png');
ImageEnView1.IO.Params.PNG_Compression := 8;
ImageEnView1.IO.SaveToFile(OutputFilename, ioPNG);
ImageEnView1.Free;
end;
Nigel Xequte Software www.imageen.com
|
|
|
xequte
38608 Posts |
Posted - Nov 30 2021 : 22:54:41
|
And one just using a TIEBitmap:
var
w, h: integer;
InputFilename, OutputFilename: string;
bmp: TIEBitmap;
begin
InputFilename := 'D:\im2.jpg';
bmp := TIEBitmap.Create();
bmp.ParamsEnabled := True;
bmp.Read( InputFilename );
// Resize to 200x200
w := 200;
h := 200;
bmp.Resample(w, h, rfLanczos3, True);
// Set PixelFormat to 8bit
bmp.PixelFormat := ie8p;
// Use PixelFormat as authoritative source of bit-depth
IEGlobalSettings().AutoSetBitDepth := True;
// Save it
OutputFilename := ReplaceStr(InputFilename,'.jpg','.png');
bmp.Params.PNG_Compression := 8;
bmp.Write(OutputFilename, ioPNG);
bmp.Free
end;
Nigel Xequte Software www.imageen.com
|
|
|
brm121966
USA
64 Posts |
Posted - Dec 01 2021 : 10:16:36
|
Hi. I'm sorry. But I get Exception: hyiedefs [hyiedefs] [EIEException] [] [WIC Failure] after SaveToFile or Write and png file 0 size. I am using EmbarcaderoŽ DelphiŽ XE5 version 19.0.14356.6604 (32). Win10 (64). My app (64) converting ipg to png without converting palette and png to png resample without converting palette works. Thanks.
Rudolf |
|
|
xequte
38608 Posts |
|
brm121966
USA
64 Posts |
Posted - Dec 01 2021 : 16:53:17
|
Thanks. It works.
Rudolf |
|
|
brm121966
USA
64 Posts |
Posted - Dec 01 2021 : 18:04:05
|
Sorry. Another problem is converting the palette JPG, PNG (24 Mb) 800x800 and resizing to PNG (8 bit) 200x200 using the algorithm with TIEBitmap, returns a file without an image or a file with vertical B/W lines, the correct image size. Some files will convert normally with the same characteristics. The procedure runs without error messages. What settings do you need to make for this code to work?
P.S. Some files have rounded corners with transparent backgrounds. In the modified files, the corners are filled with white.
Thanks.
Rudolf |
|
|
brm121966
USA
64 Posts |
Posted - Dec 02 2021 : 09:27:34
|
I've Added AlphaChannel. The conversion was correct.
One problem remains. Some png files have areas with transparent backgrounds. In modified files, the background is filled with a solid color (white or black).
Rudolf |
|
|
xequte
38608 Posts |
|
brm121966
USA
64 Posts |
Posted - Dec 04 2021 : 19:43:12
|
Hi Nigel. This is my last code.
w := 400;
h := 400;
ImageEnView1 := TImageEnView.Create(nil);
with ImageEnView1 do begin
IO.LoadFromFile( InputFilename );
AlphaChannel; //Add AlphaChannel
Proc.Resample(w, h, rfLanczos3, True); //Resize to
Proc.ConvertTo(256, ieThreshold); //Reduce colors
Proc.Sharpen(10,4);
IEBitmap.PixelFormat := ie8p; // Set PixelFormat to 8bit
IEGlobalSettings().AutoSetBitDepth := True; // Use PixelFormat as authoritative source of bit-depth
OutputFilename := ReplaceStr(InputFilename,'Cover','Folder'); // Save it
OutputFilename := ReplaceStr(OutputFilename,'.jpg','.png');
IO.Params.PNG_Compression := 8;
IO.SaveToFile(OutputFilename, ioPNG); // Save it
Free;
end;
Without an alpha channel, images are spoiled.
example image: https://www.dropbox.com/s/54gcnobsh1r3ugh/image.zip?dl=0
Compilation of Folder-4.png and Folder-5.png without alpha channel I want to get a thumbnail of the original image from the Cover*.* files.
Rudolf |
|
|
brm121966
USA
64 Posts |
Posted - Dec 08 2021 : 19:39:39
|
Hi Nigel. Some files have a transparent background. In the modified files, the background is filled with white or black. Any ideas on how to fix this. Thanks.
Rudolf |
|
|
xequte
38608 Posts |
Posted - Dec 09 2021 : 23:07:52
|
Hi Rudolf
Your code works for me as long as there is an AlphaChannel (i.e. your call to ImageEnView1.AlphaChanel). Otherwise I get an output image with vertical banding. It is an issue with the PNG output filter as it only affects PNG.
We have a fix for this in the upcoming 10.3.0
Nigel Xequte Software www.imageen.com
|
|
|
brm121966
USA
64 Posts |
Posted - Dec 20 2021 : 22:06:36
|
Hi Nigel.
I now have 10.3.0, but there is a problem. The file image is not drawn correctly.
example image: https://www.dropbox.com/s/ph4lij5f73ce3zu/Blues.zip?dl=0
Thinks.
Rudolf
P.S. my code:
ImageEnView1 := TImageEnView.Create(nil);
with ImageEnView1 do begin
IO.LoadFromFile( InputFilename );
Proc.Resample(400, 400, rfLanczos3, True);
Proc.ConvertTo(256, ieThreshold); //Reduce colors
Proc.Sharpen(10,4);
IEBitmap.PixelFormat := ie8p; // Set PixelFormat to 8bit
IEGlobalSettings().AutoSetBitDepth := True;
OutputFilename := ReplaceStr(InputFilename,'Cover','Folder');
OutputFilename := ReplaceStr(OutputFilename,'.jpg','.png');
IO.Params.PNG_Compression := 8;
IO.SaveToFile(OutputFilename, ioPNG); // Save it
Free;
end; |
|
|
xequte
38608 Posts |
Posted - Dec 21 2021 : 23:18:57
|
8bit PNG format only supports index transparency, which accounts for the artifacts you see. You might want to merge/remove the alpha channel before saving.
I'll see if we can improve the output.
Nigel Xequte Software www.imageen.com
|
|
|
brm121966
USA
64 Posts |
Posted - Dec 22 2021 : 20:42:09
|
Sorry, but FastStone 7.5 converted this file (Cover.png) without artifacts. Inserted IEBitmap.RemoveAlphaChannel(True), but the transparent areas are replaced with areas with a white background. It spoils the picture. What function can I use to merge / remove the alpha channel?
Thinks.
Rudolf |
|
|
xequte
38608 Posts |
Posted - Dec 23 2021 : 17:21:19
|
Hi Rudolf
In the new year, we'll see if there is a better method to calculate the index color.
Nigel Xequte Software www.imageen.com
|
|
|
xequte
38608 Posts |
Posted - Jan 30 2022 : 20:24:10
|
Hi
You can email me for a fix for this.
If you need to tweak the result, add pngfiltw to your uses and specify a different alpha threshold value, e.g.
gPNGAlphaThreshold := 125;
Nigel Xequte Software www.imageen.com
|
|
|
brm121966
USA
64 Posts |
Posted - Feb 08 2022 : 09:25:08
|
Hi Nigel. Sorry, but how do I use this (gPNGAlphaThreshold := 125;) in the code I have. Thank you.
Rudolf |
|
|
xequte
38608 Posts |
Posted - Feb 08 2022 : 15:27:20
|
Hi Rudolf
8bit PNG files only support 1bit alpha (transparent or not-transparent pixels).
gPNGAlphaThreshold just specifies which pixels are converted to transparent or not.
So for the default of gPNGAlphaThreshold := 125; pixels with alpha values < 125 become fully transparent. Pixels with alpha values > 125 become opaque.
Nigel Xequte Software www.imageen.com
|
|
|
Topic |
|