Potential issue: some off-by-1 errors with LaTeX comment line numbers vs. line numbers in the array of Tex lines?
Seems like find_image_line_num subtracts 1 whereas get_tex_environment does not?
That should usually be fine because the line number + 1 will likely not be the \end{} command?