The photograph on the right is awfully interesting - the uniform is indeed identical to the service uniforms of officers of the Spanish Legion....but there is one big problem
First this is a photograph of officer and soldiers of the Spanish Legion in Morocco in the early 1920s - showing identical an uniform (including the characteristic pockets of the jacket)

Now the problem: The Spanish legion was created only in 1920 (though the decision to create it was taken in 1919), since then the Spanish troops wore a forage cape inherited from the 19th century.
However the uniform itself is compatible with Spanish Colonial uniforms of the period and the forage cap "gorro de cuartel" (the future "chapiri" of the Spanish Legion) was already in service in the Spanish army between 1876 and 1887 (thence is name of Gorro isabelino) to reappear officially in 1919 - It is therefore possible that Spanish troops in Morocco already used before 1919 this cap as an unofficial forage cap - but I have no documentation to support this hypothesis.
This si a nice article (In Spanish...) on the history of the chapiri:
https://generaldavila.com/tag/el-gorro-de-cuatel-isabelino/
and some photos of the forge caps of the Spanish army in the ww1 period and after 1919
If this is indeed a Spanish officer, it is worth recording that in 1917 Spanish general Don Dámaso Berenguer Fusté, who was leading the Spanish troops in Morocco during this period, before becoming Ministry of War then High Commissaire of Morocco, visited the French front and the French military industries, he was accompanied by a number of Artillery officers, so the presence of "colonial uniforms" is no out of place.