crzcas

UDP client (pixels lost)

Jan 18th, 2021
100
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 1.05 KB | None | 0 0
  1. # UDP client with pixels lost of an image
  2. from PIL import Image
  3. import pickle
  4. import socket
  5. from time import sleep
  6. from random import randint
  7.  
  8.  
  9. udp_client = socket.socket(socket.AF_INET, socket.SOCK_DGRAM)
  10.  
  11. image = Image.open("DR-2.3-smiley.bmp")   # Image of test
  12.  
  13. width, height = image.size
  14. pixels_lost = 0
  15.  
  16. width, height = image.size
  17. print("Image size: " + str(width) + " " + str(height))
  18. #print(width, height)
  19.  
  20. size = width * height
  21. print("Pixels to be sent: " + str(size))
  22.  
  23. message = str(size)
  24. data = message.encode()
  25. udp_client.sendto(data, ("127.0.0.1", 20001))
  26.  
  27. for y in range(height):
  28.     for x in range(width):
  29.         pos = (x, y)
  30.         rgba = image.getpixel(pos)
  31.         message = (pos, rgba)
  32.         data = pickle.dumps(message)
  33.         if randint(0,9) > 0:   # random error generator
  34.             udp_client.sendto(data, ("127.0.0.1", 20001))
  35.         else:
  36.             pixels_lost += 1
  37.         sleep(0.001)
  38.  
  39. message = ((9,9), (0,0,0,0))
  40.  
  41. data = pickle.dumps(message)
  42. udp_client.sendto(data, ("127.0.0.1", 20001))
Advertisement
Add Comment
Please, Sign In to add comment