I haven’t tested this, but it should do what you want. All credit goes to @scotty65, I just added a line and modified another.
#!/bin/bash
# dpirename - a script to add the DPI value to the start of image files
# Set extglob which allows the use of "!" to exclude already renamed files from processing
shopt -s extglob
read -p "Image files in $PWD will be renamed with DPI. Do you want to proceed? " -n 1 -r
echo # (optional) move to a new line
if [[ $REPLY =~ ^[Yy]$ ]]; then
# only loop over files with extensions starting with "j" or "p" that have not already had the DPI added to their file name
for f in !(dpi_*.[jJ][pP]); do
# get the DPI of the main, not embedded thumbnail, image by finding the first instance of "Resolution:" & remove space at start of result
dpi=$(identify -units PixelsPerInch -verbose "${f}" | grep -m 1 Resolution: | cut -d: -f2 | sed -e 's/^[ \t]*//')
size=$(identify -verbose "${f}" | grep Geometry | grep -o -P '[0-9]+x[0-9]+')
# move the file unless the new name already exists (noclobber)
# For non-verbose processing change "-vn" to just "-n"
mv -vn "${f}" "dpi_${dpi}_size_${size}_${f}";
done
else
echo "Script is exiting"
fi
# Unset extglob
shopt -u extglob
exit 0