import os from PIL import Image import numpy as np def predict(path): img =…
- User
import os from PIL import Image import numpy as np
def predict(path): img = Image.open(path) img_arr = np.array(img)
perform some processing
…
return img_arr
folder_path = "/content/gdrive/My Drive/castle/" # your image folder path, which should contain the images you want to process output_folder = "/content/gdrive/My Drive/castle/Processed/" # your output folder path (must already exist)
for filename in os.listdir(folder_path): if filename.endswith(("jpeg", "png", "jpg", "PNG", "JPEG", "JPG")): # process only image files image_path = os.path.join(folder_path, filename) enhanced_image_array = predict(image_path) # Get predictions enhanced_pil_image = Image.fromarray(enhanced_image_array) # get PIL image from array enhanced_pil_image.save(os.path.join(output_folder, os.path.splitext(filename)[0] + os.path.splitext(filename)[1])) # Save the image--------------------------------------------------------------------------- KeyError Traceback (most recent call last) /usr/local/lib/python3.9/dist-packages/PIL/Image.py in save(self, fp, format, **params) 2219 try: -> 2220 format = EXTENSION[ext] 2221 except KeyError as e:
KeyError: ''
The above exception was the direct cause of the following exception:
ValueError Traceback (most recent call last) 1 frames /usr/local/lib/python3.9/dist-packages/PIL/Image.py in save(self, fp, format, **params) 2220 format = EXTENSION[ext] 2221 except KeyError as e: -> 2222 raise ValueError(f"unknown file extension: {ext}") from e 2223 2224 if format.upper() not in SAVE:
ValueError: unknown file extension:
- Assistant
There seems to be an error with the file extension of the image being saved. It is probably an empty or invalid file extension.
Try modifying the last line of the code to include only the filename without the extension, like this:
enhanced_pil_image.save(os.path.join(output_folder, os.path.splitext(filename)[0]) + ".jpg")
or
enhanced_pil_image.save(os.path.join(output_folder, os.path.splitext(filename)[0]) + ".png")
This should save the processed image with the specified file extension.
- User
FileNotFoundError Traceback (most recent call last) in <cell line: 20>() 18 enhanced_image_array = predict(image_path) # Get predictions 19 enhanced_pil_image = Image.fromarray(enhanced_image_array) # get PIL image from array ---> 20 enhanced_pil_image.save(os.path.join(output_folder, os.path.splitext(filename)[0]) + ".png") # Save the image
/usr/local/lib/python3.9/dist-packages/PIL/Image.py in save(self, fp, format, **params) 2235 fp = builtins.open(filename, "r+b") 2236 else: -> 2237 fp = builtins.open(filename, "w+b") 2238 2239 try:
FileNotFoundError: [Errno 2] No such file or directory: '/content/gdrive/My Drive/castle/Processed/processed.png'
- Assistant
This error suggests that the output_folder
"/content/gdrive/My Drive/castle/Processed/"may not exist. You could create the directory usingos.makedirs(output_folder, exist_ok=True)before generating the output files.Here's the modified code with the suggested change:
import os from PIL import Image import numpy as np def predict(path): img = Image.open(path) img_arr = np.array(img) # perform some processing # … return img_arr folder_path = "/content/gdrive/My Drive/castle/" # your image folder path, which should contain the images you want to process output_folder = "/content/gdrive/My Drive/castle/Processed/" # your output folder path (must already exist) os.makedirs(output_folder, exist_ok=True) # creates the output folder, if it doesn't exist for filename in os.listdir(folder_path): if filename.endswith(("jpeg", "png", "jpg", "PNG", "JPEG", "JPG")): # process only image files image_path = os.path.join(folder_path, filename) enhanced_image_array = predict(image_path) # Get predictions enhanced_pil_image = Image.fromarray(enhanced_image_array) # get PIL image from array enhanced_pil_image.save(os.path.join(output_folder, os.path.splitext(filename)[0]) + ".png") # Save the image - User
images = ["../gdrive/My Drive/maxim/input/"+i for i in os.listdir("../gdrive/My Drive/maxim/input/") if i.endswith(('jpeg', 'png', 'jpg',"PNG","JPEG","JPG"))]
# _ = [resize(path, 1920, save_image=True) for path in images] # Resize Images to 1920 as the max dimension's size else it'll blow the GPU / CPU memory
for path in images:
im = Image.fromarray(predict(path))
im.save("../gdrive/My Drive/maxim/output/"+path.split('/')[-1])
- Assistant
The commented out code seems to be using a function that is not defined in the code snippet. But based on the context, it seems to be resizing input images before processing them.
You might want to define the
resize()function or use an existing image resizing library such asPillow. Here's some sample code that uses thePillowlibrary to resize the input image to a maximum dimension of 1920 pixels, then applies some processing to the image, and saves it to the output folder:
Other models, same or similar prompt
Responses were produced in different, uncontrolled contexts (system prompts, settings, prior turns). This is not a benchmark.