Another alternative is to use pishrink
PiShrink is a bash script that automatically shrinkshrinks a pi image that will then resize to the max size of the SD card on boot. This will make putting the image back onto the SD card faster and the shrunk images will compress better.
The algorithm mounts the image on a loop device, executes e2fsck, calculates the minimum space required by the second partition, adds some spare space and finally executes resize2fs to shrink the second partition to the calculated minimum size.