PIL Image to NumPy array
(image)
| 53 | |
| 54 | |
| 55 | def image2array(image): |
| 56 | """PIL Image to NumPy array""" |
| 57 | assert image.mode in ('L', 'RGB', 'CMYK') |
| 58 | arr = numpy.fromstring(image.tobytes(), numpy.uint8) |
| 59 | arr.shape = (image.size[1], image.size[0], len(image.getbands())) |
| 60 | return arr.swapaxes(0, 2).swapaxes(1, 2).astype(numpy.float32) |
| 61 | |
| 62 | |
| 63 | def array2image(arr, mode): |