Generating textual images using QImage (qt)
Asked Answered
T

2

5

I am trying to create images from text using QImage.

As per the documentation here: http://doc.qt.io/qt-5/qimage.html#Format-enum

We cannot use QImage::Format_Indexed8 with QImage. I cannot use QImage::Format_Mono or QImage::Format_MonoLSB due to its low quality.

My question is:

  • What is the best way to create textual image (batch processing) so that we can get decent quality with minimum file size?
  • Is there is any way to do image compression through QT once the image is created to reduce the file size?
Tallyman answered 24/9, 2011 at 10:32 Comment(0)
O
8

Here is a sample code that does it:

QImage image(100, 50, QImage::Format_ARGB32_Premultiplied);
QPainter painter(&image);
painter.fillRect(image.rect(), Qt::yellow);
painter.drawText(image.rect(), Qt::AlignCenter | Qt::AlignVCenter, "hello, world");
image.save("output.png");

It creates this image:

enter image description here

The output format is PNG, so it will have good compression without losing any quality.

Oliver answered 27/10, 2015 at 7:17 Comment(0)
T
1

There's this example, which shows you how to use QPainter::drawText and work with fonts:

http://doc.qt.io/archives/qt-4.7/painting-fontsampler.html

QImage::save has support for a variety of formats and quality levels:

http://doc.qt.io/archives/qt-4.7/qimage.html#reading-and-writing-image-files

Although QImage is in QtCore, QPainter and the text drawing routines are in QtGUI. So on a Linux system this will require an X server to be running:

http://www.qtcentre.org/threads/1758-QPainter-in-console-apps

Thinner answered 24/9, 2011 at 10:48 Comment(1)
QtGui does not need X server. It depends on how it was built. If built for Qt/X11 yes, but not for Qt/E.Pottle

© 2022 - 2024 — McMap. All rights reserved.