Linux image to base64

How to base64 encode image in linux bash / shell

You can run a base64 encoded string in bash by piping the output of the base64 package for coreutils to bash. For example the command base64 -D

You can encode any text data by using base64 in the command line. When you want to encode any data using base64 then using -e or –encode option is optional. So, if you don’t mention any option with base64 then it will work for encoding.

To base64 encode string you can pipe an echo command into the base64 command-line tool. To ensure no extra, hidden characters are added use the -n flag. Without the -n flag you may capture a hidden characters, like line returns or spaces, which will corrupt your base64 encoding.

You need to use cat to get the contents of the file named ‘DSC_0251.JPG’, rather than the filename itself.

test="$(cat DSC_0251.JPG | base64)" 

However, base64 can read from the file itself:

Encode

On Linux

echo "data:image/jpeg;base64,$(base64 -w 0 DSC_0251.JPG)" 
base64 -w 0 DSC_0251.JPG > DSC_0251.JPG.base64 
IMAGE_BASE64="$(base64 -w 0 DSC_0251.JPG)" 
IMAGE_BASE64="data:image/jpeg;base64,$(base64 -w 0 DSC_0251.JPG)" 

On OSX

On OSX, the base64 binary is different, and the parameters are different. If you want to use it on OSX, you should remove -w 0 .

echo "data:image/jpeg;base64,$(base64 DSC_0251.JPG)" 
base64 DSC_0251.JPG > DSC_0251.JPG.base64 
IMAGE_BASE64="$(base64 DSC_0251.JPG)" 
IMAGE_BASE64="data:image/jpeg;base64,$(base64 DSC_0251.JPG)" 

Generic OSX/Linux

As Shell Function

@base64() < if [[ "$" = darwin* ]]; then # OSX if [ -t 0 ]; then base64 "$@" else cat /dev/stdin | base64 "$@" fi else # Linux if [ -t 0 ]; then base64 -w 0 "$@" else cat /dev/stdin | base64 -w 0 "$@" fi fi > # Usage @base64 DSC_0251.JPG cat DSC_0251.JPG | @base64 

As Shell Script

Create base64.sh file with following content:

#!/usr/bin/env bash if [[ "$" = darwin* ]]; then # OSX if [ -t 0 ]; then base64 "$@" else cat /dev/stdin | base64 "$@" fi else # Linux if [ -t 0 ]; then base64 -w 0 "$@" else cat /dev/stdin | base64 -w 0 "$@" fi fi 
./base64.sh DSC_0251.JPG cat DSC_0251.JPG | ./base64.sh 

Decode

Get you readable data back:

base64 -d DSC_0251.base64 > DSC_0251.JPG 

There is a Linux command for that: base64

base64 DSC_0251.JPG >DSC_0251.b64 

To assign result to variable use

If you need input from termial, try this

lc=`echo -n "xxx_$_iOS" | base64` 

-n option will not input «\n» character to base64 command.

file="DSC_0251.JPG" type=$(identify -format "%m" "$file" | tr '[A-Z]' '[a-z]') echo "data:image/$type;base64,$(base64 -w 0 "$file")" 

To base64 it and put it in your clipboard:

file="test.docx" base64 -w 0 $file | xclip -selection clipboard 

Источник

How to encode and decode data in base64 and base64URL by using unix commands?

Recent versions of coreutils include basenc(1) which supports several different encodings. From its help screen:

--base64 same as 'base64' program (RFC4648 section 4) --base64url file- and url-safe base64 (RFC4648 section 5) --base32 same as 'base32' program (RFC4648 section 6) --base32hex extended hex alphabet base32 (RFC4648 section 7) --base16 hex encoding (RFC4648 section 8) --base2msbf bit string with most significant bit (msb) first --base2lsbf bit string with least significant bit (lsb) first --z85 ascii85-like encoding (ZeroMQ spec:32/Z85); when encoding, input length must be a multiple of 4; when decoding, input length must be a multiple of 5 

Here is a string that illustrates the difference:

$ printf "%s" "$s" | xxd -b -c1 | cut -d' ' -f2 | nl 1 01111000 2 01110011 3 00111111 4 00111110 5 00111110 6 00111110 

And as 6 bit blocks (as base64 reads the data):

$ printf "%s" "$s" | xxd -b -c1 | cut -d' ' -f2 | tr -d '\n' | fold -w6 | nl 1 011110 2 000111 3 001100 4 111111 5 001111 6 100011 7 111000 8 111110 

Note that block 4 and block 8 map to / and + respectively (Base64 table on Wikipedia):

Читайте также:  Linux pci device list

This is the same suggestion as @jps but shorter. Also remember that echo by default always adds newline at the end, so when you want to encode it, you must add -n .

echo -n "Some_data_to_be_converted" | base64 | tr '/+' '_-' | tr -d '=' 

Decoding it back with bulit-in bash tools is more complicated as I didn’t find an easy way to pad the string back with ‘=’ so that the length will dividable by 4. Probably can be done with awk but I didn’t dig deep enough. If you have local ruby it becomes trivial:

2.6.2 > require 'base64' 2.6.2 > Base64.urlsafe_encode64('test', padding: false) => "dGVzdA" 2.6.2 > Base64.urlsafe_decode64('dGVzdA') => "test" 

I took it as a challenge to come up with a bash or standard unix commands to pad the string back in order to decode: stackoverflow.com/questions/58957358/…

Adding on to the answer by Kaplan Ilya, here is a command using standard linux/unix commands that can decode base64url , including handling missing padding.

Note: some versions of base64 can handle missing padding, such as Mac/BSD base64 -D . However, GNU base64 -d requires correct padding.

Also, I used the test string ~~~. instead of the one in the original question Some_data_to_be_converted , so that it will generate + , / , = characters.

text='~~~. ' # encode base64 echo "$text" | base64 # fn5+Pz8/Cg== # encode base64url base64url=$( echo "$text" | base64 | tr '/+' '_-' | tr -d '=' ) echo "$base64url" # fn5-Pz8_Cg # decode base64url echo "$base64url"==== | fold -w 4 | sed '$ d' | tr -d '\n' | tr '_-' '/+' | base64 -d # ~~~. 

Explanation of the decode base64url commands:

  • echo «$str»==== appends 4 equal signs
  • fold -w 4 split every 4 characters into separate lines
  • sed ‘$ d’ deletes the last line (the extraneous padding)
  • tr -d ‘\n’ joins all lines. Now the padding is correct.
  • tr ‘_-‘ ‘/+’ converts _ to / , — to + .

(Side note: if you’re wondering why not use tr ‘-_’ ‘+/’ since that would be in alphanumeric order, it’s because that will result in invalid option because it thinks -_ is an option. You could do tr — ‘-_’ ‘+/’ , but it’s easier just to swap the order.)

Читайте также:  Kali linux spoof dns

If you already have a base64 encoded string you just need to replace «+» with «-» and «/» with » _» to get a base64url encoded string. To achieve this, you can use the following command:

echo Some_data_to_be_converted | base64 | sed ‘s/+/-/g; s,/,_,g’

Base64 encoding maps the input bytes (8 bit) to a 6 bit representation. 4 base64 characters can encode 4*6=24 bits, which equals 3 bytes. Whenever the number of bytes in your input can’t be divided by 3, padding is required according to the standard.

As the = character is used for key-value pairs in URLs, you can’t use it directly for padding if you intend to use the encoded value in an URL. You can either just ommit the padding, because most implementations will still work and just ignore the 2 or 4 unused bits on the end. Or, if the receiver really needs padding, you have to replace the = by it’s URL safe representation %3d .

Источник

How to base64 encode image in linux bash / shell?

Base64 encoding is a process of converting binary data to a limited character set of 64 characters. It is often used to embed images in HTML, CSS, or JavaScript, or anything else that is part of a text-based format. In this tutorial, we will show you how to encode an image to base64 in Linux bash or shell.

Method 1: Using the ‘base64’ command

To base64 encode an image in Linux Bash/Shell using the base64 command, follow these steps:

  1. Open the terminal and navigate to the directory where the image is located.
  2. Use the base64 command to encode the image by running the following command:
base64 image.jpg > encoded-image.txt
base64 -d encoded-image.txt > decoded-image.jpg

Here’s an example of how to encode an image named example.png and save the output to a file named encoded.txt :

$ base64 example.png > encoded.txt

And here’s an example of how to decode the base64 encoded string from the file encoded.txt and save it as decoded.png :

$ base64 -d encoded.txt > decoded.png

That’s it! You can now use the base64 command to encode and decode images in Linux Bash/Shell.

Method 2: Using the ‘openssl’ command

To base64 encode an image in Linux bash/shell using the ‘openssl’ command, follow these steps:

  1. First, navigate to the directory containing the image you want to encode. You can do this by using the ‘cd’ command followed by the path to the directory.
  2. Once you’re in the directory, use the following command to base64 encode the image using ‘openssl’:
openssl base64 -in image_name> -out output_file_name>

Replace » with the name of the image file you want to encode, and » with the name you want to give to the output file. This will create a new file in the current directory with the base64-encoded image.

openssl base64 -in image_name> | less

This will display the encoded image in the terminal, but you won’t be able to save it as a file.

  1. To decode the base64-encoded image back to its original format, you can use the following command:
openssl base64 -d -in base64_encoded_file> -out output_image_file>

Replace » with the name of the file containing the base64-encoded image, and » with the name you want to give to the output image file. This will create a new file in the current directory with the decoded image.

That’s it! You now know how to base64 encode and decode images in Linux bash/shell using the ‘openssl’ command.

Method 3: Using the ‘python’ command

To base64 encode an image in Linux using the ‘python’ command, you can follow these steps:

  1. Open the terminal and navigate to the directory where the image is located.
  2. Use the following command to encode the image in base64:
python -c "import base64; with open('image.jpg', 'rb') as image_file: encoded_string = base64.b64encode(image_file.read()); print(encoded_string)"

In this command, replace ‘image.jpg’ with the name of your image file.

Here’s an explanation of the code used in the command:

  • import base64 : This imports the base64 module in Python, which provides functions to encode and decode data in base64.
  • with open(‘image.jpg’, ‘rb’) as image_file : This opens the image file in binary mode and creates a file object named ‘image_file’. The ‘with’ statement ensures that the file is closed after the block of code is executed.
  • encoded_string = base64.b64encode(image_file.read()) : This reads the contents of the file and encodes them in base64 using the b64encode() function from the base64 module. The encoded string is stored in the ‘encoded_string’ variable.
  • print(encoded_string) : This prints the encoded string in the terminal.

That’s it! You can use this command to encode any image in base64 using the ‘python’ command in Linux.

Источник

How to base64 encode image in linux bash / shell

You need to use cat to get the contents of the file named ‘DSC_0251.JPG’, rather than the filename itself.

test="$(cat DSC_0251.JPG | base64)"

However, base64 can read from the file itself:

Encode

On Linux

echo "data:image/jpeg;base64,$(base64 -w 0 DSC_0251.JPG)"
base64 -w 0 DSC_0251.JPG > DSC_0251.JPG.base64
IMAGE_BASE64="$(base64 -w 0 DSC_0251.JPG)"
IMAGE_BASE64="data:image/jpeg;base64,$(base64 -w 0 DSC_0251.JPG)"

On OSX

On OSX, the base64 binary is different, and the parameters are different. If you want to use it on OSX, you should remove -w 0 .

echo "data:image/jpeg;base64,$(base64 DSC_0251.JPG)"
base64 DSC_0251.JPG > DSC_0251.JPG.base64
IMAGE_BASE64="$(base64 DSC_0251.JPG)"
IMAGE_BASE64="data:image/jpeg;base64,$(base64 DSC_0251.JPG)"

Generic OSX/Linux

As Shell Function

@base64() < if [[ "$ " = darwin* ]]; then # OSX if [ -t 0 ]; then base64 "$@" else cat /dev/stdin | base64 "$@" fi else # Linux if [ -t 0 ]; then base64 -w 0 "$@" else cat /dev/stdin | base64 -w 0 "$@" fi fi># Usage@base64 DSC_0251.JPGcat DSC_0251.JPG | @base64

As Shell Script

Create base64.sh file with following content:

#!/usr/bin/env bashif [[ "$ " = darwin* ]]; then # OSX if [ -t 0 ]; then base64 "$@" else cat /dev/stdin | base64 "$@" fielse # Linux if [ -t 0 ]; then base64 -w 0 "$@" else cat /dev/stdin | base64 -w 0 "$@" fifi
./base64.sh DSC_0251.JPGcat DSC_0251.JPG | ./base64.sh

Decode

Get you readable data back:

base64 -d DSC_0251.base64 > DSC_0251.JPG 

Источник

Оцените статью
Adblock
detector