You can fix the image file name by correcting some of the nested calls inside \includegraphics:

\documentclass{article}
\usepackage{graphicx,etoolbox}
\makeatletter
\patchcmd{\Gin@setfile}% <cmd>
{\ProvidesFile}% <search>
{\xdef\imgfilename{
{}{}% <success><failure>
\makeatother
\begin{document}
\begin{figure}
\centering
\includegraphics[width=.5\linewidth]{example-image}
\caption{Image: \imgfilename}
\end{figure}
\end{document}
\imgfilenamewill contain the file name used in \includegraphicsonly after its use.
source
share