减小 QPixmap 的文件大小

发布于 2024-12-15 03:36:13 字数 135 浏览 6 评论 0原文

当我使用scaledToHeight调整QPixmap的大小然后将其转换为QByteArray时,该ByteArray的大小恰好是未缩放的QPixmap的ByteArray的大小。是否有可能缩小 QPixmap 的像素大小和文件大小?

此致

when i resize a QPixmap with scaledToHeight and then transform it to an QByteArray, the size of this ByteArray is exactly the size of ByteArray from the unscaled QPixmap. Is there a possibility to shrink QPixmap in pixel-size and file-size?

Best regards

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

泡沫很甜 2024-12-22 03:36:13

这是一个概念验证,表明您正在做的事情肯定是可能的。也许你可以看到你在做什么不同?

#include <QtGui>

int main(int argc, char **argv) {
  QApplication app(argc, argv);

  QPixmap pixmap("/path/to/image.jpg");
  QByteArray bytes1;
  QBuffer buffer1(&bytes1);
  buffer1.open(QIODevice::WriteOnly);
  pixmap.save(&buffer1, "png");
  qDebug() << bytes1.size();

  pixmap = pixmap.scaledToHeight(100);
  QByteArray bytes2;
  QBuffer buffer2(&bytes2);
  buffer2.open(QIODevice::WriteOnly);
  pixmap.save(&buffer2, "png");
  qDebug() << bytes2.size();

  return app.exec();
}

两种猜测:

  • scaledToHeight 返回原始图像的缩放副本。它不直接缩放像素图实例。
  • 如果您重复使用相同的 QByteArray,您可能必须先截断它,然后才能看到大小差异。也就是说,即使实际内容小得多,QByteArray的容量仍可能保持较大。

Here is a proof-of-concept that shows that what you are doing is certainly possible. Maybe you can see what you are doing different?

#include <QtGui>

int main(int argc, char **argv) {
  QApplication app(argc, argv);

  QPixmap pixmap("/path/to/image.jpg");
  QByteArray bytes1;
  QBuffer buffer1(&bytes1);
  buffer1.open(QIODevice::WriteOnly);
  pixmap.save(&buffer1, "png");
  qDebug() << bytes1.size();

  pixmap = pixmap.scaledToHeight(100);
  QByteArray bytes2;
  QBuffer buffer2(&bytes2);
  buffer2.open(QIODevice::WriteOnly);
  pixmap.save(&buffer2, "png");
  qDebug() << bytes2.size();

  return app.exec();
}

Two guesses:

  • scaledToHeight returns a scaled copy of the original image. It does not scale the pixmap instance directly.
  • If you are reusing the same QByteArray, you may have to truncate it before seeing the difference in size. That is, the capacity of the QByteArray may remain larger even through the actual content is much smaller.
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文