如何在 PyQt canvas 中插入 PIL.Image - PyQt5
How to insert a PIL.Image in a PyQt canvas - PyQt5
我想在应用程序的仪表中显示一些数据。我正在使用 pyqt5.
我正在创建一个 canvas
,其中将显示我的图表或我的仪表(有时是图表,有时是仪表):
class MplCanvas(FigureCanvasQTAgg):
def __init__(self, parent=None, width=8, height=6, dpi=100):
fig = Figure(figsize=(width, height), dpi=dpi)
self.axes = fig.add_subplot(111)
super(MplCanvas, self).__init__(fig)
并在我的主布局中添加此 canvas
:
class MainWindow(QtWidgets.QMainWindow):
def __init__(self, *args, **kwargs):
super(MainWindow, self).__init__(*args, **kwargs)
self.canvas = MplCanvas(self, width=12, height=8, dpi=100)
self.layout_plot.addWidget(self.canvas)
self.show()
我在 how to create a gauge 上找到了一个 link:
import PIL
from PIL import Image
percent = 20 # Percent for gauge
output_file_name = 'new_gauge.png'
percent = percent / 100
rotation = 180 * percent # 180 degrees because the gauge is half a circle
rotation = 90 - rotation # Factor in the needle graphic pointing to 50 (90 degrees)
dial = Image.open('needle.png')
dial = dial.rotate(rotation, resample=PIL.Image.BICUBIC, center=loc) # Rotate needle
gauge = Image.open('gauge.png')
gauge.paste(dial, mask=dial) # Paste needle onto gauge
gauge.save(output_file_name)
我尝试以这种方式将 gauge
添加到我的 `canvas 中:
dial = Image.open('needle.png')
dial = dial.rotate(rotation, resample=PIL.Image.BICUBIC, center=loc) # Rotate needle
gauge = Image.open('gauge.png')
gauge.paste(dial, mask=dial) # Paste needle onto gauge
self.layout_plot.removeWidget(self.canvas)
self.layout_plot.addWidget(gauge)
self.canvas.draw()
我收到这个错误:
TypeError: addWidget(self, QWidget, stretch: int = 0, alignment: Union[Qt.Alignment, Qt.AlignmentFlag] = Qt.Alignment()): argument 1 has unexpected type 'PngImageFile'
如何在我的 canvas
中添加这个 gauge
?
您的问题令人困惑,因为如果分析您指出的内容,则可以解释为:
如何在还添加了 canvas 的布局中添加 PIL.image。如果是这样,那么问题是 addWidget 方法需要一个 QWidget,所以你必须使用像 QLabel 这样的 QWidget 将图像放在那里,然后将 QLabel 放在布局中:
from PIL.ImageQt import ImageQt
im = ImageQt(gauge).copy()
pixmap = QtGui.QPixmap.fromImage(im)
label = QtWidgets.QLabel()
label.setPixmap(pixmap)
self.layout_plot.addWidget(label)
如何在canvas里面添加PIL.Image,那样的话你不应该使用布局,而是imshow方法:
self.canvas.axes.imshow(np.asarray(gauge))
我想在应用程序的仪表中显示一些数据。我正在使用 pyqt5.
我正在创建一个 canvas
,其中将显示我的图表或我的仪表(有时是图表,有时是仪表):
class MplCanvas(FigureCanvasQTAgg):
def __init__(self, parent=None, width=8, height=6, dpi=100):
fig = Figure(figsize=(width, height), dpi=dpi)
self.axes = fig.add_subplot(111)
super(MplCanvas, self).__init__(fig)
并在我的主布局中添加此 canvas
:
class MainWindow(QtWidgets.QMainWindow):
def __init__(self, *args, **kwargs):
super(MainWindow, self).__init__(*args, **kwargs)
self.canvas = MplCanvas(self, width=12, height=8, dpi=100)
self.layout_plot.addWidget(self.canvas)
self.show()
我在 how to create a gauge 上找到了一个 link:
import PIL
from PIL import Image
percent = 20 # Percent for gauge
output_file_name = 'new_gauge.png'
percent = percent / 100
rotation = 180 * percent # 180 degrees because the gauge is half a circle
rotation = 90 - rotation # Factor in the needle graphic pointing to 50 (90 degrees)
dial = Image.open('needle.png')
dial = dial.rotate(rotation, resample=PIL.Image.BICUBIC, center=loc) # Rotate needle
gauge = Image.open('gauge.png')
gauge.paste(dial, mask=dial) # Paste needle onto gauge
gauge.save(output_file_name)
我尝试以这种方式将 gauge
添加到我的 `canvas 中:
dial = Image.open('needle.png')
dial = dial.rotate(rotation, resample=PIL.Image.BICUBIC, center=loc) # Rotate needle
gauge = Image.open('gauge.png')
gauge.paste(dial, mask=dial) # Paste needle onto gauge
self.layout_plot.removeWidget(self.canvas)
self.layout_plot.addWidget(gauge)
self.canvas.draw()
我收到这个错误:
TypeError: addWidget(self, QWidget, stretch: int = 0, alignment: Union[Qt.Alignment, Qt.AlignmentFlag] = Qt.Alignment()): argument 1 has unexpected type 'PngImageFile'
如何在我的 canvas
中添加这个 gauge
?
您的问题令人困惑,因为如果分析您指出的内容,则可以解释为:
如何在还添加了 canvas 的布局中添加 PIL.image。如果是这样,那么问题是 addWidget 方法需要一个 QWidget,所以你必须使用像 QLabel 这样的 QWidget 将图像放在那里,然后将 QLabel 放在布局中:
from PIL.ImageQt import ImageQt
im = ImageQt(gauge).copy() pixmap = QtGui.QPixmap.fromImage(im) label = QtWidgets.QLabel() label.setPixmap(pixmap) self.layout_plot.addWidget(label)
如何在canvas里面添加PIL.Image,那样的话你不应该使用布局,而是imshow方法:
self.canvas.axes.imshow(np.asarray(gauge))