[Qt] Summary of QImage usage

Image format conversion

Convert from RGB format to BGR format

QImage::rgbSwapped()
returns a QImage in which the values ​​of the red and blue components of all pixels are swapped, effectively converting an RGB image to a BGR image.

QImage image(fileName);
QImage bgr = image.rgbSwapped();

Convert color image to grayscale

Use the QImage::convertToFormat() function, and
select QImage::Format_Grayscale8 as the parameter (requires Qt5.5 or later to support).

QImage image(fileName);
QImage gray = image.convertToFormat(QImage::Format_Grayscale8);

image save

bool QImage::save(const QString &fileName, const char *format = Q_NULLPTR, int quality = -1) const

Save format selection

The parameter format selects the format to save. The supported formats are as follows:
BMP (Windows Bitmap)
GIF (Graphic Interchange Format (optional))
JPG (Joint Photographic Experts Group)
JPEG (Joint Photographic Experts Group)
PNG (Portable Network Graphics)
PBM (Portable Bitmap)
PGM (Portable Graymap)
PPM (Portable Pixmap)
XBM (X11 Bitmap)
XPM (X11 Pixmap)

Save quality settings

quality must be in the range 0 to 100 or -1.
Specify 0 for small compressed files, 100 for large uncompressed files, and -1 (default) to use the default setting.

#include <QDir>
#include <QCoreApplication>

QString appDirPath = QCoreApplication::applicationDirPath();
QString imagePath = appDirPath + "/image.bmp";
imagePath = QDir::toNativeSeparators(imagePath);
image.save(imagePath,"BMP");

Get data in QImage

uchar *QImage::bits()
returns a pointer to the first pixel data. This is equivalent to the function scanLine(0).
Note that QImage uses implicit data sharing. This function performs a depth copy of shared pixel data.

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=325523066&siteId=291194637