OpenCV 3: Why does the UI render incorrectly? - python-3.x

I am a new to openCV and trying its examples from documentation. I am doing the trackbar as the color palette example.
But my UI renders in wrong order as well as crops the switch title: OFF.
where it is supposed to be like this.
I am using macbook pro with macOS 10.13.

OpenCV's Highgui module uses QT as a backend. QT renders elements differently depending on the OS that is using it. I believe that this is because QT uses UI elements that are native to the OS. So a frame in Windows will look like a Windows Frame, a frame in Linux will look like a Linux frame.
This is what the same example produces in Windows:
You can get the slider bars to fit the window by changing:
cv2.namedWindow('image')
to:
cv2.namedWindow('image',cv2.WND_PROP_AUTOSIZE)
Doing that produces this output (in Windows):

Related

Why fonts in Qt are appearing blurry or pixelated?

All my fonts are appearing pixelated, so I used AntiAliasing but it isn't helping out. As you can see the pixelated font in the image itself:
This is the code I am currently using:
butt1 = QtWidgets.QLabel("""Scrappr""")
font = QtGui.QFont()
font.setStyleStrategy(QtGui.QFont.PreferAntialias)
font.setPixelSize(22)
font.setFamily('Segoe UI Bold')
butt1.setFont(QtGui.QFont(font))
I tried different solutions on SO, qtforums etc but nothing works for me :(
I tried:
Different combinations of ClearType text but It didn't work out as, by default all the text appears good on windows and chrome but with Qt only, it becomes pixelated.
Changing windows aero theme to classic one...
But none of them helped.
Here are My PC Specs:
windows: 7 ultimate
PySide2 version: 5.14.2.1
Resolution: 1360 X 768
I'm using BrownPro font and the texts were blurry at all resolutions, but much more evident at low resolutions.
I was able to solve the issue by setting the hinting preference for the font to: PreferNoHinting. Applying it at the application level, fixes the issue everywhere.
Here is the documentation: https://doc.qt.io/qt-5/qfont.html#HintingPreference-enum
And here is the code I used:
QFontDatabase::addApplicationFont(":/fonts/BrownPro-Bold.ttf");
QFontDatabase::addApplicationFont(":/fonts/BrownPro-Regular.ttf");
QFontDatabase::addApplicationFont(":/fonts/BrownPro-Light.ttf");
QFont brown_pro_font("BrownPro");
brown_pro_font.setHintingPreference(QFont::HintingPreference::PreferNoHinting); //This line did the trick
QApplication::setFont(brown_pro_font);
Try to see the fonts used by PyQt5:
import PyQt5
from pyQt5 import QtGui
dir(QtGui.QFont)
the result will show all you need for QFond and the fonts can be used:
[..., 'Helvetica',...,'SansSerif',..., 'Serif',..., 'Times', ...
You can try to add your custom fonts but you need to test each font.
For example, the documentation tells us:
In Windows a request for the “Courier” font is automatically changed to “Courier New”, an improved version of Courier that allows for smooth scaling. The older “Courier” bitmap font can be selected by setting the PreferBitmap style strategy (see setStyleStrategy() ).
Once a font is found, the remaining attributes are matched in order of priority:
fixedPitch()
pointSize() (see below)
weight()
style()
I happen to work with Qt last year and i used qml for building the UI part of my application.
Qt itself prefers us to use qml for building UI, since they have written a UI engine that renders everything better compared to the old engines.
In case of PyQt you are using the python only approach which is only not usually recommended, i am not saying that the qml version is pixel perfect. it still works bad at drawing curves (but that is not the stuff we usually require). As far as your problem is concerned qml will work fine for you (it has much better text rendering).
You might struggle a bit finding the learning resource for qml. But at least give it a shot and yes it is easier much easier than Python only approach.

How to draw an reactangle with some text using python

I want to draw in an image using python3. I have implemented same thing in android application using java. Looking for something to be done on the web application. You can see at the bottom of the image where I have used paint in android.
My image is :
Sample Image
I am not getting any library or a code snippet to do so in python. Any suggestions will be a great help. Thanks
Pillow package provides the tools you need to add watermarks to your photos!
How to Watermark Your Photos with Python

How do you create a perfect pygame fullscreen?

I am making a game, and I want it to be fullscreen. However, the pygame fullscreen is strange, creating a screen too large. So I referred to this: Pygame FULLSCREEN Display Flag Creates A Game Screen That Is Too Large For The Screen. However, when I followed these instructions
import ctypes
ctypes.windll.user32.SetProcessDPIAware()
true_res = (ctypes.windll.user32.GetSystemMetrics(0), ctypes.windll.user32.GetSystemMetrics(1))
pygame.display.set_mode(true_res,pygame.FULLSCREEN)
from an answer (but instead using pywin32 instead of ctypes, like this: win32api.GetSystemMetric(0)).
I used this, and while it does create a fullscreen, it also creates a black border around my screen and enlarges everything a slight bit, including my cursor. How can I get rid of this black border and get all shapes to normal size? Or is there a better way to create a good fullscreen?
If it helps, I use Windows 10.
Thanks in advance!
I think the problem of enlarging everything arose with the use of ctypes module as because the ctypes module makes use of a function named as GetSystemMetrics() whose work is to get the size of the screen of your system.
And might be the import pygame is loading some dll that is not compatible with a dll that windll needs.
So I suggest either you update the ctype library or pygame library or update both libraries or you can enlarge screen size by providing custom width and height values according to the resolution supported by your system.
Hope this helps !!

ClientDC and WindowDC do not draw under Fedora 16 Gnome 3, only PaintDC

I am writing an app which draws text and shapes in a ClientDC of a Frame. When I run the app under my Fedora 16(Gnome 3) nothing is drawn in the Frame, but if I run it under Windows all drawings display normally.
I've tried using WindowDC to do the drawing on, but it is not different to ClientDC under Fedora. I can only get a successful drawing when using PaintDC.
Am I doing something wrong(or missing something), or is it just Linux/Fedora/Gnome 3?
I think the recommended way to paint is with the newer wx.GCDC with a fallback to the wx.PaintDC. If you need advanced drawing, see FloatCanvas, cairo or the glcanvas.

JFree Chart with itext problem with font

I want to use Jfree chart with itext to make a pdf. I have used custom font for JFree chart title and its other components using java.awt.Font class. It works ok in windows but on linux centOS, the title is overlapping with upper line and so do other things. Need help.
Thanks
Found the answer. It's because the two platforms are using different version of java. On Windows its java (SE) 1.6 and on linux I was using openjdk. By making both java SE, problem's solved.

Resources