title: How to fetch bytes from SQL Server Database and Convert to Image in Python
body: <p>--    I'm loading test data into my SQL Server Database using python and was able to successfully take images and break them down into bytes and store them in the database, but when trying to fetch back the bytes and decode it to save it as a new file type, all i get is a blank image file. Not sure what i am doing wrong here...</p>
  
  <p>--    I've tried several iterations using base64 from other tutorials and similar questions, but cant seem to find one that will solve my problem.</p>
  
  <pre><code>SQLCommand = ("SELECT Photo FROM Validation")
  
  
  cursor.execute(SQLCommand)
  data = cursor.fetchone()[0]
  
  
  
  
  image_64_decode = base64.decodebytes(data)
  image_result = open('booking.png', 'wb')
  image_result.write(image_64_decode)
  image_result.close()
  
  
  connection.close()
  
  The expected result is that I should be able to fetch the bytes from the database which the database column is varbinary(max) the equivalent of bytes in python. once the bytes are fetched using the script in python it should save a file as booking.png which should replicate the image i stored in the database.
  
  When i run the script i don't get an error, and in fact it saves a file, but the file is empty containing 1kb and does not reproduce the image. Not sure where i am going wrong, but it seems like it's not properly fetching the bytes.
  </code></pre>