Skip to content

Commit c8493ea

Browse files
committed
Fix dimensions of greyscale image.
1 parent 7891790 commit c8493ea

File tree

1 file changed

+10
-2
lines changed

1 file changed

+10
-2
lines changed

src/_png.cpp

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -369,8 +369,6 @@ static PyObject *_read_png(PyObject *filein, bool float_result)
369369
} else {
370370
dimensions[2] = 1; // Greyscale images
371371
}
372-
// For gray, return an x by y array, not an x by y by 1
373-
num_dims = (png_get_color_type(png_ptr, info_ptr) & PNG_COLOR_MASK_COLOR) ? 3 : 2;
374372

375373
if (float_result) {
376374
double max_value = (1 << bit_depth) - 1;
@@ -431,6 +429,16 @@ static PyObject *_read_png(PyObject *filein, bool float_result)
431429
// free the png memory
432430
png_read_end(png_ptr, info_ptr);
433431

432+
// For gray, return an x by y array, not an x by y by 1
433+
num_dims = (png_get_color_type(png_ptr, info_ptr) & PNG_COLOR_MASK_COLOR) ? 3 : 2;
434+
435+
if (num_dims == 2) {
436+
PyArray_Dims dims = {dimensions, 2};
437+
PyObject *reshaped = PyArray_Newshape((PyArrayObject *)result, &dims, NPY_CORDER);
438+
Py_DECREF(result);
439+
result = reshaped;
440+
}
441+
434442
exit:
435443
if (png_ptr && info_ptr) {
436444
#ifndef png_infopp_NULL

0 commit comments

Comments
 (0)