Skip to content

Incompatible with matplotlib >=3.6: FigureCanvas.tostring_rgb() removed #282

@Xyc2016

Description

@Xyc2016

Description

Starting from matplotlib 3.6+, FigureCanvas.tostring_rgb() was removed (replaced by buffer_rgba()). This causes AttributeError when trying to render segmentation results in both fastsam/prompt.py and utils/tools.py.

Error

AttributeError: 'FigureCanvasAgg' object has no attribute 'tostring_rgb'. Did you mean: 'tostring_argb'?

Locations

  1. fastsam/prompt.py line 172 (in plot_to_result())
  2. utils/tools.py line 183 (in fast_process())

Both follow the same pattern:

try:
    buf = fig.canvas.tostring_rgb()
except AttributeError:
    fig.canvas.draw()
    buf = fig.canvas.tostring_rgb()

Note: The except block retries the same failing call, which will never succeed.

Suggested Fix

Replace tostring_rgb() with buffer_rgba() which is the modern matplotlib API:

try:
    buf = fig.canvas.buffer_rgba()
except AttributeError:
    fig.canvas.draw()
    buf = fig.canvas.buffer_rgba()

Also update the reshape from 3 channels (RGB) to 4 channels (RGBA), and switch color conversion accordingly.

Additional note

When running on a headless server (no display), matplotlib may also fail due to missing TkAgg backend. A robust fix would add matplotlib.use("Agg") or switch backend before plot calls.

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions