Issue
We have been capturing and sending files to a REST server and saving them to PNG extensions from a toolbar, now have nearly a half a million files.
Recently we found they have been actually saving as base64 streamed files and not PNG format.
I have a python script that can convert them but is not recursive and connot use wildcards.
ImageMagick says the base64 files are too big(5K max) so thats out for the conversion.
IE; /bin/base64-to-png.py <base64_file_name>.png <output_name.png>
The directory paths /mnt/s3/pages/ where "pages" has several thousand numbered folders.
IE; /mnt/s3/pages/100
"pages" subfolders are never more than 1 folder deep.
The files are typically 5540_thumb.png, 5540_snapshot.png and 5540_crop.png
where the 5540 is the capture number.
I need a BASH script wrapper to make it recusive accross the folders, copy all originals to backup files (filename.b64) and pass wildcards as proper path/names to the python script, convert the files and leave the output file in the same respective folder then use mogrify to compress them.
IE; base64-to-png.sh *_snapshot.b64 *_snapshot.png <compression number>
should
cp <file_name.png> <file_name>.b64
/bin/base64-to-png.py <file_name.png> <file_name.png>
mogrify -quality <compression number> -format png <file_name.png>
Solution
Try this script
#!/bin/bash
SEARCHPATH=$1
COMNUM=$2
FINDEXT="*.png"
for f in `find $SEARCHPATH -type f -name $FINDEXT` ; do
#just get files name without extension
fname=`echo $f | cut -d'.' -f1`
#perform operations
cp $f ${fname}.b64
/bin/base64-to-png.py $f $f
mogrify -quality $COMNUM -format png $f
done
you can call it as
$ base64-to-png.sh /somepath/tofind 100
param 1 : path where to find `.png` files
param 2 : compression number.
This assumes that /bin/base64-to-png.py
can work on absolute path e.g. /somepath/somedir/somefile.png
not just somefile.png
Warning: no QA done, use it on your risk!
Answered By - Rohan Answer Checked By - Willingham (WPSolving Volunteer)