I just had the same problem and wanted to clarify the solution, in case anyone else stumbles upon this.
The option that Mathieu was pointing to is not in the BuddyPress option but in the WordPress options: Settings > Discussion > Avatar.
I normally disable avarats on all sites and was also wondering why I didn’t have that option now on my new BP site.
You need to replace $group_id with the ID of the group you want to assign users to.
I have not tested the code when inviting members as a non-admin. That might be a limitation. There is an optional is_confirmed argument that can be submitted with groups_invite_users. Maybe, setting it to true helps in such cases.