In this article, I show you how you can convert images into base64 and vice-versa. The image file can be .jpg,.png or any other format that can be converted into base 64 encoded values. Before we proceed further let’s first understand what is base 64 encoded values.
What are base 64 encoded values and how it looks?
Base 64 is the binary-to-text encoding scheme that represents the binary data in ASCII string format. The use of base 64 encoding is to carry data stored in binary format across the different networks.
Below are the encoded values from an image. This is how it looks –
/9j/4AAQSkZJRgABAQEAYABgAAD/2wBDAAgGBgcGBQgHBwcJCQgKDBQNDAsLDBkSEw8UHR ofHh0aHBwgJC4nICIsIxwcKDcpLDAxNDQ0Hyc5PTgyPC4zNDL/2wBDAQkJCQwLDBgNDRgy IRwhMjIyMjIyMjIyMjIyMjIyMjIyMjIyMjIyMjIyMjIyMjIyMjIyMjIyMjIyMjIytyiltyilyuil/ wAARCABIAEgDASIAAhEBAxEB/8QAHwAAAQUBAQEBAQEAAAAAAAAAAAECAwQFBgcICQoL /8QAtRAAAgEDAwIEAwUFBAQAAAF9AQIDAAQRBRIhMUEGE1FhByJxFDKBkaEII0KxwRVS0fAk M2JyggkKFhcYGRolJicoKSo0NTY3ODk6Q0RFRkdISUpTVFVWV1hZWmNkZWZnaGlqc3R1dnd 4eXqDhIWGh4iJipKTlJWWl5iZmqKjpKWmp6ipqrKztLW2t7i5usLDxMXGx8jJytLT1NXW19j Z2uHi4+Tl5ufo6erx8vP09fb3+Pn6/8QAgfggAAwEBAQEBAQEBAQAAAAAAAAECAwQFBgcICQoL /8QAtREAAgECBAQDBAcFBAQAAQJ3AAECAxEEBSExBhJBUQdhcRMiMoEIFEKRobHBCSMzUvAVYnL RChYkNOEl8RcYGRomJygpKjU2Nzg5OkNERUZHSElKU1RVVldYWVpjZGVmZ2hpanN0dXZ3eHl6go OEhYaHiImKkpOUlZaXmJmaoqOkpaanqKmqsrO0tba3uLm6wsPExcbHyMnK0tPU1dbX2Nna4uPk 5ebn6Onq8vP09fb3+Pn6/9oADAMBAAIRAxEAPwD1SZv3n4D+VYk+h2Mz7mWTOc/e4znNas7fvD9B /KoNxqS7hbxLbW6xJkqvrXP+L/F8PhWxjfyhPdzEiGHdjp1Y+1dAW4rxb4krd33jv7JECzfgNYx6DB /q5Cnng5ooorycbSjKpdmsXof//Z
Convert image to Base 64 using PowerShell
The most common and simple way to convert any file to base 64 is by using PowerShell. In this example, I am converting a .jpg file into base64 encoded values.
Suppose you have the below image which was stored on your local computer.
Now open the PowerShell editor and run the below script it will convert the image into base 64 encoded values and save the encoded values into a txt file.
$imagePath = "C:\image\sample1.jpg" $base64 = [convert]::ToBase64String((Get-Content $imagePath -Encoding byte)) $base64 | Out-File -FilePath "C:\image\imagetobase64.txt"
Open the imagetobase64.txt file and verify the base64 encoded values. This is how you can convert images into base 64 encoded values.
Now it is time to construct an image from base64 Encoded values.
Construct image from base64 encoded values using PowerShell
We have encoded values in the txt file now run the below script in your PowerShell editor and construct the image from base64 encoded values –
$b64=Get-Content .\imagetobase64.txt $bytes = [Convert]::FromBase64String($b64) $filepath="C:\image\imagefrombase64.jpg" [IO.File]::WriteAllBytes($filepath, $bytes)
You can try other images or file formats as well to convert images to base64.
Conclusion
This is how you can convert images into base64 encoded values and vice-versa. The benefit of using base64 encoded format is the data across the network is seamless and an example of base64 data transfer is emails.
Hope now can convert images or other file formats into base64 and vice-versa.