title: Converting data:image from base64 to JPEG in Python
body: <p>From the client's POST submission, we are successfully receiving an image in the following format, <code>data:image/jpeg;base64,/9j/....</code> and an image file is being generated by the following code:</p>
  <pre><code>@app.route('/submission', methods=('GET', 'POST'))
  def submission():
      
      if request.method == 'POST':
          
          raw_image = request.form['file']
          #Problem Starts Here
          # this doesn't work either: 
          # raw_image = raw_image.replace(&quot;data:image/jpeg;base64,/9j/&quot;, '');
          with open(UPLOADED_IMAGES_DEST+'/image.jpeg', 'wb') as fh:
              fh.write(base64.decodebytes(raw_image))
          #Problem Ends Here
          return jsonify(request.form)
          
      return render_template('submission.html')
  </code></pre>
  <p>However, the image cannot be opened as it's an &quot;Invalid or Unsupported Format&quot;. I'm trying to improve the conversion process so it will produce a viewable JPEG image.</p>
  <h3>Beginning of <code>raw_image</code>:</h3>
  <p><code>data:image/jpeg;base64,/9j/4AAQSkZJRgABAQAAAQABAAD/2wBDAAMCAgMCAgMDAwMEAwMEBQgFBQQEBQoHBwYIDAoMDAsKCwsNDhIQDQ4RDgsLEBYQERMUFRUVDA8XGBYUGBIUFRT/2wBDAQMEBAUEBQkFBQkUDQsNFBQUFBQUFBQUFBQUFBQUFBQUFBQUFBQUFBQUFBQUFBQUFBQUFBQUFBQUFBQUFBQUFBT/wAARCADwAUADASIAAhEBAxEB/8QAHQAAAgIDAQEBAAAAAAAAAAAABQYEBwIDCAEACf/EAEwQAAEDAwMCBAQEAwUFBQQLAAECAxEABAUGEiExQQcTUWEIInGBFDKRoRVCUiOxwdHwJGJyouEJFjOSwhdDgvEmU2ODk7LDxNLU4v/EABsBAAMBAQEBAQAAAAAAAAAAA</code>...</p>
  <h3></h3>