3

I would like a way to change all of the pixels of a photo to white, except the pixels that are white or black that were already in the photo.

I tried to use PIL but I couldn't find it.

Tomerikoo
  • 18,379
  • 16
  • 47
  • 61
Carlos
  • 119
  • 1
  • 9

2 Answers2

6

i would like the way to change all of the pixels of a photo to white, except the pixels that white or black that were already in the photo.

So basically you want to change all pixels to white except black, right? If so, then below work (note: it expect cv2lib is installed on your machine)

import cv2
import numpy as np

img = cv2.imread('my_img.jpeg')
img[img != 0] = 255 # change everything to white where pixel is not black
cv2.imwrite('my_img2.jpeg', img)
sagarr
  • 1,152
  • 1
  • 9
  • 16
  • Yes, this its partly what I wanted to do. I wanted to change all the pixels that were neither black or white. So, if the pixel color is different to black or white, change the color. I tried your code with img[img != 0 and != 255] = 177 but it shows an error with the and. I was also wondeering if I could instead of changing them to a fix color (such as 177 in this example), if I could turn them transparent. Thank you so much! – Carlos Mar 16 '19 at 11:42
  • 1
    In that case you can go with bitwise `&`, like `img[(img != 0) & (img != 255)] = 177` – sagarr Mar 16 '19 at 16:21
  • 1
    It worked perfectly yesterday but today it displays this error. What can I do? TypeError: 'NoneType' object does not support item assignment – Carlos Mar 17 '19 at 13:20
  • It works with some photos, with others it doesn't. Any idea why? – Carlos Mar 18 '19 at 21:24
  • for `TypeError: 'NoneType' object does not support item assignment ` error, just check the image name correct. In my case, i made a mistake in image name and it threw the same error. (instead of .jpg, i put as .jpgg) – Karthic Srinivasan Feb 24 '20 at 06:00
1

Assuming you have access to matplotlib and are willing to use it:

import matplotlib.pyplot as plt
import matplotlib.image as mpimg

# read the image pixels and saves them as a numpy array
image = mpimg.imread('<your image>')

# see original image (just for testing)
plt.imshow(image)
plt.show()

# loop through all pixels, and replace those that are not strict white or black with white
for x in range(image.shape[0]):
    for y in range(image.shape[1]):
        if (image[x,y]!=0).all() and (image[x,y]!=1).all():
            image[x,y] = [1,1,1]  

# see modified image (to make sure this is what you need)
plt.imshow(image)
plt.show()

# save image
mpimg.imsave('<new name>',image)

You can probably vectorize this, but I find this more readable, depending on your performance requirements. Also, make sure the input is in [0,1] format. If it is in [0,255] modify the above 1s with 255s.

Note: this solution works for RGB, with no alpha. If you have an alpha, you might need to modify, depending on your requirements.

Tomerikoo
  • 18,379
  • 16
  • 47
  • 61
Tacratis
  • 1,035
  • 1
  • 6
  • 16