Transfering Bitmap Images over a network
Published: 2021-01-05 12:46:38

![Alt Text](https://dev-to-uploads.s3.amazonaws.com/i/p8fds3zul61lt0ha6cr5.jpg)
######Photo by Sora Shimazaki from Pexels

### An image is made up of pixels(tiny dots of colours). Images can be stored digitally as bitmaps.
A bitmap (Bit-Map) is a mapping of a domain to bits. In our case, the domain is an array of pixels, here we map a single bit to a pixel.
Pix-map is a map of pixels which maps multiple bits to a pixel.

Bitmaps use the 24bit or 32bit colour depth and use the RGB(255, 255, 255) colour system each of the value in the RGB colour system is stored as a byte(8 bits) an extra 8bits might be used to show transparency of the image
```
R =8bits
G=8bits
B=8bits #24bit
A=8bits #32bit #extra transparency bit
```

Storing images in bitmap format will make them very large so images are mostly stored in compressed file format
.PNG:: the lossless compression. It compresses data by using pattern tin pixel colour to reduce the amount of data stored. [readmore](https://en.wikipedia.org/wiki/Portable_Network_Graphics#Compression).
.jpg:: the lossy compression. It compresses data by getting rid o colours that the human eyes will not pick. [read more](https://en.wikipedia.org/wiki/JPEG#JPEG_compression).


Now we are going to write a program to send an image over a network. So we are going to send an image over a network using **UDP** and we will also use **pickle** for serialisation


1. Fist of we create a socket `sockfd = socket(AF_INET, SOCK_DGRAM)`
2. Open the image to be sent `image = Image.open("image.png")`
3. Get size of the image i.e width and height`width, height = image.size`
4. Iterate through the whole image (the reason why we have two loops because the image is in a two-dimensional array form)
5. Get RGB value of every pixel in the image`rgba = image.getpixel(pos)`
6. Change it to a transferable form using “dump” from the pixel module `msg = dumps(message)`
7. Sending it `sockfd.sendto(msg, ("127.0.0.1", 9999))`

![Alt Text](https://dev-to-uploads.s3.amazonaws.com/i/cuo99rkfm44tbiltyic5.png)
###image we are sending
```
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)
			msg = dumps(message)
			sockfd.sendto(msg, ("127.0.0.1", 9999))
			time.sleep(0.001)
		except:
			print(1)#just a check



```

1. Now to receive the image…
2. Create a socket 
3. Bind socket
4. Create an image file to save data sent `image4 = Image.new("RGBA", size=(width, height))`
6. Create a function to receive data and write it into the image file


**Here I already knew the width and height but we can also send that over the network before we start sending the pixel data**

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



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

height = 170
width = 272
isize = (height, width)
#image = open("image2.png", "x")
image4 = Image.new("RGBA", size=(width, height))
pixels = image4.load()

def get_pixel_data():
	while True:
		msg, cli_addr = sockfd.recvfrom(1024)
		#decoding 
		message = loads(msg)
		pos = message[0]
		rgba = message[1]
		print(pos)
		pixels[pos[0], pos[1]] = rgba
		#print(pixels[pos[0], pos[1]])
		if pos[0] + 1 == width and pos[1] + 1 == height:	
			image4.save("image4.png")
			break
#calling my function
get_pixel_data()


```
###image we received
![Alt Text](https://dev-to-uploads.s3.amazonaws.com/i/d93ngu0mobqhcoq64whq.png)

This was fun to do hope you learnt from my write up, please leave a comment or contact me [@black_stroke](https://twitter.com/black_strok3)