py_image_resize.py 1.9 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253
  1. from evaluator import *
  2. DESCRIPTION = "Test if the model can resize several images in a given subdirectory."
  3. TAGS = ['code', 'python']
  4. question = """Write function resize_image in python that open get a folder path as in put and looks for all of the images files in that folder using only pillow and resize them to 32x32 and overwrite it. Just give me the python code that I can run by python code.py and the default folder is /tmp """
  5. def setup():
  6. from PIL import Image
  7. import random
  8. import os
  9. def create_random_image(file_path):
  10. # Random size between 100x100 and 800x800
  11. width, height = random.randint(100, 800), random.randint(100, 800)
  12. # Random color
  13. color = (random.randint(0, 255), random.randint(0, 255), random.randint(0, 255))
  14. # Create an image with the random color
  15. image = Image.new("RGB", (width, height), color)
  16. # Save the image
  17. image.save(file_path)
  18. for i in range(10):
  19. file_path = os.path.join('/tmp/', f"random_image_{i+1}.jpg" if random.random() > 0.5 else f"random_image_{i+1}.jpeg" , )
  20. create_random_image(file_path)
  21. def test():
  22. import os
  23. from PIL import Image
  24. # Iterate over each file in the folder
  25. target_size = (32,32)
  26. folder_path = '/tmp/'
  27. for file_name in os.listdir(folder_path):
  28. file_path = os.path.join(folder_path, file_name)
  29. # Check if the file is an image
  30. if file_path.lower().endswith(('.jpg', '.jpeg')):
  31. # Open the image
  32. with Image.open(file_path) as img:
  33. # Check if the image size matches the target size
  34. if img.size != target_size:
  35. print('size is ',img.size)
  36. return False
  37. return True
  38. TestImgResize = Setup(setup) >> question >> LLMRun() >> ExtractCode(keep_main=True) >> Echo() >> PythonRun() >> PyEvaluator(test)
  39. if __name__ == "__main__":
  40. print(run_test(TestImgResize))