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'