9951 explained code solutions for 126 technologies


python-pillowHow to add noise


Pillow doesn't have integrated noise methods, but noise can be easily simulated using putpixel() to set random color for random pixels of an image:

from PIL import Image
import random

im = Image.open('/var/www/examples/heroine.png')
for i in range( round(im.size[0]*im.size[1]/5) ):
  im.putpixel(
    (random.randint(0, im.size[0]-1), random.randint(0, im.size[1]-1)),
    (random.randint(0,255),random.randint(0,255),random.randint(0,255))
  )

im.show()ctrl + c
PIL

import Pillow package modules

Image.open

open given image with Pillow

round(im.size[0]*im.size[1]/5

each 5th pixel will be noised out (change 5 to larger values to decrease noise density)

.putpixel(

sets color of a given pixel

.show()

displays resulting image


How to add noise, python pillow

Usage example

from PIL import Image, ImageFilter
import random

im = Image.open('/var/www/examples/heroine.png')
for i in range( round(im.size[0]*im.size[1]/5) ):
  im.putpixel(
    (random.randint(0, im.size[0]-1), random.randint(0, im.size[1]-1)),
    (random.randint(0,255),random.randint(0,255),random.randint(0,255))
  )

im.show()