-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathimg_arr.py
29 lines (25 loc) · 1.03 KB
/
img_arr.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
import numpy as np
from PIL import Image
color = [255,255,255] #rgb value
tolerance = 120 #between 0 and 255
replace = [0,0,0,0] #rgba value for png, rgb for jpg
imagepath="C:\\Pictures\\python\\original.png"
print("opening image...")
img = Image.open(imagepath, 'r')
height, width = img.size
print("Height: {}, Width: {}".format(height, width))
print("converting into array...")
pixels = np.array(img)
print("processing image...")
for y in range(height):
for x in range(width):
try:
if ((color[0] - tolerance < pixels[x][y][0] < color[0] + tolerance) and (color[1] - tolerance < pixels[x][y][1] < color[1] + tolerance) and (color[2] - tolerance < pixels[x][y][2] < color[2] + tolerance)):
pixels[x][y] = replace
#print("x = {}, y = {}, color = {}".format(x, y, pixels[x][y]))
except:
print("No pixel found at x={} y={}".format(x, y))
print("saving file...")
result = Image.fromarray(pixels)
result.save("C:\\Users\\802042\\Pictures\\python\\result.png")
print("all done!")