PDA

View Full Version : PyQt Indexed colour images, non-blured font?



morph
10th September 2015, 00:44
Hi and welcome :)

I want to generate a indexed colour bitmap (up to 6 colours) image and write some text on it.
The text shouldn't have any kind of blur, just pure one-coloured pixels (the text is going to be tiny, every pixel matter ;) )

I used the standard:

pixmap = QPixmap(rect.width(), rect.height())
pixmap.fill(Qt.white)

painter = QPainter()
painter.begin(pixmap)
painter.setFont(font)
painter.rotate(lineAngle)
painter.drawText(linePosX, linePosY, text)

The main problem is that when I save the text is a bit blurred.
11365
The second thing is: how to limit the colour number in and image?

Thank you for your help.

anda_skoa
10th September 2015, 09:11
I think you want to use a QImage instead of a QPixmap.
The image allows you to specify the format, e.g. color indexed data, and a color map, the pixmap is always the color resolution of the display.

For the text blurriness, check that you have antialiasing disabled.

Cheers,
_

morph
24th September 2015, 03:06
Looks like the antialiasing flag worked, thanks!

The only problem I'm left with right now is how to add a 1px shadow to the text while using drawText.
Can anybody suggest an option for this?

BTW Do I have to use QImage instead of QPixmap to control the image parameters like colour number?

anda_skoa
24th September 2015, 10:37
The only problem I'm left with right now is how to add a 1px shadow to the text while using drawText.
Can anybody suggest an option for this?

Maybe first draw the text with 1px offset and the shadow color and then draw the text at the actual position.



BTW Do I have to use QImage instead of QPixmap to control the image parameters like colour number?

That's what I said, isn't it?

Cheers,
_

morph
2nd October 2015, 02:59
Well I do need to convert the image to pixmap to use it in QLabel and draw text on it after opening it

Right now the last problem I'm left with is that when I try to convert back to image before saving I end up either with a clean but 4 bit colour depth image:
11411
or a 'damaged' 8 bit image:
11410

I'm converting by using:

# this gives me an 8 bit image with 'noise'
image = self.label.pixmap().toImage()
image = image.convertToFormat(QImage.Format_Indexed8, Qt.PreferDither) #produces 4 bit depth without PreferDither

I've also tried using the colour map parameter while converting (values taken from original image) but this makes the image drop to 4 bit colour depth :/

Does anybody know how to solve this?

EDIT:
OR redefining the questions, if it's an easier option:
I can't write on an 8 bit QImage, so I have to convert it to some other format.
How can I convert it, and to which format, and how should I convert it back to 8 bit after writing on it?

anda_skoa
2nd October 2015, 11:06
OR redefining the questions, if it's an easier option:
I can't write on an 8 bit QImage, so I have to convert it to some other format.

You mean writing fails when the QImage has a color table?
Or that what you write into it does not look what you want?

Cheers,
_

morph
2nd October 2015, 11:40
according to docs:
http://doc.qt.io/qt-5/qimage.html


Note: Drawing into a QImage with QImage::Format_Indexed8 is not supported.


QPainter which I use to draw text fails when I give him an Format_Indexed8 image

So my alternative idea was: convert the input 8 bit image to something which I can write on, then write on it :), and then convert back.
The problem is that by doing this I can't get the colours right in the output image :(

anda_skoa
2nd October 2015, 12:03
Hmm.

Maybe this would work:
- draw text into a Mono image or QBitmap with the same size as the image
- then iterate of that image and and the same time over the output image and set a pixel to the font color when there is a set pixel in the mono color image

Cheers,
_

morph
2nd October 2015, 14:22
Hmm.
Maybe this would work:
- draw text into a Mono image or QBitmap with the same size as the image
- then iterate of that image and and the same time over the output image and set a pixel to the font color when there is a set pixel in the mono color image


I'm not sure I know how to do this, can you provide a proof-of-concept code for this?

Or just a solution to open a 8 bit BMP file, write text on it and save, also as an 8 bit bmp file without losing colours information