Solved

Converting Base64 encoded field to raster


I've been searching everywhere and coming up empty. I'm loading data from a SQL Server database that users can use to store images as well. The image is stored in a field called "photo" and it is stored in base64.  You can see an example of the base64 data that's being stored in the field (attached). Using online tools and copy pasting the contents of this base64 data does render a png image, however, I cannot get FME to convert it into something that I can export to a folder or even store as a regular raster in the table. 

Wondering if anyone can provide some clarification. 

Every base64 image starts with the following: 

data:image/png;base64,

I tried trimming that bit and taking just the encoding and running it through a text decoder. In the "character encoding for binary data" I chose "Binary (fme-binary)". Then i used an attributefilewriter to write the data into a folder. However, the resulting images cannot be opened using any image viewer.

Any help on this matter would be appreciated. 

icon

Best answer by ebygomm 1 June 2022, 13:04

View original

10 replies

Userlevel 6
Badge +32

One way to do this :

  • BinaryDecoder to decode from base64 to binary.
  • AttributeFileWriter to write the file from binary.

Attached workspace demonstrating this.

One way to do this :

  • BinaryDecoder to decode from base64 to binary.
  • AttributeFileWriter to write the file from binary.

Attached workspace demonstrating this.

Appreciate the reply!

I follow the process you've outlined and it's still not rendering the image. I ran the workbench you provided as well and it shows this error when I go to open it in windows image viewer (see attached). Not sure if you're seeing the same on your end.

 

Thanks

Userlevel 6
Badge +32

Appreciate the reply!

I follow the process you've outlined and it's still not rendering the image. I ran the workbench you provided as well and it shows this error when I go to open it in windows image viewer (see attached). Not sure if you're seeing the same on your end.

 

Thanks

Strange, when I run it and open the result.png I see a ceiling with a light in it.

 

Are you sure this is not caused by the image viewer? I checked the result with windows Photos, IrfanView, Paint, Paint.net... all resulting in a good image.

 

I did use the data you provided without the "data:image/png;base64," part.

Userlevel 6
Badge +32

Appreciate the reply!

I follow the process you've outlined and it's still not rendering the image. I ran the workbench you provided as well and it shows this error when I go to open it in windows image viewer (see attached). Not sure if you're seeing the same on your end.

 

Thanks

This is my result file.

Appreciate the reply!

I follow the process you've outlined and it's still not rendering the image. I ran the workbench you provided as well and it shows this error when I go to open it in windows image viewer (see attached). Not sure if you're seeing the same on your end.

 

Thanks

That's the correct image. Even your workflow. I connected teh attribute creator to the attributefilewriter. Set the source attribute as "base64" and set a destination with a png file format. The file gets output properly but just not displaying. I did have to change the python compatibility to "ESRI ArcGIS Python 2.7". Your workbench was set to "ESRI ArcGIS Python 3.7". I wonder if that's what's causing the problem.

Appreciate the reply!

I follow the process you've outlined and it's still not rendering the image. I ran the workbench you provided as well and it shows this error when I go to open it in windows image viewer (see attached). Not sure if you're seeing the same on your end.

 

Thanks

Does the attached image work for you?

Userlevel 6
Badge +32

Appreciate the reply!

I follow the process you've outlined and it's still not rendering the image. I ran the workbench you provided as well and it shows this error when I go to open it in windows image viewer (see attached). Not sure if you're seeing the same on your end.

 

Thanks

Nope. It is a different result from what I have. If I try to open it using Notepad++ I just see the Base64 string.

 

What version FME do you use? I can't get my head around the fact that you are using my workspace and get a different result, this must be a version related issue?

Userlevel 1
Badge +21

I connected teh attribute creator to the attributefilewriter. 

 

It sounds like you have downloaded the workspace and opened with an older version and are missing the binary decoder. The workspace should look like this

 

imageNote that i've also had to add an attributeexposer as the binary decoder doesn't seem to expose the created attribute in 2019

Appreciate the reply!

I follow the process you've outlined and it's still not rendering the image. I ran the workbench you provided as well and it shows this error when I go to open it in windows image viewer (see attached). Not sure if you're seeing the same on your end.

 

Thanks

I think it might a version related issue. I'm using FME Desktop 2021.1.2.0. ESRI Edition.

When I opened your workbench, it did warn me saying that yours was created using a newer version.

So i created your workflow exactly as you had done it in a brand new workspace and it's still not exporting properly. Very strange. Can you set the character encoding for the workspace anymore? I noticed in previous versions of FME you can set the character encoding but I don't see that option under workspace parameters anymore.

I connected teh attribute creator to the attributefilewriter. 

 

It sounds like you have downloaded the workspace and opened with an older version and are missing the binary decoder. The workspace should look like this

 

imageNote that i've also had to add an attributeexposer as the binary decoder doesn't seem to expose the created attribute in 2019

Wow, this worked. I wasn't using the binaryDecoder at all. I used a text decoder and attributefilewriter but none seemed to work. I think what was messing me up was the missing AttributeExposer as well. Just like yours, the binary attribute coming from the Decoder is not being picked up by the AttributeFileWriter. Thank you so much for this! Appreciate it.

 

@nielsgerrits​ This method seems to work.

Reply