title: Python PIL bytes to Image
body: <pre><code>import PIL
  from PIL import Image
  from PIL import ImageDraw
  from PIL import ImageFont
  import urllib.request
  
  with urllib.request.urlopen('http://pastebin.ca/raw/2311595') as in_file:
      hex_data = in_file.read()
  print(hex_data)
  img = Image.frombuffer('RGB', (320,240), hex_data) #i have tried fromstring
  draw = ImageDraw.Draw(img)
  font = ImageFont.truetype("arial.ttf",14)
  draw.text((0, 220),"This is a test11",(255,255,0),font=font)
  draw = ImageDraw.Draw(img)
  img.save("a_test.jpg")
  </code></pre>
  
  <p>i m trying to convert binary to image,and then draw the text.but i get the error with:</p>
  
  <pre><code>img = Image.frombuffer('RGB', (320,240), hex_data) 
  raise ValueError("not enough image data")
  ValueError: not enough image data
  </code></pre>
  
  <p>i have uploaded the bytes string at here <a href="http://pastebin.ca/raw/2311595" rel="noreferrer">http://pastebin.ca/raw/2311595</a><br>
  and the picture size i can sure that is 320x240</p>
  
  <h2>ADDITIONAL</h2>
  
  <p>here is what i can sure the picture bytes string are from 320x240,just run the code will create a image from the bytes string</p>
  
  <pre><code>import urllib.request
  import binascii
  import struct
  
  # Download the data.
  with urllib.request.urlopen('http://pastebin.ca/raw/2311595') as in_file:
       hex_data = in_file.read()
  # Unhexlify the data.
  bin_data = binascii.unhexlify(hex_data)
  print(bin_data)
  # Remove the embedded lengths.
  jpeg_data = bin_data
  # Write out the JPEG.
  with open('out.jpg', 'wb') as out_file:
      out_file.write(jpeg_data)
  </code></pre>
  
  <h2>SOLVED, THIS IS THE CODE UPDATED</h2>
  
  <pre><code>from PIL import Image
  from PIL import ImageDraw
  from PIL import ImageFont
  import urllib.request
  import io
  import binascii
  
  data = urllib.request.urlopen('http://pastebin.ca/raw/2311595').read()
  r_data = binascii.unhexlify(data)
  #r_data = "".unhexlify(chr(int(b_data[i:i+2],16)) for i in range(0, len(b_data),2))
  
  stream = io.BytesIO(r_data)
  
  img = Image.open(stream)
  draw = ImageDraw.Draw(img)
  font = ImageFont.truetype("arial.ttf",14)
  draw.text((0, 220),"This is a test11",(255,255,0),font=font)
  draw = ImageDraw.Draw(img)
  img.save("a_test.png")
  </code></pre>