Skip to content

Instantly share code, notes, and snippets.

@estan
Created April 11, 2019 15:05
Show Gist options
  • Save estan/011d8b7af1493d4e04fbf62333307cfc to your computer and use it in GitHub Desktop.
Save estan/011d8b7af1493d4e04fbf62333307cfc to your computer and use it in GitHub Desktop.
C++ version:
pixel at 0 0 is r = 255 g = 0 b = 0
pixel at 1 0 is r = 0 g = 255 b = 0
pixel at 0 1 is r = 0 g = 0 b = 255
pixel at 1 1 is r = 0 g = 0 b = 128
Python version:
pixel at 0,0 is r = 0, g = 0, b = 0
pixel at 1,0 is r = 0, g = 0, b = 0
pixel at 0,1 is r = 0, g = 0, b = 255
pixel at 1,1 is r = 0, g = 0, b = 0
#include <QImage>
#include <QColor>
#include <QDebug>
int main(int, char *[]) {
QImage image(2, 2, QImage::Format_RGB32);
image.setPixel(0, 0, QColor(Qt::red).rgb());
image.setPixel(1, 0, QColor(Qt::green).rgb());
image.setPixel(0, 1, QColor(Qt::blue).rgb());
image.setPixel(1, 1, QColor(Qt::darkBlue).rgb());
for (int y = 0; y < image.height(); ++y) {
const auto scanLine = (QRgb *)image.scanLine(y);
for (int x = 0; x < image.width(); ++x) {
const auto pixel = scanLine[x];
qDebug() << "pixel at" << x << y << "is"
<< "r =" << qRed(pixel)
<< "g =" << qGreen(pixel)
<< "b =" << qBlue(pixel);
}
}
return 0;
}
from PySide2.QtCore import Qt
from PySide2.QtGui import QImage, qRed, qGreen, qBlue, QColor
image = QImage(2, 2, QImage.Format_RGB32)
image.setPixel(0, 0, QColor(Qt.red).rgb())
image.setPixel(1, 0, QColor(Qt.green).rgb())
image.setPixel(0, 1, QColor(Qt.blue).rgb())
image.setPixel(1, 1, QColor(Qt.darkBlue).rgb())
for y in range(image.height()):
scanLine = image.scanLine(y)
for x in range(image.width()):
pixel = scanLine[x]
print('pixel at {},{} is r = {}, g = {}, b = {}'.format(
x, y, qRed(pixel), qGreen(pixel), qBlue(pixel)))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment