How do I display a PGM file within a QGraphicsView? - qt

I am trying to display a PGM file using the following test application. The code works with a PNG file, but not PGM:
import sys
from PyQt4.QtGui import *
from PyQt4.QtCore import *
import Image
import ImageQt
class MyView(QGraphicsView):
def __init__(self):
QGraphicsView.__init__(self)
self.scene = QGraphicsScene(self)
self.setScene(self.scene)
img = Image.open('test.pgm') # works with 'test.png'
self.imgQ = ImageQt.ImageQt(img)
pixmap = QPixmap.fromImage(self.imgQ)
self.scene.addPixmap(pixmap)
if __name__ == '__main__':
app = QApplication(sys.argv)
view = MyView()
view.show()
sys.exit(app.exec_())
How can I display a PGM file within the QGraphicsView?

QImage supports reading PGM files. Have you checked that img is loaded correctly?
Perhaps you have abad file, or a typo in the name, or wrong permissions

It looks like you're using PIL to open the image, convert to a QImage and then convert that to a QPixmap.
Is there any reason why you can't just load the PGM directly into a QPixmap and avoid PIL?
Replacing
img = Image.open('test.pgm') # works with 'test.png'
self.imgQ = ImageQt.ImageQt(img)
pixmap = QPixmap.fromImage(self.imgQ)
self.scene.addPixmap(pixmap)
with
pixmap = QPixmap('test.pgm')
self.scene.addPixmap(pixmap)
should work for you.

Related

Pyside6 shearing PDF file on window resize

I'm using QT (PySide) to view PDFs (using the PyMuPDF library) but when I resize I get a shearing artifact.
Like this:
Here is a minimal example:
import sys
import fitz
from PySide6.QtWidgets import QApplication, QLabel, QMainWindow, QScrollArea
from PySide6.QtGui import QPixmap, QImage
from PySide6.QtCore import Qt
class PDFWindow(QMainWindow):
def __init__(self, filename):
super().__init__()
# Open the PDF and get the first page
self.pdf_doc = fitz.open(filename)
self.page = self.pdf_doc[0]
# Create a QLabel to hold the rendered page
self.label = QLabel()
self.label.setAlignment(Qt.AlignCenter)
# Create a QScrollArea to hold the label
self.scroll_area = QScrollArea()
self.scroll_area.setWidget(self.label)
self.scroll_area.setWidgetResizable(True)
self.setCentralWidget(self.scroll_area)
# Render the page and set it as the pixmap for the label
self.render_page()
# Connect the windowResized signal to the render_page function
self.resizeEvent = self.render_page
def render_page(self, event=None):
zoom = 1
if event is not None:
bound = self.page.bound()
page_width, page_height = bound.width, bound.height
zoom = min(event.size().width() / page_width,
event.size().height() / page_height)
pixmap = self.page.get_pixmap(matrix=fitz.Matrix(zoom, zoom),
colorspace='rgb')
image = QImage(pixmap.samples, pixmap.width, pixmap.height,
QImage.Format_RGB888)
cur_pixmap= QPixmap.fromImage(image)
self.label.setPixmap(cur_pixmap)
if __name__ == "__main__":
app = QApplication(sys.argv)
window = PDFWindow("example.pdf")
window.show()
sys.exit(app.exec())

Current Screen Size in Python3 with PyQt5

Is there an alternative in Qt5 python3 to the following code :
https://askubuntu.com/questions/153549/how-to-detect-a-computers-physical-screen-size-in-gtk
from gi.repository import Gdk
s = Gdk.Screen.get_default()
print(s.get_width(), s.get_height())
You can get the primary screen from the QApplication, which returns a QScreen object giving access to many useful properties:
import sys
from PyQt5 import QtWidgets
app = QtWidgets.QApplication(sys.argv)
screen = app.primaryScreen()
print('Screen: %s' % screen.name())
size = screen.size()
print('Size: %d x %d' % (size.width(), size.height()))
rect = screen.availableGeometry()
print('Available: %d x %d' % (rect.width(), rect.height()))
Note that the primaryScreen method is static, so if you've already created an instance of QApplication elsewhere in your application, can easily get a QScreen object later on like this:
screen = QApplication.primaryScreen()
The following python3 code allows to get screen size but is there an alternative to
QtWidgets.QDesktopWidget().screenGeometry(-1) method :
import sys
from PyQt5 import QtWidgets
def main():
"""
allow you to get size of your current screen
-1 is to precise that it is the current screen
"""
sizeObject = QtWidgets.QDesktopWidget().screenGeometry(-1)
print(" Screen size : " + str(sizeObject.height()) + "x" + str(sizeObject.width()))
if __name__ == '__main__':
app = QtWidgets.QApplication(sys.argv)
main()
sys.exit(app.exec_())
The value for screenGeometry() is the display you take a look at.
0 is the main screen, 1, 2 indicates further monitors installed.
To list all availible displays:
def screen_resolutions():
for displayNr in range(QtWidgets.QDesktopWidget().screenCount()):
sizeObject = QtWidgets.QDesktopWidget().screenGeometry(displayNr)
print("Display: " + str(displayNr) + " Screen size : " + str(sizeObject.height()) + "x" + str(sizeObject.width()))
from PyQt5.QtWidgets import QApplication, QWidget
self.desktop = QApplication.desktop()
self.screenRect = self.desktop.screenGeometry()
self.height = self.screenRect.height()
self.width = self.screenRect.width()
You can see here - https://www.programmersought.com/article/58831562998/
Screen information is available from the QApplication object as noted by ekhumoro. However, there should be only one global instance of QApplication. It is often set in the main script of a PyQt Gui application:
import sys
from PyQt5.QtWidgets import QApplication
def main():
...
app = QApplication(sys.argv)
window = MainWindow()
window.show
sys.exit(app.exec_())
That's probably not where you want to work with screen properties.
As pointed out by Omkar76, to access the global instance from elsewhere (e.g. in MainWindow()) use its instance() function. You can then use its primaryScreen property to get access to the many useful pieces of information available from the QScreen object:
from PyQt5.QtWidgets import QApplication, QMainWindow
class MainWindow(QMainWindow):
def __init__(self):
# Window dimensions
app = QApplication.instance()
screen = app.primaryScreen()
geometry = screen.availableGeometry()
self.setFixedSize(geometry.width() * 0.8, geometry.height() * 0.7)
QDesktopWidget is obsolete and so should be avoided.
i honestly don't know why it's always such a hustle to find pyqt5 answers,but anyway i think this is what you were looking for .
print(self.frameSize())

QMovie setting filename second time wont work

I am setting the Initial Movie File like this:
self.movie = QMovie("dotGreenBlack.gif", QByteArray(), self)
At runtime, i want to change it by setting:
self.movie.setFileName("dotGreyStatic.gif")
But this leads to frame-freeze of the primarly set file "dotGreenBlack.gif"
Anything else i have to do to change the Gif-Animation on runtime?
Here is the fullcode:
from PyQt4 import QtCore, QtGui
from PyQt4.QtGui import *
from PyQt4.QtCore import *
import sys
class ImagePlayer(QWidget):
def __init__(self, filename, title, parent=None):
QWidget.__init__(self, parent)
# Load the file into a QMovie
self.movie = QMovie(filename, QByteArray(), self)
size = self.movie.scaledSize()
self.setGeometry(200, 200, size.width(), size.height())
self.setWindowTitle(title)
self.movie_screen = QLabel()
# Make label fit the gif
self.movie_screen.setSizePolicy(QSizePolicy.Expanding, QSizePolicy.Expanding)
self.movie_screen.setAlignment(Qt.AlignCenter)
# Create the layout
main_layout = QVBoxLayout()
main_layout.addWidget(self.movie_screen)
self.setLayout(main_layout)
# Add the QMovie object to the label
self.movie.setCacheMode(QMovie.CacheAll)
self.movie.setSpeed(100)
self.movie_screen.setMovie(self.movie)
self.movie.start()
self.movie.loopCount()
self.movie.setFileName("dotGreyStatic.gif")
self.movie_screen.setMovie(self.movie)
if __name__ == "__main__":
gif = "dotGreenBlack.gif"
app = QApplication(sys.argv)
app.setStyleSheet("QWidget { background-color: black }")
player = ImagePlayer(gif, "was")
player.show()
sys.exit(app.exec_())
Gif Icons used in this Example:
You just need to stop and restart the movie:
self.movie.stop()
self.movie.setFileName("dotGreyStatic.gif")
self.movie.start()

Qt visualization as a function in separate file

I'm newbie in PyQtGraph and Qt. I need to write a function (preferably in separate file) for visualization my data. The short version of my snippet is below. The function and the main code in the same file here:
import numpy as np
from pyqtgraph.Qt import QtGui
import pyqtgraph as pg
def visualizator(image):
app = QtGui.QApplication([])
win = QtGui.QMainWindow()
w = QtGui.QWidget()
win.setCentralWidget(w)
layout = QtGui.QGridLayout()
w.setLayout(layout)
imgView = pg.ImageView()
layout.addWidget(imgView)
imgView.setImage(image)
win.show()
app.exec_()
return
my_image = np.ones((256,256),np.float)
visualizator(my_image)
When I run my code I get Qt crash. What am I doing wrong? What should I return from my function?
You have to be careful with your QApplications. You don't want to start another one in a function. Try removing the app-stuff.

pyqt4 - singleapplication - bring up the original window in an attempt to open the app for the second time

I want only one instance of my app to be running at each time. but when the user attempts to open it the second time, I want the first window to be brought to the front (it could be just minimized or minimized to the corner of the taskbar and the user doesn't know how to open it)
I have this code that does the detection job and it doesn't allow the second instance. I have trouble with the part that it has to open the original window. I have commented out some of my attempt.
import sys
from PyQt4 import QtGui, QtCore
import sys
class SingleApplication(QtGui.QApplication):
def __init__(self, argv, key):
QtGui.QApplication.__init__(self, argv)
self._activationWindow=None
self._memory = QtCore.QSharedMemory(self)
self._memory.setKey(key)
if self._memory.attach():
self._running = True
else:
self._running = False
if not self._memory.create(1):
raise RuntimeError(
self._memory.errorString().toLocal8Bit().data())
def isRunning(self):
return self._running
def activationWindow(self):
return self._activationWindow
def setActivationWindow(self, activationWindow):
self._activationWindow = activationWindow
def activateWindow(self):
if not self._activationWindow:
return
self._activationWindow.setWindowState(
self._activationWindow.windowState() & ~QtCore.Qt.WindowMinimized)
self._activationWindow.raise_()
self._activationWindow.activateWindow()
class Window(QtGui.QWidget):
def __init__(self):
QtGui.QWidget.__init__(self)
self.label = QtGui.QLabel(self)
self.label.setText("Hello")
layout = QtGui.QVBoxLayout(self)
layout.addWidget(self.label)
if __name__ == '__main__':
key = 'random _ text'
app = SingleApplication(sys.argv, key)
if app.isRunning():
#app.activateWindow()
sys.exit(1)
window = Window()
#app.setActivationWindow(window)
#print app.topLevelWidgets()[0].winId()
window.show()
sys.exit(app.exec_())
I've made this work on Windows using the win32 api (I'm not entirely sure, but there are probably equivalent calls on macos/unix).
Add the following import to your application,
import win32gui
set the window title to a fixed name (instead of doing this, you could store its whndl in the shared memory)
window = Window()
window.setWindowTitle('Single Application Example')
window.show()
and then change your activateWindow method to something like the following:
def activateWindow(self):
# needs to look for a named window ...
whndl = win32gui.FindWindowEx(0, 0, None, "Single Application Example")
if whndl is 0:
return #couldn't find the name window ...
#this requests the window to come to the foreground
win32gui.SetForegroundWindow(whndl)
You might be interested by the solutions proposed here
For instance, I would try:
app = SingleApplication(sys.argv, key)
if app.isRunning():
window = app.activationWindow()
window.showNormal()
window.raise()
app.activateWindow()
sys.exit(1)
window = Window()
app.setActivationWindow(window)
window.setWindowFlags(Popup)
window.show()

Resources