There is a task.
There is an image. It is necessary to add noise to the image, and then, using the threshold filtering algorithm, clear the noisy image. Under the threshold filtering algorithm, I mean "If the brightness of a pixel exceeds the average brightness of a local neighborhood, then the brightness of a pixel is replaced with the average brightness of a neighborhood." The code itself:
from PIL import Image import random from multiprocessing import Pool from multiprocessing.dummy import Pool as ThreadPool img=Image.open('pic.bmp') print(img.size) randomenter=int(input('Enter numpix: ')) for numpix in range(0, randomenter): x=random.randint(0,int(img.size[0]-1)) y=random.randint(0,int(img.size[1]-1)) r=random.randint(0,255) g=random.randint(0,255) b=random.randint(0,255) img.putpixel((x,y),(r,g,b)) img.show() img.save("noise.bmp", "BMP") img2=Image.open("noise.bmp") width, height = img2.size pix=img2.load() for x in range(0,width-1): if x-1>0 and x<width: for y in range(0,height-1): if y-1>0 and y<height: local1=(0.3 * pix[x-1,y-1][0]) + (0.59 * pix[x-1,y-1][1]) + (0.11 * pix[x-1,y-1][2]) local2=(0.3 * pix[x-1,y][0]) + (0.59 * pix[x-1,y][1]) + (0.11 * pix[x-1,y][2]) local3=(0.3 * pix[x-1,y+1][0]) + (0.59 * pix[x-1,y+1][1]) + (0.11 * pix[x-1,y+1][2]) local4=(0.3 * pix[x,y-1][0]) + (0.59 * pix[x,y-1][1]) + (0.11 * pix[x,y-1][2]) LOCAL5=(0.3 * pix[x,y][0]) + (0.59 * pix[x,y][1]) + (0.11 * pix[x,y][2]) local6=(0.3 * pix[x,y+1][0]) + (0.59 * pix[x,y+1][1]) + (0.11 * pix[x,y+1][2]) local7=(0.3 * pix[x+1,y-1][0]) + (0.59 * pix[x+1,y-1][1]) + (0.11 * pix[x+1,y-1][2]) local8=(0.3 * pix[x+1,y][0]) + (0.59 * pix[x+1,y][1]) + (0.11 * pix[x+1,y][2]) local9=(0.3 * pix[x+1,y+1][0]) + (0.59 * pix[x+1,y+1][1]) + (0.11 * pix[x+1,y+1][2]) localall=(local1+local2+local3+local4+local6+local7+local8+local9)/8 localall2=(local1+local2+local3+local4+local6+local7+local8+local9+LOCAL5)/9 if LOCAL5>=localall: img2.putpixel((x,y),(int(pix[x,y][0]*localall/LOCAL5),int(pix[x,y][1]*localall/LOCAL5),int(pix[x,y][2]*localall/LOCAL5))) img2.show()
Stuck just at the moment of replacing the brightness. Official documentation has no details on this. Is there any solution to this problem? Update I got to the pixels. Now the problem is that the neighboring pixels change with the variable.
RGB
image in your understanding? - Avernial