Forum Discussion

vipulk10's avatar
vipulk10
New Contributor III
8 years ago

Way to base 64 encode images

hi all,
I am figuring out a way to base64 encode images . please help. I think base64 function is for text and not for images

4 Replies

  • tstack's avatar
    tstack
    Former Employee

    You can use the Binary to Document Snap to base64-encode a binary stream into a document with a property that contains the encoded value.

    • vipulk10's avatar
      vipulk10
      New Contributor III

      tried this but i see difference in encoded values from using snaplogic functions and different online encoders available. may be a lot other things are getting encoded apart from image data. ie $content availble from file redaer has much more data than the actual image

  • del's avatar
    del
    Contributor III

    Long before the Binary to Document snap was developed and available, I used a Script snap. I should look at the Binary to Document snap to see if it improves my pipeline, but if that snap doesn’t serve your purpose, here’s a snippet of Python that may help. You need to import base64.
    (note: this code is 3 years old, and I’m not a Pyhton guru, so certain code & exception handling may be improved)

            try:
                with open(filepath2, "rb") as f:
                    bytes = ""
                    byte = f.read(1)
                    while byte != b"":
                        bytes = bytes + byte
                        byte = f.read(1)
                    if byte != b"":
                        bytes = bytes + byte
                    output = base64.b64encode(bytes)
                    data2.put('IMAGE',output)
                    self.output.write(data2)		
            except Exception as e:
                map = java.util.HashMap()
                map.put('FILENAME',filepath2)
                map.put('Exception',str(e.errno) + ": " + e.strerror)
                self.error.write(map)