Slicing image then save

Hi. I have a large image I want to slice up into even squares then save each slice as a PNG. I created a lua script but it’s a bit off (the pixels are out) and wondered if there was a better way to do it?