Results 1 to 9 of 9

Thread: PyQt Indexed colour images, non-blured font?

  1. #1
    Join Date
    Sep 2015
    Posts
    6

    Default PyQt Indexed colour images, non-blured font?

    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:
    Qt Code:
    1. pixmap = QPixmap(rect.width(), rect.height())
    2. pixmap.fill(Qt.white)
    3.  
    4. painter = QPainter()
    5. painter.begin(pixmap)
    6. painter.setFont(font)
    7. painter.rotate(lineAngle)
    8. painter.drawText(linePosX, linePosY, text)
    To copy to clipboard, switch view to plain text mode 

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

    Thank you for your help.

  2. #2
    Join Date
    Jan 2006
    Location
    Graz, Austria
    Posts
    8,416
    Thanks
    37
    Thanked 1,544 Times in 1,494 Posts
    Qt products
    Qt3 Qt4 Qt5
    Platforms
    Unix/X11 Windows

    Default Re: PyQt Indexed colour images, non-blured font?

    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,
    _

  3. #3
    Join Date
    Sep 2015
    Posts
    6

    Default Re: PyQt Indexed colour images, non-blured font?

    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?

  4. #4
    Join Date
    Jan 2006
    Location
    Graz, Austria
    Posts
    8,416
    Thanks
    37
    Thanked 1,544 Times in 1,494 Posts
    Qt products
    Qt3 Qt4 Qt5
    Platforms
    Unix/X11 Windows

    Default Re: PyQt Indexed colour images, non-blured font?

    Quote Originally Posted by morph View Post
    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.

    Quote Originally Posted by morph View Post
    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,
    _

  5. #5
    Join Date
    Sep 2015
    Posts
    6

    Default Re: PyQt Indexed colour images, non-blured font?

    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:
    bad_bmp.jpg
    or a 'damaged' 8 bit image:
    good_bmp.jpg

    I'm converting by using:
    Qt Code:
    1. # this gives me an 8 bit image with 'noise'
    2. image = self.label.pixmap().toImage()
    3. image = image.convertToFormat(QImage.Format_Indexed8, Qt.PreferDither) #produces 4 bit depth without PreferDither
    To copy to clipboard, switch view to plain text mode 

    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?
    Last edited by morph; 2nd October 2015 at 04:01.

  6. #6
    Join Date
    Jan 2006
    Location
    Graz, Austria
    Posts
    8,416
    Thanks
    37
    Thanked 1,544 Times in 1,494 Posts
    Qt products
    Qt3 Qt4 Qt5
    Platforms
    Unix/X11 Windows

    Default Re: PyQt Indexed colour images, non-blured font?

    Quote Originally Posted by morph View Post
    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,
    _

  7. #7
    Join Date
    Sep 2015
    Posts
    6

    Default Re: PyQt Indexed colour images, non-blured font?

    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

  8. #8
    Join Date
    Jan 2006
    Location
    Graz, Austria
    Posts
    8,416
    Thanks
    37
    Thanked 1,544 Times in 1,494 Posts
    Qt products
    Qt3 Qt4 Qt5
    Platforms
    Unix/X11 Windows

    Default Re: PyQt Indexed colour images, non-blured font?

    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,
    _

  9. #9
    Join Date
    Sep 2015
    Posts
    6

    Default Re: PyQt Indexed colour images, non-blured font?

    Quote Originally Posted by anda_skoa View Post
    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

Similar Threads

  1. how to select a particular indexed table row
    By gauravg in forum Qt Programming
    Replies: 1
    Last Post: 19th December 2011, 10:32
  2. pyQt with interactive SVG images
    By JeffMGreg in forum Qt Programming
    Replies: 0
    Last Post: 4th November 2011, 22:30
  3. QGraphicsScene addText() - Set font colour?
    By Gavin Harper in forum Qt Programming
    Replies: 2
    Last Post: 15th April 2011, 12:00
  4. Replies: 2
    Last Post: 1st April 2011, 08:31
  5. Replies: 0
    Last Post: 1st April 2011, 05:47

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
Digia, Qt and their respective logos are trademarks of Digia Plc in Finland and/or other countries worldwide.