You can use get_the_author_meta
to retrieve the username for the author of the current post in the loop. You could then use that in the image URL.
<?php _e('Posted by'); ?>
<a href="https://wordpress.stackexchange.com/questions/147891/<?php echo get_author_posts_url(get_the_author_meta("ID')); ?>" rel="author">
<img src="/wp-content/themes/mytheme/img/<?php echo esc_attr( get_the_author_meta( 'user_login' ) ); ?>.png" alt="author" />
<?php the_author_meta('display_name'); ?>
</a>
You can also use get_the_author_meta
if the image file name is a combination of the author’s first and last name, by passing ‘first_name’ or ‘last_name’ to the function to retrieve these values.