Calculation of the pion-nucleus optical potential