State preparation by photon filtering