Matplotlib 将文件保存为 JPEG 编码器错误 [英] Matplotlib save file as JPEG encoder error

查看:82
本文介绍了Matplotlib 将文件保存为 JPEG 编码器错误的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试将 matplotlib 图形另存为 JPEG 文件,但出现以下错误.我不确定它是否与我的安装或我的代码有关.因此,我还附上了一个示例程序来执行,该程序会产生以下错误.我将图形保存为 .png 或 .pdf 没有问题,只有 .jpg 有问题

I am trying to save a matplotlib figure as a JPEG file and am getting the following error. I'm not sure if it has to do with my install or with my code. therefore I have as well attached a sample program to perform that produces the error below. I have no issue with saving the figure as a .png or.pdf only an issue with .jpg

Error:
Traceback (most recent call last):
  File "simpleissue.py", line 36, in updateplot
    self.figure.savefig(savename)
  File "/usr/local/lib/python3.5/dist-packages/matplotlib/figure.py", line 1814, in savefig
    self.canvas.print_figure(fname, **kwargs)
  File "/usr/local/lib/python3.5/dist-packages/matplotlib/backends/backend_qt5agg.py", line 172, in print_figure
    super(FigureCanvasQTAggBase, self).print_figure(*args, **kwargs)
  File "/usr/local/lib/python3.5/dist-packages/matplotlib/backend_bases.py", line 2259, in print_figure
    **kwargs)
  File "/usr/local/lib/python3.5/dist-packages/matplotlib/backends/backend_agg.py", line 584, in print_jpg
    return background.save(filename_or_obj, format='jpeg', **options)
  File "/usr/lib/python3/dist-packages/PIL/Image.py", line 1675, in save
    save_handler(self, fp, filename)
  File "/usr/lib/python3/dist-packages/PIL/JpegImagePlugin.py", line 708, in _save
    ImageFile._save(im, fp, [("jpeg", (0, 0)+im.size, 0, rawmode)], bufsize)
  File "/usr/lib/python3/dist-packages/PIL/ImageFile.py", line 480, in _save
    e = Image._getencoder(im.mode, e, a, im.encoderconfig)
  File "/usr/lib/python3/dist-packages/PIL/Image.py", line 431, in _getencoder
    return encoder(mode, *args + extra)
TypeError: integer argument expected, got float

示例代码:

import sys

from PyQt5.QtGui import *
from PyQt5.QtWidgets import *
from PyQt5.QtCore import *

from matplotlib.backends.backend_qt5agg import FigureCanvasQTAgg as FigureCanvas
from matplotlib.backends.backend_qt5agg import NavigationToolbar2QT as NavigationToolbar
from mpl_toolkits.mplot3d import Axes3D
import matplotlib.pyplot as plt

class plot(QWidget):
    def __init__(self):
        super().__init__()

        self.figure = plt.figure()
        self.canvas = FigureCanvas(self.figure)

        self.Layout = QVBoxLayout()

        self.xarray = [1,2,3,4,5,6]
        self.yarray = [6,7,5,4,2,1]

        update_btn = QPushButton("Save Plot", self)
        self.Layout.addWidget(update_btn, 1)
        update_btn.clicked.connect(self.saveplot)

        self.createplot()

        self.setLayout(self.Layout)

    def saveplot(self):
        filename = QFileDialog.getSaveFileName(self, "Save Plot As", "plot.jpg", "*.jpg ;; *.png ;; *.pdf")
        savename = filename[0]
        if savename:
            self.figure.savefig(savename)

    def createplot(self):
        self.ax = self.figure.add_subplot(1,1,1)
        self.ax.grid()
        self.plot = self.ax.plot(self.xarray, self.yarray,'o', marker = 'o', c= 'b')[0]

        self.Layout.addWidget(self.canvas, 2)
        self.canvas.draw()

if __name__ == '__main__':
    appl = QApplication(sys.argv)
    main = plot()
    main.show()
    sys.exit(appl.exec_())

版本信息:

Matplotlib 2.1.0

Matplotlib 2.1.0

Pyqt5

Python 3.5

Python 3.5

Pillow-3.1.2

Pillow-3.1.2

Ubuntu 16.04

Ubuntu 16.04

推荐答案

此问题已通过更新 Pillow 得到纠正.运行以下命令:sudo pip3pillow --upgrade

The issue was corrected by updating Pillow. The following command was run: sudo pip3 pillow --upgrade

这篇关于Matplotlib 将文件保存为 JPEG 编码器错误的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆