Open Image in Pillow
I'm not immediately able to test the numpy and PIL lines as I don't have those installed, but...



First - is your main loop really setting the pixels you intended? The comment says "create random colors for each pixel" but what it's actually doing is creating random colors in every 3rd pixel - and the stop condition of num_pixels-5 is arbitrary and seemingly will have no effect, since you're stepping 12 each time.

So that's the first thing. If you actually want to set all pixels, it would be range(0, num_pixels, 4)

Second - your line creating the 'pxs' array is making an int array of length 12800 (80*40*4) out of all the pixel components. If you want a binary packed representation, maybe you need to do 

pxdata = struct.pack("&lt;%uf" % len(img.pixels), *img.pixels) # pack RGBA floats as binary.


instead? Double-check what the PIL Image call is taking.