使用delphi将PNGImage转换为灰度
你好呀 这是我的代码:
procedure TForm4.Button1Click(Sender: TObject);
var
png: TPNGImage;
data: PRGBQarray;
p: ^tagRGBQuad;
i, o: integer;
begin
png := TPNGImage.Create;
try
png.LoadFromFile('C:\Untitled.png');
for o := 1 to 100 do
begin
data:=png.Scanline[o];
for I := 1 to 400 do
begin
p := @data^[i];
p.rgbGreen := p.rgbBlue;
p.rgbRed := p.rgbGreen;
end;
end;
img.picture.Assign(png);
finally
png.Free;
end;
end;
它不起作用并且使图片变得混乱,我确定这是因为 rgbReserved 的原因。 我应该怎么办?
hi there
here it is my code:
procedure TForm4.Button1Click(Sender: TObject);
var
png: TPNGImage;
data: PRGBQarray;
p: ^tagRGBQuad;
i, o: integer;
begin
png := TPNGImage.Create;
try
png.LoadFromFile('C:\Untitled.png');
for o := 1 to 100 do
begin
data:=png.Scanline[o];
for I := 1 to 400 do
begin
p := @data^[i];
p.rgbGreen := p.rgbBlue;
p.rgbRed := p.rgbGreen;
end;
end;
img.picture.Assign(png);
finally
png.Free;
end;
end;
it doesn't work and it makes the pic messy, I'm sure it's because of the rgbReserved.
what should i do?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
安德烈亚斯的答案将为您提供一个良好、快速的近似值,但您会失去一些质量,因为在人眼中红色、绿色和蓝色不会以相同的强度混合。如果你想“做对”,而不是
grey := (rgbBlue + rgbGreen + rgbRed) div 3;
试试这个:
grey := round(rgbRed * .3) + round (rgbGreen * .59) + round(rgbBlue * .11);
与简单平均值相比,您的性能会受到一些影响,尽管除非您使用的是非常大的数据,否则可能不会很明显。图像。
Andreas's answer will give you a good, fast approximation, but you'll lose some quality, because red, green and blue don't mix with equal intensities in the human eye. If you want to "get it right", instead of
grey := (rgbBlue + rgbGreen + rgbRed) div 3;
try this:
grey := round(rgbRed * .3) + round(rgbGreen * .59) + round(rgbBlue * .11);
You'll get a bit of a performance hit over the simple average, though it probably won't be noticeable unless you're on a very large image.
这就是将位图灰化的方法。 (是的,如果您想灰化 PNG,您首先需要从中获取位图数据。我认为 VCL 会为您完成此操作。)
示例用法:
This is how to greyify a bitmap. (And, yes, if you want to greyify a PNG, you first need to get the bitmap data out of it. I think the VCL will do this for you.)
Sample usage:
我知道问题已经得到解答,但这是我的 2c 价值...
以下代码来自 Thany 生成的 PNGComponents 包 (PngFunctions.pas)。
有一组例程最初由 PNGImage 组件的作者发布,可以在 Code Central 上找到,它展示了如何执行其他操作,例如 Alpha 混合两个图像、旋转、叠加等。 CodeCentral 链接
I know the question has already been answered but here is my 2c worth...
The following code comes from the PNGComponents package (PngFunctions.pas) produced by Thany.
There is a set of routines, that was originally published by the author of the PNGImage components, that can be found on Code Central that shows how to do other things like Alpha blending two images, rotation, overlay, etc. CodeCentral Link
这确实应该是对 @Mason 将 RGB 转换为 GreyScale 的例程的注释,但由于我不知道如何使注释显示代码,所以我将其作为答案。
这就是我进行转换的方式:
我不知道它是否是 NTSC 公式或其他什么,但它们似乎在我的程序中工作:-)。
This really should have been a comment to @Mason's routine to turn RGB into GreyScale, but since I don't know how to make a comment show code, I'm making it an answer instead.
This is how I do the conversion:
I don't know if it is NTSC formula or whatever, but they seem to work in my programs :-).