How to show only posts with images?

No need for post IDs what has no thumbnail. Use meta query to get only those what has thumbnail.

Add meta query

function get_only_posts_with_images( $query ) {

  if ( $query->is_home() && $query->is_main_query() )  {
      $query->set( 'meta_query', array( array( 'key' => '_thumbnail_id' ) ) );
  }

}
add_action( 'pre_get_posts', 'get_only_posts_with_images' );

Or use custom query.

$query = "
  SELECT posts.* 
  FROM $wpdb->posts AS posts
  INNER JOIN $wpdb->posts AS attachment 
    ON attachment.`post_parent`=posts.`ID` 
      AND attachment.`post_type`='attachment' 
  WHERE posts.`post_type`='post'
";

$posts_with_images = $wpdb->get_results( $query, OBJECT );