Search code examples
pythonimagetkinterpython-imaging-librarycrop

tkinter crop multiple images OSError: [Errno 22]


I create a script to crop images and save them in multiple equal parts. I'm trying to call it using tkinter but I got always this error. It seems the erro is telling me the name of path is to big but how can I solve this issue.

Any suggestions?

def crop():
    global file, basename, chopsize, infile, path
    infile = filedialog.askopenfilename(initialdir="C:\\2022\\Python program\\Bloc1\\Input bloc1",
                                         title= "Select a File", filetypes=(("jpg files", ".jpg"),("all files","*.*")))
    
    chopsize = 400
    path = Path(infile)
    print(path.name)
    #basename = os.path.basename(infile)
    
    img0 = Image.open(infile)
    #img0 = ImageTk.PhotoImage(img0)
    #img0 = Image.open(infile)
    #img0 = Tk.PhotoImage(Image.open(infile))
    #img0 = Image.open(infile, "r")
    #img0 = PIL.Image.open(infile)
    #img0 = cv2.imread(basename)

    #img = ImageOps.grayscale(img)

    width, height = img0.size
    
    print(img0.size)


    # Save Chops of original image
    for x0 in range(0, width, chopsize):
       for y0 in range(0, height, chopsize):
          box = (x0, y0,
                 x0+chopsize if x0+chopsize <  width else  width - 1,
                 y0+chopsize if y0+chopsize < height else height - 1)
          print('%s %s' % (infile, box))
          #img.crop(box).save('zchop.%s.x%03d.y%03d.jpg' % (infile.replace('.jpg',''), x0, y0))
          
          img0.crop(box).save("C:/2022/Python program/Bloc1/Prediction bloc1/Test images/zchop.%s.x%03d.y%03d.jpg" % (infile.replace('.jpg',''), x0, y0))
          #img.crop(box).save("C:/2022/Python program/Bloc1/Prediction bloc1/Test images/zchop.%s.x%03d.y%03d.jpg" % (infile.replace('.jpg',''), x0, y0))

File "C:\Users\jra02028\Anaconda3\lib\site-packages\PIL\Image.py", line 2317, in save
    fp = builtins.open(filename, "w+b")
OSError: [Errno 22] Invalid argument: 'C:/2022/Python program/Bloc1/Prediction bloc1/Test images/zchop.C:/2022/Python program/Bloc1/Input bloc1/predicted_imageCAO2.x000.y000.jpg'

Solution

  • Since infile is already a full pathname, the following code will put this full pathname inside another full pathname:

    "C:/2022/Python program/Bloc1/Prediction bloc1/Test images/zchop.%s.x%03d.y%03d.jpg" % (infile.replace('.jpg',''), x0, y0)
    

    which produces an invalid full pathname.

    To construct the required full pathname, it is easier to use pathlib module:

    from pathlib import Path
    ...
    # convert infile to Path object
    infile = Path(infile)
    # output folder
    outdir = Path("C:/2022/Python program/Bloc1/Prediction bloc1/Test images")
    # construct the output filename
    outname = "zchop.%s.x%03d.y%03d%s" % (infile.stem, x0, y0, infile.suffix)
    # suggest to use f-string
    #outname = f"zchop.{infile.stem}.x{x0:03}.y{y0:03}{infile.suffix}"
    # construct the output full pathname
    outfile = outdir / outname
    

    Then outfile is like

    C:\2022\Python program\Bloc1\Prediction bloc1\Test images\zchop.predicted_imageCAO2.x000.y000.jpg