title: Sending an image over a socket as bytes
body: <p>I want to send a screenshot of the server to the client, in order to do this, I want to send the image as bytes, and split it to parts, each part in length of 1024.</p>
  
  <p>First, the program does multiple things, it sends also files, the time etc.. so the client needs to tell the server he/(it)? wants an image so he first sends 'Img', then the server returns 'Img' back, when the client receives the 'Img' back, he starts listening to all the parts and the server starts sending all the parts, when the last part is sent, the server sends 'Sent'.</p>
  
  <p>My code so far: <strong>Client.py:</strong></p>
  
  <pre><code>def listen_for_image():
      bytes_data = socket_con.recv(1024)
      part = bytes_data
      i = 1
      print("Part #", str(i))
      while part.decode() != 'Sent':
          part = socket_con.recv(1024)
          bytes_data += part
          i += 1
          print("Part #", str(i))
      return bytes_data
  
  
      socket_con.send('Img')
      data_back = socket_con.recv(1024)
      if data_back.decode() == 'Img':
          bytes_full_data = listen_for_image()
          img = Image.frombytes('RGB', (1366, 768), bytes_full_data)
          img.save('newimg.jpg')
  </code></pre>
  
  <p><strong>Server.py:</strong></p>
  
  <pre><code>def send_screen_shot():
      con.send('Img'.encode())
      image = ImageGrab.grab()
      start = 0
      end = 1024
      while end &lt; len(image.tobytes()):
          con.send(image.tobytes()[start:end])
          start = end + 1
          end += 1024
      con.send("Sent".encode())
  
  
  if con.recv(1024).decode() == 'Img':
          send_screen_shot()
  </code></pre>
  
  <p>Note that my program does more so this is not the full code but only the parts needed, if you need something else, just ask :)</p>
  
  <p>The problem with my code is that on client side I get an error:</p>
  
  <pre><code>Traceback (most recent call last):
    File "C:/Users/dodob/PycharmProjects/untitled/client.py", line 33, in &lt;module&gt;
      bytes_full_data = listen_for_image()
    File "C:/Users/dodob/PycharmProjects/untitled/client.py", line 14, in listen_for_image
      while part.decode() != 'Sent':
  UnicodeDecodeError: 'utf-8' codec can't decode byte 0xff in position 0: invalid start byte
  </code></pre>
  
  <p><strong>EDIT:</strong>
  I tried switching the encoder in order to solve this problem, for example: </p>
  
  <p><strong>Client.py:</strong></p>
  
  <pre><code>def listen_for_image():
      bytes_data = socket_con.recv(1024)
      part = bytes_data
      i = 1
      print("Part #", str(i))
      while part.decode('ISO-8859-1') != 'Sent':
          part = socket_con.recv(1024)
          bytes_data += part
          i += 1
          print("Part #", str(i))
      return bytes_data
  </code></pre>
  
  <p><strong>Server.py:</strong></p>
  
  <pre><code>def send_screen_shot():
      con.send('Img'.encode())
      image = ImageGrab.grab()
      start = 0
      end = 1024
      while end &lt; len(image.tobytes()):
          con.send(image.tobytes(encoder_name='ISO-8859-1')[start:end])
          start = end + 1
          end += 1024
      con.send("Sent".encode('ISO-8859-1'))
  </code></pre>
  
  <p>But then I get an error on server side:</p>
  
  <pre><code>Traceback (most recent call last):
    File "C:\Python\Python35\lib\site-packages\PIL\Image.py", line 437, in _getencoder
      encoder = getattr(core, encoder_name + "_encoder")
  AttributeError: module 'PIL._imaging' has no attribute 'ISO-8859-2_encoder'
  
  During handling of the above exception, another exception occurred:
  
  Traceback (most recent call last):
    File "C:/Users/dodob/PycharmProjects/untitled/server.py", line 42, in &lt;module&gt;
      send_screen_shot()
    File "C:/Users/dodob/PycharmProjects/untitled/server.py", line 19, in send_screen_shot
      con.send(image.tobytes(encoder_name='ISO-8859-2')[start:end])
    File "C:\Python\Python35\lib\site-packages\PIL\Image.py", line 681, in tobytes
      e = _getencoder(self.mode, encoder_name, args)
    File "C:\Python\Python35\lib\site-packages\PIL\Image.py", line 441, in _getencoder
      raise IOError("encoder %s not available" % encoder_name)
  OSError: encoder ISO-8859-2 not available
  </code></pre>
  
  <p>And the client keeps running like this:</p>
  
  <pre><code>...
  Part # 2376824
  Part # 2376825
  Part # 2376826
  Part # 2376827
  Part # 2376828
  Part # 2376829
  Part # 2376830
  Part # 2376831
  Part # 2376832
  Part # 2376833
  Part # 2376834
  ...
  </code></pre>
  
  <p>I think this error is caused because there is no such encoder but I tried multiple ones and it happens every time.. but anyway, I don't know why the client keeps running, the <code>recv</code> function should stop until it receives something, and here it is running forever.</p>
  
  <p>Thanks for helping.</p>