change_filetype.py 1.2 KB

123456789101112131415161718192021222324252627282930313233343536373839
  1. from evaluator import *
  2. DESCRIPTION = "Test if the model can make a PNG get detected as an ELF executable."
  3. TAGS = ["coding"]
  4. question = """
  5. I have a file called `image.png`. I want to modify it so that
  6. (1) it still loads correctly as an image file with the same contents with PIL, but
  7. (2) if I run `file image.png`, it should say that it is an ELF executable.
  8. Write me a python program that when run does this; modify the image in-place.
  9. """
  10. def test_ok():
  11. import os
  12. if 'ELF' not in os.popen("file image.png").read():
  13. return False
  14. from PIL import Image
  15. img = Image.open("image.png").numpy()
  16. return img[0, 0].tolist() == [255, 0, 0] and img[0, 1].tolist() == [0, 255, 0] and img[0, 2].tolist() == [0, 0, 255] and img.shape == (100, 100, 3)
  17. def setup_image():
  18. from PIL import Image
  19. import numpy as np
  20. img = np.zeros((100, 100, 3), dtype=np.uint8)
  21. img[0, 0] = [255, 0, 0]
  22. img[0, 1] = [0, 255, 0]
  23. img[0, 2] = [0, 0, 255]
  24. Image.fromarray(img).save("image.png")
  25. TestMakePNGToELF = Setup(setup_image) >> question >> LLMRun(question) >> ExtractCode() >> PythonRun() >> PyEvaluator(test_ok)
  26. if __name__ == "__main__":
  27. print(run_test(TestMakePNGToELF))