Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Dynamically generate valid image of a certain filesize for testing

I'm trying to work out a method for dynamically generating image files with PIL/Pillow that are a certain file size in order to better exercise certain code paths in my unit tests.

For example, I have some image validation code that limits the file size to 100kb. I'd like to generate an image dynamically that is 150kb to ensure that the validation works. It needs to be a valid image and within given dimensions (ie 400x600).

Any thoughts on how to add sufficient "complexity" to an image canvas for testing?

like image 675
erikcw Avatar asked Nov 11 '22 16:11

erikcw


1 Answers

Does it have to be exactly 150kb, or just somewhere comfortably over 100kb?

One approach would be to create a JPEG at 100% quality, and insert lots of (random) text into all the available EXIF and IPTC headers. Including a large thumbnail image will also push the size up.

(And like Bo102010 suggested, you could also use random RGB values to minimise the compression.)

like image 161
Hugo Avatar answered Nov 15 '22 05:11

Hugo