Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/283.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/python-3.x/16.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python 如何将地物对象转换为具有RGBA值的Numpy数组?_Python_Python 3.x_Numpy_Plotly_Reportlab - Fatal编程技术网

Python 如何将地物对象转换为具有RGBA值的Numpy数组?

Python 如何将地物对象转换为具有RGBA值的Numpy数组?,python,python-3.x,numpy,plotly,reportlab,Python,Python 3.x,Numpy,Plotly,Reportlab,我有Plotly地物对象,我使用Plotly创建了地物。我想将此地物对象转换为Numpy数组。这样我就可以把这个数组数据转换成PIL图像对象。并使用.drawImage方法在Reportlab上创建drawImage 这里,b是图形对象 def fig2data ( b ): """ @brief Convert a Plotly figure to a 4D numpy array with RGBA channels and return it @param fig

我有Plotly地物对象,我使用Plotly创建了地物。我想将此地物对象转换为Numpy数组。这样我就可以把这个数组数据转换成PIL图像对象。并使用.drawImage方法在Reportlab上创建drawImage

这里,b是图形对象

def fig2data ( b ):
    """
    @brief Convert a Plotly figure to a 4D numpy array with RGBA channels and return it
    @param fig a plotly figure
    @return a numpy 3D array of RGBA values
    """
    # draw the renderer
    fig.canvas.draw ( )
    # Get the RGBA buffer from the figure
    w,h = fig.canvas.get_width_height()
    buf = numpy.fromstring ( fig.canvas.tostring_argb(), dtype=numpy.uint8 )
    buf.shape = ( w, h,4 )
    # canvas.tostring_argb give pixmap in ARGB mode. Roll the ALPHA channel to have it in RGBA mode
    buf = numpy.roll ( buf, 3, axis = 2 )
    return buf

buf = fig2data(b)
buf

这让我

AttributeError: 'Figure' object has no attribute 'canvas'

我正在使用以下库:

import pandas
import numpy
import plotly.graph_objects as go
from PIL import Image
from reportlab.pdfgen import canvas
from reportlab.lib.utils import ImageReader
from reportlab.platypus import Paragraph, SimpleDocTemplate, Spacer, Image, Flowable
from reportlab.lib.units import inch
from reportlab.lib.styles import getSampleStyleSheet

谢谢。

不确定您的设置或导入,但如果一切正常,您的问题可能是需要将画布附加到图形上。这可以通过使用FigureCanvasAgg完成:


    from matplotlib.backends.backend_agg import FigureCanvasAgg

    def fig2data ( b ):
        """
        @brief Convert a Matplotlib figure to a 4D numpy array with RGBA channels 
         and return it
        @param fig a matplotlib figure
        @return a numpy 3D array of RGBA values
        """

        canvas = FigureCanvasAgg(fig)
        # draw the renderer
        fig.canvas.draw ( )
        # Get the RGBA buffer from the figure
        w,h = fig.canvas.get_width_height()
        buf = numpy.fromstring ( fig.canvas.tostring_argb(), dtype=numpy.uint8 )
        buf.shape = ( w, h,4 )
        # canvas.tostring_argb give pixmap in ARGB mode. Roll the ALPHA channel to 
         have it in RGBA mode
        buf = numpy.roll ( buf, 3, axis = 2 )
        return buf

    buf = fig2data(b)
    buf


希望这有帮助

不确定您的设置或导入,但如果一切正常,您的问题可能是需要将画布附加到图形上。这可以通过使用FigureCanvasAgg完成:


    from matplotlib.backends.backend_agg import FigureCanvasAgg

    def fig2data ( b ):
        """
        @brief Convert a Matplotlib figure to a 4D numpy array with RGBA channels 
         and return it
        @param fig a matplotlib figure
        @return a numpy 3D array of RGBA values
        """

        canvas = FigureCanvasAgg(fig)
        # draw the renderer
        fig.canvas.draw ( )
        # Get the RGBA buffer from the figure
        w,h = fig.canvas.get_width_height()
        buf = numpy.fromstring ( fig.canvas.tostring_argb(), dtype=numpy.uint8 )
        buf.shape = ( w, h,4 )
        # canvas.tostring_argb give pixmap in ARGB mode. Roll the ALPHA channel to 
         have it in RGBA mode
        buf = numpy.roll ( buf, 3, axis = 2 )
        return buf

    buf = fig2data(b)
    buf


希望这有帮助

Hello@KathyJones,这给了我
AttributeError:“Figure”对象没有属性“set\u canvas”
。你能分享你导入的库吗?还有详细的错误报告。我已经添加了这个问题。Hello@KathyJones,这给了我
AttributeError:“Figure”对象没有属性“set\u canvas”
。你能分享你导入的库吗?还有详细的错误报告。我已经补充了这个问题。