greeting
i own big number(more than 3000 image) of important images that i try to convert(encode) them from “jpg” to base64.
i find base64 already installed but it convert one file every time.
is there’s some app can convert all image that put together inside one folder?
Hi
A bash script that loops through the command… what is the command your running?
You can try this link and see if the solution presented works for you.
https://stackoverflow.com/questions/16918602/how-to-base64-encode-image-in-linux-bash-shell
Maybe try this if it helps
[https://www.imagemagick.org/script/formats.php
from that page](https://www.imagemagick.org/script/formats.php)|INLINE|RW|Base64-encoded inline image|The inline image look similar to inline:data:;base64,/9j/4AAQSk…knrn//2Q==. If the inline image exceeds 5000 characters, reference it from a file (e.g. inline:inline.txt). You can also write a base64-encoded image. Embed the mime type in the filename, for example, magick myimage inline:jpeg:myimage.txt.|
|—|—|—|—|
left the edit a bit late so
when on that linked page go to - Pseudo-image Formats, the second scroll box, scroll down to find the text mentioned before.
Hope this helps
I started the search with Converseen app
many thanks for you all, as am totally naive most shell code that i find can not understand them. other command line that i understand them do it file by file.
from the search i try edit next command line to:
1- go through each “JPG, PNG” file in some directory (i will use open in terminal for opening the folder in terminal).
2- encode every file “image” to base64, then add at the top (start) of result this string “data:image/jpeg;base64,”.
3- save the result in txt file hold same name the JPG or PNG image.
cat dan.jpg | base64 -w 0 > dan.txt
is there’s command line can do this?
otherwise is this script shell is correct (will do what i mention up)? if yes, is there’s some way to add “data:image/jpeg;base64,” at start of every generation base64 string before storage it in text file?
#!/bin/bash
FILES="$@"
for f in $FILES
do
if -f ${f}.jpg || ${f}.png ]
then
cat $f | base64 -w 0 > $f.txt
done
The script you posted I think a good start.
I would change it to:
#!/bin/bash
FILES="$@"
for f in $FILES; do
case "$f" in
*.png)
echo -n "data:image/png;base64," > "$f.txt"
base64 -i "$f" >> "$f.txt"
;;
*.jpg)
echo -n "data:image/jpeg;base64," > "$f.txt"
base64 -i "$f" >> "$f.txt"
;;
*)
echo "Skipping $f"
;;
esac
done
Did run it on a .png file and got:
> head -n3 Bloem2.png.txt
data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAAYQAAAEHCAIAAAAh+6byAAAgAElEQVR42jy82a5s2XUlNsaYa++I
c+7NlkwyqaJEqilBKrsAwwasAizYhg34yf4J/4J/yP9h+MFwI8koC4UqyaAa0qJEKpPM5vbnnIi9
5hh+WHGZDwkk8l5ExN5zzTm6ufg//0//493pfoySCkzHmQ4ByEZC4CAJqEGkZjqUjcwJAjBa9pww
If you do want the base64 string as one long line, run base64 with “-w 0”
1- many thanks for you, i read the code and understand it (you can say i understand the map of it, love the idea of using familiar terms ).
2- i go to the directory(folder that continue two image, make it for test). create file “imagetobase.sh”, take copy from your code and past it in to the file then save the code. and make the file executable.
3- double click on file (run it) but no result appears. so try run it from terminal using next steps.
4- open the directory in terminal(using open in terminal chose from right click drop menu).
5- in terminal ./imagetobase.sh, but also can not find any results.
did i do some thing wrong?
is the output will store in different directory?
thanks in advance
so i go back and run the script for image file “./imagetobase.sh car.jpg”, and it work.
is there’s some way to make this script automatically read all jpg files in directory and convert them all one by one to base64?
many thanks for all, really help me a lot.
the final code that convert all file inside some directory is:
#!/bin/bash
FILES="$@"
for f in /home/naive/Downloads/test/*; do
case "$f" in
*.jpg)
echo "data:image/png;base64," > "$f.txt"
base64 -w 0 "$f" >> "$f.txt"
;;
*.png)
echo "data:image/jpeg;base64," > "$f.txt"
base64 -w 0 "$f" >> "$f.txt"
;;
*)
echo "Skipping $f"
;;
esac
done
https://cdn.shopify.com/s/files/1/1951/6959/products/BoxTY198_front.jpg?v=1524954763
Good you got it working but the original script could already loop over a complete directory or a list of list thanks to bash filename globbing.
$ ls Bloem*.png
Bloem2.pngBloem.png
$ ./imagetobase.sh Bloem*.png
$ ls *.png.txt
Bloem2.png.txt Bloem.png.txt
without your help i never find it, many thanks.
- is the solution. in my case i run the script file directly once from inside the folder without give any argument, and second from outside with folder name as argument. and last one run it from terminal with one argument(specific file name).