Python save matplotlib figure on an PIL Image object

前端 未结 2 1018
星月不相逢
星月不相逢 2021-02-02 00:15

HI, is it possible that I created a image from matplotlib and I save it on an image object I created from PIL? Sounds very hard? Who can help me?

2条回答
  •  猫巷女王i
    2021-02-02 00:25

    I was having the same question and I stumbled upon this answer. Just wanted to add to the above answer that PIL.Image.fromstring has been deprecated and frombytes should be used now instead of fromstring. Hence, we should modify line:

    pil_image = PIL.Image.fromstring('RGB', canvas.get_width_height(), 
                     canvas.tostring_rgb())
    

    to

    pil_image = PIL.Image.frombytes('RGB', canvas.get_width_height(), 
                     canvas.tostring_rgb())
    

提交回复
热议问题