Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8997059
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 15, 20262026-06-15T23:50:15+00:00 2026-06-15T23:50:15+00:00

When saving a 2-dimensional Numpy array (of single values) with Scipy toimage or imsave

  • 0

When saving a 2-dimensional Numpy array (of single values) with Scipy toimage or imsave the pixel values do not exactly match those in the Numpy array. Instead there are areas, mostly at edges, where the image algorithm seems use some sort of interpolation.

Is there an option to stop that interpolation and retain the exact data (e.g. 7 always gets rgb(7,7,7) in a PNG?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-15T23:50:15+00:00Added an answer on June 15, 2026 at 11:50 pm

    If you have a 2D numpy array, then you are saving into a grayscale PNG so you never get an rgb image (only one channel). I’m not sure what you mean by single values, perhaps it is single precision floats? Although the PIL supports single precision floats, PNG does not. Saving to PNG you can either use 8-bits per channel (the default) or 16-bits per channel. This means that your array will be scaled to a maximum of 2^8/2^16 (8/16 bits), and converted to integer. It is in this conversion that results may vary slightly.

    With scipy.misc.image there seems to be no option to save as 16-bit, so it will always write an 8-bit PNG. But you can use scipy.misc.toimage to create a 16-bit image, just be sure to pass mode='I'. Also be sure to specify the array min and max to avoid scaling. Here’s how to use it to save a 16-bit png:

    import numpy as np
    import scipy.misc
    
    a = np.random.uniform(0, 2**16 - 1, (500, 500)).astype('int32')
    img = scipy.misc.toimage(a, high=np.max(a), low=np.min(a), mode='I')
    img.save('my16bit.png')
    
    # check that you got the same values
    b = scipy.misc.imread('my16bit.png')
    b.dtype
    # dtype('int32')
    np.array_equal(a, b)
    # True
    

    Note that in this example I used int32 for data type. However, the data must still fit in a uint16. If you put negative values or values larger than 2^16, those will be clipped in the save to PNG. Conversely, even though sp.misc.imread reads as int32, the data will never be more than uint16.

    In summary: if you want to write exactly the same numpy array to a PNG you need to make sure it is of uint8/uint16 type, and that you pass the correct high/low/mode to scipy.misc.toimage.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I am attempting a basic recursion to create multi-dimensional arrays based on the values
Quick sanity check. Is Array.Copy for one dimensional arrays only, or can it handle
The following code creates a multi dimensional list (not sure if that's the Pythonic
I need to compare a 1-dimensional array, in that I need to compare each
I am trying to create a global 2-dimensional array who's size would depend on
I am trying to output a 4D numpy float array to a plaintext file
I'm saving 2-dimensional coordinates on an XML file with a structure similar to: <?xml
I have a multi-dimensional associative array that is encoded into JSON for database storage,
I have a numpy array containing: [1, 2, 3] I want to create an
i have a two dimensional array, and i want to retrieve its value in

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.