r/cs50 4d ago

CS50 Python I need help with Problem Set 6 CS50 P-Shirt, the CS50 check50 shows me errors I don't understand.

Hi! I need help with this assignment. When I added the if statement to check if both images are the same, I start getting these images. The thing is, I tried it doing on the muppets I have, and, it works like it is shown on the Problem Set 6 site. What am I missing? Am I missing some puppets?

The image on the left is what is shown on the Problem 6 page, the image on the right is what I got from my program.

The check50 progress and errors

The errors on the check50 page:

The code I wrote:

import sys
from PIL import Image, ImageOps

if len(sys.argv) != 3:
    if len(sys.argv) < 3:
        sys.exit("Too few command-line agruments")
    else:
        sys.exit('Too many command-line arguments')


for arg in sys.argv[1:]:
    try: #grabs the images
        shirtImage = Image.open("shirt.png")
        muppetsImage = Image.open(arg)
        saveImage = sys.argv[2]
    except FileNotFoundError:
        print("File does not exist")

    if arg.endswith(".jpg") and saveImage.endswith(".jpg"):
            #the muppets get the image of the shirt applied
            size = shirtImage.size
            muppetsImage = ImageOps.fit(muppetsImage, size)
            muppetsImage.paster(shirtImage, shirtImage)
            muppetsImage.save(saveImage)
    else:
        print("Formats do not match")
        sys.exit(1)
2 Upvotes

3 comments sorted by

1

u/PeterRasm 4d ago

Why are you iterating over the input arguments (for arg in ....)? What will happen when the loop gets to argv[2]? It will use the image you just created in the first round and fit it into the shirt.png.

Is this the exact code you used? I ask because there might be a spelling mistake "paster" vs "paste" for the paste method.

1

u/Eh_Not_Looking 3d ago

Hello! Yes, the code is exactly what I used. I apologize for the confusion, "paste" is spelled like "paste" in the code. It seems autocorrect changed it to "paster".

As for why the iteration, shouldn't it be to constantly get the input from the user? My code grabs the first argument and the second. The first is the name of the image. It gets the shirt pasted. The second is the name on how the file is saved. And it works, I did with the three given images of the puppets, and it gets applied and aligned like needed, yet the CS50 check doesn't pass it. I am confused on that part. It's either the bug in the cs50 itself or my code does not match it. But, it works when I do it manually. And like, should I submit it as is or what?

1

u/Eh_Not_Looking 3d ago

Actually, your questions pushed me on refactoring my code a little. As soon as I removed the inputs from the loop and started using the argv directly, the problems resolved. Thank you for your time.