Getting "cannot write mode P as JPEG" while operating on JPG image
You need to convert the image to RGB mode.
Image.open('old.jpeg').convert('RGB').save('new.jpeg')
This answer is quite old, however, I thought I will put a better way to do the same by checking for the mode before doing the conversion:
if img.mode != 'RGB':
img = img.convert('RGB')
This is required to save your image in JPEG format.