Create a PNG :class:`Image` object from a 2-dimensional array. One application of this function is easy PIL-style saving: ``png.from_array(pixels, 'L').save('foo.png')``. Unless they are specified using the *info* parameter, the PNG's height and width are taken from the array s
(a, mode=None, info={})
| 1075 | |
| 1076 | |
| 1077 | def from_array(a, mode=None, info={}): |
| 1078 | """ |
| 1079 | Create a PNG :class:`Image` object from a 2-dimensional array. |
| 1080 | One application of this function is easy PIL-style saving: |
| 1081 | ``png.from_array(pixels, 'L').save('foo.png')``. |
| 1082 | |
| 1083 | Unless they are specified using the *info* parameter, |
| 1084 | the PNG's height and width are taken from the array size. |
| 1085 | The first axis is the height; the second axis is the |
| 1086 | ravelled width and channel index. |
| 1087 | The array is treated is a sequence of rows, |
| 1088 | each row being a sequence of values (``width*channels`` in number). |
| 1089 | So an RGB image that is 16 pixels high and 8 wide will |
| 1090 | occupy a 2-dimensional array that is 16x24 |
| 1091 | (each row will be 8*3 = 24 sample values). |
| 1092 | |
| 1093 | *mode* is a string that specifies the image colour format in a |
| 1094 | PIL-style mode. It can be: |
| 1095 | |
| 1096 | ``'L'`` |
| 1097 | greyscale (1 channel) |
| 1098 | ``'LA'`` |
| 1099 | greyscale with alpha (2 channel) |
| 1100 | ``'RGB'`` |
| 1101 | colour image (3 channel) |
| 1102 | ``'RGBA'`` |
| 1103 | colour image with alpha (4 channel) |
| 1104 | |
| 1105 | The mode string can also specify the bit depth |
| 1106 | (overriding how this function normally derives the bit depth, |
| 1107 | see below). |
| 1108 | Appending ``';16'`` to the mode will cause the PNG to be |
| 1109 | 16 bits per channel; |
| 1110 | any decimal from 1 to 16 can be used to specify the bit depth. |
| 1111 | |
| 1112 | When a 2-dimensional array is used *mode* determines how many |
| 1113 | channels the image has, and so allows the width to be derived from |
| 1114 | the second array dimension. |
| 1115 | |
| 1116 | The array is expected to be a ``numpy`` array, |
| 1117 | but it can be any suitable Python sequence. |
| 1118 | For example, a list of lists can be used: |
| 1119 | ``png.from_array([[0, 255, 0], [255, 0, 255]], 'L')``. |
| 1120 | The exact rules are: ``len(a)`` gives the first dimension, height; |
| 1121 | ``len(a[0])`` gives the second dimension. |
| 1122 | It's slightly more complicated than that because |
| 1123 | an iterator of rows can be used, and it all still works. |
| 1124 | Using an iterator allows data to be streamed efficiently. |
| 1125 | |
| 1126 | The bit depth of the PNG is normally taken from |
| 1127 | the array element's datatype |
| 1128 | (but if *mode* specifies a bitdepth then that is used instead). |
| 1129 | The array element's datatype is determined in a way which |
| 1130 | is supposed to work both for ``numpy`` arrays and for Python |
| 1131 | ``array.array`` objects. |
| 1132 | A 1 byte datatype will give a bit depth of 8, |
| 1133 | a 2 byte datatype will give a bit depth of 16. |
| 1134 | If the datatype does not have an implicit size, |
no test coverage detected