简体   繁体   English

如何在Qimage中设置像素并保存更改

[英]How to set pixels in Qimage and save changes

I want change some colors of pixels and save changes, but not working. 我想更改像素的某些颜色并保存更改,但是不起作用。 I have this loop. 我有这个循环。 First I print on screen true value: like 255,173..., and second cout print on screen zeros. 首先,我在屏幕上打印真实值:例如255,173 ...,第二个cout在屏幕零上打印。 Until here is good. 直到这里好。

for (int i = 0; i < image->width(); i++) {
            for (int j = 0; j < image->height(); j++) {

                QRgb pixelData = image->pixel(i,j);
                int red = qRed(pixelData);
                cout<<red<<endl;
                image->setPixel(i, j, qRgb(0, 0, 0));
                pixelData = image->pixel(i,j);
                int red2 = qRed(pixelData);
                 cout<<red2<<endl;
            }
        }

After this loop I saved image. 在此循环后,我保存了图像。 When I reopen or read this image. 当我重新打开或阅读此图像时。 I got default values. 我有默认值。

if (image->save(out.c_str())) {
            std::cout << "save successful!" <<out<<std::endl;
}

Path is good. 路径是好的。 So I think setPixel not work for save function? 所以我认为setPixel无法用于保存功能吗? How can I fix that? 我该如何解决?

You have problems with save/load, not with setPixel. 您在保存/加载时遇到问题,而不是setPixel。 May be you need to specify a format while saving, for example: 可能是您在保存时需要指定格式,例如:

image.save("somefile.jpg", "JPG");

Problem is saving in JPG. 问题是保存在JPG中。 Somehow this format change values and try making file smaller. 这种格式会以某种方式更改值并尝试使文件更小。 I saving in PNG and my values stay like I want. 我保存为PNG,但我的值仍然保持不变。

image.save("somefile.png", "PNG");

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM