title: Convert image into hexadecimal format with Python
body: <p>I have a jpg file under the <code>tmp</code> folder.</p>
  
  <pre><code>upload_path = /tmp/resized-test.jpg
  </code></pre>
  
  <p>I have been using the codes below:</p>
  
  <p>Method 1</p>
  
  <pre><code>with open(upload_path, "rb") as image_file:
      encoded_string = base64.b64encode(image_file.read())
  </code></pre>
  
  <p>Method 2</p>
  
  <pre><code>def imgToHex(file):
      string = ''
      with open(file, 'rb') as f:
          binValue = f.read(1)
          while len(binValue) != 0:
              hexVal = hex(ord(binValue))
              string += '\\' + hexVal
              binValue = f.read(1)
      string = re.sub('0x', 'x', string) # Replace '0x' with 'x' for your needs
      return string
  imgToHex(upload_path)
  </code></pre>
  
  <p>But none of them are working as I want.</p>