Detecting missing pixels in an Image
Tags: python, network, beginners
Published: 2021-01-08 14:03:21



###Today we will be talking about finding missing data from the image we sent over the network. From our previous example of sending an image over a network, we are going to check if any pixels were lost during the transfer

####First off we will try to map out an algorithm for detecting an error in the image file.
Receive a pixel
Store position of the pixel
Receive the next 
Check if the pixels are in the correct order i.e pixel1, pixel2
If they are not then there is a missing pixel
Get the position of the missing pixel
Continue this 6 steps repeatedly until the image is sent
Send a request for missing pixels
Open image
And insert missing pixels into their position
**Because we are transferring images on the same machine I induced an error by skipping any pixel with an odd Y value.**


###This is the code that sends the image
```
from socket import *
from pickle import *
from PIL import Image
import time

#create a socket

sockfd = socket(AF_INET, SOCK_DGRAM)

#open image
image = Image.open("100kb.png")

#get image size
width, height = image.size
print(image.size)

for x in range(width):
	for y in range(height):
		
		#get the position in the two dimensional array
		try:
			pos = (x, y)
			rgba = image.getpixel(pos)

			message = (pos, rgba)
			if y % 2 == 0:
				msg = dumps(message)
				sockfd.sendto(msg, ("127.0.0.1", 9916))
			#time.sleep(0.001)
		except:
			print(1)
message = "done"
msg = dumps(message)
sockfd.sendto(msg, ("127.0.0.1", 9916))

def send_missing_pixel():
	while True:
		msg, cli_addr = sockfd.recvfrom(1024)
		pos = loads(msg)
		if pos == "done":
			break
		rgba = image.getpixel(pos)
		data = (pos, rgba)
		data = dumps(data)
		sockfd.sendto(data, cli_addr)
		print("eggs")


send_missing_pixel()

```


**The first image sent**
![Alt Text](https://dev-to-uploads.s3.amazonaws.com/i/g7hvtd1h5s1fp64y91k4.png)

###Code to receive the image and ask for missing pixels

```
from socket import *
from pickle import *
from PIL import Image 
import time



#viewer = ImageViewer()
sockfd = socket(AF_INET, SOCK_DGRAM)
sockfd.bind(("127.0.0.1", 9916))

height = 170
width = 272
isize = (height, width)
#image = open("image2.png", "x")
image4 = Image.new("RGBA", size=(width, height))
pixels = image4.load()
#the following variables for storing positions of missing pixels
error_pos = []
pos1 = None
x = []
y = []

def get_pixel_data():
	j = 0
	x1 = []
	e_count = 0
	#erro counter
	error_count = 0
	while True:
		j = j + 1
		msg, cli_addr = sockfd.recvfrom(1024)
		#decoding 
		message = loads(msg)
		if message == "done":
			image4.save("image3.png")
			break
		pos = message[0]
		rgba = message[1]
		#print(pos)
		#setting pixels into image file
		pixels[pos[0], pos[1]] = rgba
		#stroing postitions to check if there is a missing pixel
		xy = (pos[0], pos[1]) 
		#print(xy)
		x1.append(xy)
		try:
			s = j - 1
			#print(x1[_s])
			if len(x1) > 1:
				#checking difference betwwen previous coordinates and present coordinates 
				xnums = x1[s][0] - x1[s-1][0]
				ynums = x1[s][1] - x1[s-1][1]
				#this will check if the difference Y returns a negative
				#that means a new X value and recalculate the Y value
				if ynums < 0:
					ynums = height - x1[s-1][1] + x1[s][1]
				#iterate through the difference and create a list of missing pixel positions
				if ynums > 1:
					for i in range(1, ynums):
						if xnums > 1:

							un = x1[s][0] + i
							x.append(un)						
						if xnums <= 1:
							e = x1[s][0]
							x.append(e)
						un = x1[s][1] + i
						y.append(un)
						_c = (x[e_count], y[e_count])
						error_pos.append(_c)
						e_count = e_count + 1
		except:
			pass

	q = len(error_pos)
	j1 = width * height
	#print(q , e_count)
	if e_count == q:
		#print(123)
		get_missing_data(cli_addr)


def get_missing_data(cli_addr):
	_x = len(error_pos)
image5 = Image.open("image3.png")
	for i in range(_x):
		e_pos = error_pos[i]
		msg = dumps(e_pos)
		sockfd.sendto(msg, cli_addr)
		#time.sleep(0.001)
		msg, cli_addr = sockfd.recvfrom(1024)
		#decoding 
		message = loads(msg)
		pos = message[0]
		rgba = message[1]
		pixels = image5.load()
		pixels[pos[0], pos[1]] = rgba
		print(pixels[pos[0], pos[1]])
	image5.save("image3.png")
	x = "done"
	msg = dumps(x)
	sockfd.sendto(msg, cli_addr)
		
get_pixel_data()

	

```

**Halfway into correcting image**
![Alt Text](https://dev-to-uploads.s3.amazonaws.com/i/lnh6038xyik4e8dtjhz6.png)

**complete Image**
![Alt Text](https://dev-to-uploads.s3.amazonaws.com/i/092vj7nd2fp6ptbmdrw2.png)

#####Photo by Umberto on Unsplash

leave a comment, suggestion or contact me [@black_strok3]()