pyqt4 - Why does QPainter seem to scale fonts unpredictably when using drawText on a QImage? -


i'm loading 2 separate images, 1 png 1 jpg, separate qimages using pyqt4 (linux, windows, doesn't matter). images both 640x480.

self.image1 = qtgui.qimage('image1.png'); self.image2 = qtgui.qimage('image2.jpg'); 

when drawtext onto each (again separately) following code, resulting text vastly different in size , placement.

def imageoverlay( self, img, text):      #img qimage     #imageoverlay called many times font creation done once.      if self.font none:         self.font = qtgui.qfont('sans',16)         metrics = qtgui.qfontmetrics(self.font)         self.fontheight = metrics.ascent();      painter = qtgui.qpainter()     painter.begin(img)     painter.setopacity(1.0)     painter.setpen(qtcore.qt.black)     painter.setfont(self.font)     painter.drawtext( 1,self.fontheight, text )     painter.end() 

each image, modified imageoverlay() call, loaded qlabel , displayed in simple grid layout.

the rendering different:

enter image description here

however, if re-set font-size pixel (as opposed points), appears work expect:

    self.font = qtgui.qfont('sans', 16)     self.font.setpixelsize(16) 

enter image description here

i suspect has attribute of qimage suggesting type of context render text, have not been able find definitive reason why happening.

can shed light on why happens or, if possible, point me documentation describes behavior?

qtgui.qfontmetrics(self.font) (with font not created specific paint device) creates font metric suitable display resolution, means uses dpi value of display when converting between sizes in points , pixels.

when you're drawing text on qpaintdevice (in case qimage), painter uses font metric based on paint devices' resolution. can access metric using painters' fontmetrics() property, or using qfontmetrics(font, paintdevice) constructor.

if compare 2 images' image.dotspermeterx() , image.dotspermetery() values (or open them in image editor , check resolution there), i'm sure you'll see different values, meaning image resoltion metadata different, that's why rendered text size different.

so if want text size same in pixels, stick font.setpixelsize(), if you're ok text size being set relative images' dpi, use font size in points use painter.fontmetrics().ascent() calculate text baseline.


Comments

Popular posts from this blog

get url and add instance to a model with prefilled foreign key :django admin -

css - Make div keyboard-scrollable in jQuery Mobile? -

ruby on rails - Seeing duplicate requests handled with Unicorn -