How to save Qimage using jpeg format in PyQt?

I platform Windows7

, Python3.4

, PyQt5

.

I found that I cannot save the object QImage

in jpeg

.

>>> from PyQt5 import QtCore, QtGui
>>> i = QtGui.QImage()
>>> i.load(r"C:\Users\paleneutron\Pictures\Capture4.PNG")
True
>>> i.save(r"C:\Users\paleneutron\Pictures\hehe.jpg")
False
>>> i.save(r"C:\Users\paleneutron\Pictures\hehe.jpg",format = 'jpeg')
False
>>> i.save('hehe.png')
True
>>> i.save('hehe.bmp')
True
>>> i.save('hehe.jpg')
False
>>> i.save('hehe.jpeg')
False

      

In this page , jpeg

it supports both reading and writing.

Why was I wrong about this?

EDIT

I checked the supported formats as a comment:

>>> QtGui.QImageWriter.supportedImageFormats()
[PyQt5.QtCore.QByteArray(b'bmp'), PyQt5.QtCore.QByteArray(b'pbm'), PyQt5.QtCore.QByteArray(b'pgm'), PyQt5.QtCore.QByteArray(b'png'), PyQt5.QtCore.QByteArray(b'ppm'), PyQt5.QtCore.QByteArray(b'xbm'), PyQt5.QtCore.QByteArray(b'xpm')]

      

The problem here is jpeg

missing!

But I have qjpeg.dll

to C:\Python34\Lib\site-packages\PyQt5\plugins\imageformats

. What should I do to include jpeg

in my program?

+3


source to share


1 answer


Thank you, Chernobyl!

I got the solution by accident.

I tried the test code in my complete program and it works well. This is because many functions PyQt

must be used QGuiApplication

before using it.



from PyQt5 import QtCore, QtGui, QtWidgets
import sys
app = QtWidgets.QApplication(sys.argv)
print(QtGui.QImageWriter.supportedImageFormats())

      

We now have fully supported formats:

[PyQt5.QtCore.QByteArray(b'bmp'), PyQt5.QtCore.QByteArray(b'ico'), PyQt5.QtCore.QByteArray(b'jpeg'), PyQt5.QtCore.QByteArray(b'jpg'), PyQt5.QtCore.QByteArray(b'pbm'), PyQt5.QtCore.QByteArray(b'pgm'), PyQt5.QtCore.QByteArray(b'png'), PyQt5.QtCore.QByteArray(b'ppm'), PyQt5.QtCore.QByteArray(b'tif'), PyQt5.QtCore.QByteArray(b'tiff'), PyQt5.QtCore.QByteArray(b'wbmp'), PyQt5.QtCore.QByteArray(b'xbm'), PyQt5.QtCore.QByteArray(b'xpm')]

      

+4


source







All Articles