Why does NumPy's random function seemingly display a pattern in its generated values?
Don't blame Numpy, blame PIL / Pillow. ;) You're generating floats, but PIL expects integers, and its float to int conversion is not doing what we want. Further research is required to determine exactly what PIL is doing...
In the mean time, you can get rid of those lines by explicitly converting your values to unsigned 8 bit integers:
img_arrays = (np.random.random((100, 256, 256, 3)) * 255).astype(np.uint8)
As FHTMitchell notes in the comments, a more efficient form is
img_arrays = np.random.randint(0, 256, (100, 256, 256, 3), dtype=np.uint8)
Here's typical output from that modified code:
The PIL Image.fromarray function has a known bug, as described here. The behaviour you're seeing is probably related to that bug, but I guess it could be an independent one. ;)
FWIW, here are some tests and workarounds I did on the bug mentioned on the linked question.
I'm pretty sure the problem is to do with the dtype, but not for the reasons you think. Here is one with np.random.randint(0, 256, (1, 256, 256, 3), dtype=np.uint32)
note the dtype is not np.uint8
:
Can you see the pattern ;)? PIL interprets 32 bit (4 byte) values (probably as 4 pixels RGBK) differently from 8 bit values (RGB for one pixel). (See PM 2Ring's answer).
Originally you were passing 64 bit float values, these are going to also are interpreted differently (and probably incorrectly from how you intended).