Search code examples
pythonimage-processingsavepython-imaging-librarycs50

Image.save giving AttributeError: 'NoneType' object has no attribute 'save'error


My code is giving me error while saving the image

the following is my code:

import sys
from PIL import Image, ImageOps
import os
import PIL

#suppurted formet
spfr = [".jpeg", ".jpg", ".png"]

#check CLA
if len(sys.argv) < 3:
    sys.exit("Too few command-line arguments")
elif len(sys.argv) > 3:
    sys.exit("Too many command-line arguments")
elif os.path.splitext(sys.argv[1])[1] != os.path.splitext(sys.argv[2])[1]:
     sys.exit("Not same extension")
elif os.path.splitext(sys.argv[1])[1] not in spfr:
    sys.exit("Not a image")

#open image
try:
    shirt = Image.open("shirt.png")
    old = Image.open(sys.argv[1])
except FileNotFoundError or PIL.UnidentifiedImageError:
    sys.exit()

shirt_size = shirt.size

old = ImageOps.fit(old, shirt_size)

new = old.paste(shirt, shirt)

new.save(sys.argv[2])

It gives me the following error:

Traceback (most recent call last):
  File "/workspaces/116355975/Python/shirt/shirt.py", line 32, in <module>
    new.save(
    ^^^^^^^^
AttributeError: 'NoneType' object has no attribute 'save'

What should I do

I was expexting it save the image to the new file


Solution

  • I have solved it by removing the "new" variable and not assigning ǫld.paste(shirt, shirt) to anything and changing new to old in new.save(sys.argv[2])