Stack multiple images in python pillow -
i trying image stacking in python pillow. take large number of images (say 10), , each pixel, take median value this: http://petapixel.com/2013/05/29/a-look-at-reducing-noise-in-photographs-using-median-blending/.
right now, can in incredibly brute force manner (using getpixel , put pixel), takes long time.
here have far:
import os pil import image files = os.listdir("./") new_im = image.new('rgb', (4000,3000)) ims={} in range(10,100): ims[i]={} im=image.open("./"+files[i]) x in range(400): ims[i][x]={} y in range(300): ims[i][x][y]=im.getpixel((x,y)) x in range(400): y in range(300): these1=[] these2=[] these3=[] in ims: these1.append(ims[i][x][y][0]) these2.append(ims[i][x][y][1]) these3.append(ims[i][x][y][2]) these1.sort() these2.sort() these3.sort() new_im.putpixel((x,y),(these1[len(these1)/2],these2[len(these2)/2],these3[len(these3)/2])) new_im.show()
you can vectorise lot of these loops arrays. example np.array(im) return array of pixels, shape (400, 300, 3). store in array.
image_stacks = np.zeros(shape=(10, 400, 300, 3), dtype=np.uint8) in xrange(image_stacks.shape[0]): # open image file , store in variable `im`, image_stacks[i] = np.array(im) now can calculate median preferred way, numpy has method that, too.
image_median = np.median(image_stacks, axis=0).astype(np.uint8) image = image.fromarray(image_median) two things notice here np.median() return float type array, , want convert unsigned int8. other thing if number of elements even, median calculated mean of 2 middle values, may end being odd number divided two, such 13.5. when converted integer, rounded down. such minor precision loss should not visually affect result.
Comments
Post a Comment