Python: PIL (Pillow) & Numpy intersect images


Input images used :

1.png C.png

from itertools import izip
from PIL import Image
import numpy as np
i1 = Image.open('1.png').convert(mode='L', dither=Image.NONE)
i2 = Image.open('C.png').convert(mode='L', dither=Image.NONE)
pixelThreshold = 200
i1 = np.array(i1)
i1 = np.where(i1 > pixelThreshold, 255, 0)
i2 = np.array(i2)
i2 = np.where(i2 > pixelThreshold, 255, 0)
final1 = (i1+i2)
final1 = Image.fromarray(final1.astype(np.uint8))
final1.show()
final1.save('test.png')

1 Comment

  1. # Here’s how to intersect a multitude of images
    from PIL import Image
    import numpy as np
    import os

    images = []
    file_o = r’.\tmp’
    for i, filename in enumerate(os.listdir(file_o)):
    images.append(Image.open(os.path.join(file_o,filename)).convert(mode=’L’, dither=Image.NONE))

    # threshold the image
    t_images = []
    pixelThreshold = 200
    for image in images:
    image = np.array(image)
    t_images.append(np.where(image > pixelThreshold, 255, 0))

    composite = t_images[0]
    for i, image in enumerate(t_images):
    if i >= 1:
    composite = (composite + image)

    composite = np.where(composite > pixelThreshold, 255, 0)
    composite = Image.fromarray(composite.astype(np.uint8))
    composite.show()
    composite.save(‘composite.png’)

    Like

Leave a Comment

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google photo

You are commenting using your Google account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s