title: binary data to image raw python
body: <p>so here is what I'm trying to do:
  There is this image server that will send data on port 5003
  The format of the data it will transmit is as follows
  1 byte for the image type(0=raw, 1=JPEG)
  then the next 4 byte for the image size
  then after that there will be n byte with the following order
  2 byte for width, 2 byte for height, 1 byte for B, 1 byte for R, 1 byte for G</p>
  
  <p>So what I'm trying to do is to get the data and transform it into an image with the following code:</p>
  
  <pre><code>#! /usr/bin/python
  import socket
  import sys
  import binascii
  from PIL import Image
  from StringIO import StringIO
  
  
  # Connect to the server image
  serverHost = 'localhost'
  serverPort = 5003
  s = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
  s.connect((serverHost, serverPort))
  
  print &gt;&gt;sys.stderr, 'Connecting to host: ' +  str(serverHost) 
  print &gt;&gt;sys.stderr, 'and server Port: ' + str(serverPort)
  
  s.settimeout(1)
  
  #Receive the image type
  imageType = s.recv(1)
  print&gt;&gt;sys.stderr, 'received %r' %binascii.hexlify(imageType)
  print&gt;&gt;sys.stderr, 'Unpacked: ', int(binascii.hexlify(imageType), 16)
  received = imageType.__len__()
  print&gt;&gt; sys.stderr, "Received: ", received 
  
  #Receive the image size
  imageSize = s.recv(4)
  print&gt;&gt;sys.stderr, 'received %r' %binascii.hexlify(imageSize)
  print&gt;&gt;sys.stderr, 'Unpacked: ', int(binascii.hexlify(imageSize), 16)
  received = imageSize.__len__()
  print&gt;&gt; sys.stderr, "Received: ", received 
  
  
  #Receive the image Data
  imageData = ''
  received =0
  while(received &lt; int(binascii.hexlify(imageSize), 16)):
    buffer = s.recv(4096)
    imageData += buffer
    received += buffer.__len__()
    print&gt;&gt; sys.stderr, "Received: ", received 
  
  img = Image.fromstring('RGB', (1280, 720), imageData, 'raw')
  
  #img = Image.open(StringIO(binascii.hexlify(imageData)))
  img = img.convert('RGB')
  img.save('out.png')
  
  #file = open('test.png', 'w');
  #file.write(imageData)
  #file.close()
  
  #When we receive the image, we send the acknowledgement
  s.send('OK')
  s.close()`enter code here`
  </code></pre>
  
  <p>But everytime I run the code it always get this kind of error</p>
  
  <pre><code>"Value error not enough Image Data"
  </code></pre>
  
  <p>And if change</p>
  
  <pre><code>img = Image.fromstring('RGB', (1280, 720), imageData, 'raw')
  </code></pre>
  
  <p>to</p>
  
  <pre><code>img = Image.fromstring('BRG', (1280, 720), imageData, 'raw')
  </code></pre>
  
  <p>I get this error:</p>
  
  <pre><code>Value error: Unrecognized mode,
  </code></pre>
  
  <p>Anyone knows how to solve this kind of problem?</p>