Search code examples
qtqpixmap

Qt Qpixmap generates a blurry image


As part of a larger project i'm trying to draw basic shapes on a QPixmap but the generated image always appears blurry ie. the circle is not a pure red circle, the edges are blurred and the colour is not true red as expected. This becomes an issue later on with what I need the image for. Here's the relevant code snippet, i've played around with antialiasing options and render hints but i've not had any luck. Any help would be much appreciated!

QPixmap pixmap(QSize(300,300));
QPainter painter( &pixmap);

painter.setRenderHint(QPainter::Antialiasing);
painter.setRenderHint(QPainter::SmoothPixmapTransform);

painter.fillRect( QRectF(0, 0, 300, 300),Qt::white);

painter.setPen(Qt::red);
painter.drawEllipse( QPoint(150,150), 75, 75);

painter.setPen(Qt::green);
QLineF line(30, 30, 30, 270);
painter.drawLine(line);

pixmap.save("test1.jpg", "jpg", 100);

I've been asked to include an image to show the issue, here's a close up of the image which shows the green line not being true green and the red circle not being true red and with blurred edges.


Solution

  • The problem that you have with the red color is that Qt by default uses chroma color subsampling (2x2 blocks) when saving to JPG format. The only way to avoid this problem is using another format. Saving the same image with PNG format returns a pure red circle (255,0,0).

    Regarding to the blurry circle, it occurs because of QPainter::Antialiasing RenderHint. In your case, not using any renderHints shows a sharp image. Below you can see your image with pure red color (PNG format) and sharp edges (not RenderHints). Take into account that the blurriest approach will be using QPainter::Antialiasing so avoid it if you are aiming to image sharpeness. Depending on your needs you could try QPainter::HighQualityAntialiasing RenderHint, but nothing else.