Evolution of galaxy shapes from prolate to oblate through compaction events