Movatterモバイル変換


[0]ホーム

URL:


Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Backport PR #29931 on branch v3.10.x (Allow Python native sequences in Matplotlibimsave().)#29933

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to ourterms of service andprivacy statement. We’ll occasionally send you account related emails.

Already on GitHub?Sign in to your account

Merged
Show file tree
Hide file tree
Changes fromall commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 6 additions & 1 deletionlib/matplotlib/image.py
View file
Open in desktop
Original file line numberDiff line numberDiff line change
Expand Up@@ -1536,7 +1536,8 @@ def imsave(fname, arr, vmin=None, vmax=None, cmap=None, format=None,
extension of *fname*, if any, and from :rc:`savefig.format` otherwise.
If *format* is set, it determines the output format.
arr : array-like
The image data. The shape can be one of
The image data. Accepts NumPy arrays or sequences
(e.g., lists or tuples). The shape can be one of
MxN (luminance), MxNx3 (RGB) or MxNx4 (RGBA).
vmin, vmax : float, optional
*vmin* and *vmax* set the color scaling for the image by fixing the
Expand DownExpand Up@@ -1567,6 +1568,10 @@ def imsave(fname, arr, vmin=None, vmax=None, cmap=None, format=None,
default 'Software' key.
"""
from matplotlib.figure import Figure

# Normalizing input (e.g., list or tuples) to NumPy array if needed
arr = np.asanyarray(arr)

if isinstance(fname, os.PathLike):
fname = os.fspath(fname)
if format is None:
Expand Down
22 changes: 22 additions & 0 deletionslib/matplotlib/tests/test_image.py
View file
Open in desktop
Original file line numberDiff line numberDiff line change
Expand Up@@ -184,6 +184,28 @@ def test_imsave(fmt):
assert_array_equal(arr_dpi1, arr_dpi100)


def test_imsave_python_sequences():
# Tests saving an image with data passed using Python sequence types
# such as lists or tuples.

# RGB image: 3 rows × 2 columns, with float values in [0.0, 1.0]
img_data = [
[(1.0, 0.0, 0.0), (0.0, 1.0, 0.0)],
[(0.0, 0.0, 1.0), (1.0, 1.0, 0.0)],
[(0.0, 1.0, 1.0), (1.0, 0.0, 1.0)],
]

buff = io.BytesIO()
plt.imsave(buff, img_data, format="png")
buff.seek(0)
read_img = plt.imread(buff)

assert_array_equal(
np.array(img_data),
read_img[:, :, :3] # Drop alpha if present
)


@pytest.mark.parametrize("origin", ["upper", "lower"])
def test_imsave_rgba_origin(origin):
# test that imsave always passes c-contiguous arrays down to pillow
Expand Down
Loading

[8]ページ先頭

©2009-2025 Movatter.jp