|
| 1 | +#Authors Bhargav K,SriVennelaVaishnapu |
| 2 | + |
| 3 | +#Error Diffusion Algorithm |
| 4 | +#error = actualColour - nearestColour |
| 5 | +#PutPixelColour(x+1, y ) = Truncate(GetPixelColour(x+1, y ) + 0.5 * error) |
| 6 | +#PutPixelColour(x , y+1) = Truncate(GetPixelColour(x , y+1) + 0.5 * error) |
| 7 | +#Procedure Truncate(value) |
| 8 | +# If value < 0 Then value = 0 |
| 9 | +# If value > 255 Then value = 255 |
| 10 | +# Return value |
| 11 | +#EndProcedure |
| 12 | + |
| 13 | +from PIL import Image |
| 14 | +i = Image.open("lena.jpg") |
| 15 | +#debugging purpose |
| 16 | +#print(i.format,i.size,i.mode) |
| 17 | +#pixel data is stored in pixels in form of two dimensional array |
| 18 | +pixels = i.load()# this is not a list, nor is it list()'able |
| 19 | +width, height = i.size |
| 20 | +j=Image.new(i.mode,i.size) |
| 21 | +def Truncate(value): |
| 22 | + if(value < 0): |
| 23 | + value = 0 |
| 24 | + if(value > 255): |
| 25 | + value = 255 |
| 26 | + return value |
| 27 | +def Error(value): |
| 28 | + if(value >= 0 and value <=127): |
| 29 | + return 0 |
| 30 | + else: |
| 31 | + return 128 |
| 32 | +for x in range(width-1): |
| 33 | + for y in range(height-1): |
| 34 | + cpixel = pixels[x, y] |
| 35 | + #cpixel[0] contains red value cpixel[1] contains green value |
| 36 | + #cpixel[2] contains blue value cpixel[3] contains alpha value |
| 37 | + error_red = cpixel[0]-Error(cpixel[0]) |
| 38 | + error_green = cpixel[1]-Error(cpixel[1]) |
| 39 | + error_blue = cpixel[2]-Error(cpixel[2]) |
| 40 | + apixel= pixels[x+1,y] |
| 41 | + bpixel= pixels[x,y+1] |
| 42 | + j.putpixel((x+1, y ),(int(Truncate(apixel[0] + 0.5 * error_red)),int(Truncate(apixel[1]+0.5*error_blue)),int(Truncate(apixel[2]+0.5*error_green)))) |
| 43 | + j.putpixel((x , y+1),(int(Truncate(bpixel[0] + 0.5 * error_red)),int(Truncate(bpixel[1]+0.5*error_blue)),int(Truncate(bpixel[2]+0.5*error_green)))) |
| 44 | + |
| 45 | +j.save('error.png') |
0 commit comments