Skip to content Skip to sidebar Skip to footer

Image Deduction With Pillow And Numpy

I have two images: and I want to export an image that just has the red 'Hello' like: So I am running a simple deduction python script: from PIL import Image import numpy as np

Solution 1:

It is perhaps easier just to set the pixels you don't want in the second image to 0?

im = im2.copy()
im[im1 == im2] = 0
im = Image.fromarray(im)

seems to work for me (obviously just with bigger artifacts because I used your uploaded JPGs)

Result of script

It is also possible to do this without numpy:

from PIL import ImageChops
from PIL import Image

root = '/root/'
im1 = Image.open(root + '1.jpg')
im2 = Image.open(root + '2.jpg')

defnonzero(a):
    return0if a < 10else255

mask = Image.eval(ImageChops.difference(im1, im2), nonzero).convert('1')

im = Image.composite(im2, Image.eval(im2, lambda x: 0), mask)

Post a Comment for "Image Deduction With Pillow And Numpy"