3

I'm writing a png image to file using PyPng, the file is created but it's all black when viewed.Here is my code:

pngImage = numpy.uint16(numpy.zeros((NUM_ROWS, NUM_COLS)))
#code that assigns the pixels
with open(dataFile + ".png", "wb") as outFile:
        pngWriter = png.Writer(NUM_COLS, NUM_ROWS, greyscale=True, bitdepth=16)
        pngWriter.write(outFile, pngImage)

I'm certain that the code that assigns the pixels works fine. I put a break point on the write call and verified that pngImage has nonzero values. pngImage is a 2D array with integer value for each pixel (not R,G,B tuple).

Am I missing something? Should I be converting the numpy array or something?

ventsyv
  • 3,316
  • 3
  • 27
  • 49
  • See http://stackoverflow.com/questions/25696615/can-i-save-a-numpy-array-as-a-16-bit-image-using-normal-enthought-python/25814423#25814423 for an example of creating PNG files with pypng from a numpy array. – Warren Weckesser Dec 18 '14 at 21:24
  • I looked at that but I don't see how it's different from what I'm doing. – ventsyv Dec 18 '14 at 23:03
  • (Ignore my deleted comments and answer.) I just tried your code, using `pngImage = np.random.randint(0, 65536, size=(NUM_ROWS, NUM_COLS)).astype(np.uint16)`, with both `NUM_ROWS` and `NUM_COLS` set to 16, and it worked for me (using pypng 0.0.17). – Warren Weckesser Dec 18 '14 at 23:24
  • Btw, you can specify the type of the array when creating it instead of casting it after creation: `numpy.zeros((NUM_ROWS, NUM_COLS), dtype=numpy.uint16)`. – Imanol Luengo Dec 18 '14 at 23:46
  • Do you have a (small) example PNG that you could email me at drj@pobox.com? Note that you're creating a 16-bit PNG. Pixel values should range from 0 to 65535. What's your typical range for your pixel values? If it's 0 to 255, it will be very dark. – David Jones Sep 08 '15 at 13:25

0 Answers0