Make the input image's numpy array the right format for preprocess() by adding a conversion to RGB in the image read statement:
img = np.float32(PIL.Image.open(sys.argv[1]).convert('RGB'))
Otherwise, it's pretty easy to find JPG or other types of files that crash the script.
It might be less trouble for a user if the image write format were left to PIL to intuit from the output file name's extension, too.
(Hope this is readable. Browser not supported by github.)