How to purposefully create a truncated image file? #6447
-
I have some error handling in a Django app that is designed to identify truncated images, so I'd like to write tests that confirm it works as-expected. In PIL's test suite, I found references to some truncated images. While I can download these for use in my tests, I would like to understand how the author's went about creating them? i.e. it'd be nice to know how exactly you go about corrupting an image. That way I could also perform the operation in-memory rather than storing files |
Beta Was this translation helpful? Give feedback.
Replies: 1 comment 1 reply
-
Most of the test suite images are in this repository, in the Tests directory: https://github.com/python-pillow/Pillow/tree/main/Tests/images For most of the images, you should be able to click on the commit that adding them (e.g. 430e992 for the one you linked) and from there you can open the pull request adding them (#1366). For most tests added recently, there will likely be a comment explaining how the image was obtained, but there is none here, so you would likely have to ask the PR author However, in general, a truncated image simply means that it is not the complete file, but was truncated, i.e. it is only a part of the file, with the end cut off. You can clearly see where the png you referenced was cut off in the commit preview. For performing the operation in memory, you could do something like: data = file.read()
truncated = data[:len(data) // 2] The image in question is exactly 81920 bytes in size (80 KiB exactly), so you could also do |
Beta Was this translation helpful? Give feedback.
Most of the test suite images are in this repository, in the Tests directory: https://github.com/python-pillow/Pillow/tree/main/Tests/images
Some extra images are in a separate repository for various reasons: https://github.com/python-pillow/pillow-depends/tree/main/test_images
For most of the images, you should be able to click on the commit that adding them (e.g. 430e992 for the one you linked) and from there you can open the pull request adding them (#1366). For most tests added recently, there will likely be a comment explaining how the image was obtained, but there is none here, so you would likely have to ask the PR author
@homm
. Looking at the image, I suspect it was simply found t…