Cross-posted at Wolfram Community

I would like to have a function makeJPG[megabytes_] that generates random jpg images of given file sizes with itf filesize (in megabytes) watermarked on the image itself.

This is tricky because jpg compression isn't constant. Here's my first pass at it (but the watermark is squished, and the filesizes are not accurate):

(*Estimated by hand*)
points = 
 {{0.841, 1241.906471417973`}, {2.399941`, 2000}, {9.597819`, 4000}, {38.390543`, 8000}};
ifun = Interpolation[points];

makeJPG[megabytes_] := 
  Module[{dim = IntegerPart @ ifun[megabytes], numberWatermark},
    numberWatermark = 
        FontFamily -> "Helvetica Neue"], ImageSize -> {dim, dim}];
      ImageAdd[Image[RandomReal[1, {dim, dim, 3}]], 
        ImageSubtract[Image @ numberWatermark, 0.7]]]]


The application here is an iphone app that I need to test on images of different megapixels.

  • 2
    $\begingroup$ What you could try is to make a function makeImage[text_, dim_] which produces a watermarked image of a specified size, then have another routine try different values of dim until your exported image has the right file size. I wonder if FindRoot will work for that... $\endgroup$ – Rahul Sep 7 '13 at 3:15
  • $\begingroup$ To get the watermark at the right size without changing the aspect ratio try this: ImageCrop[Rasterize[string, RasterSize -> dim, ImageSize -> {dim}], {dim, dim}, Padding -> White] $\endgroup$ – Simon Woods Sep 7 '13 at 10:20
  • 1
    $\begingroup$ For the file size you might consider fine tweaking by changing the maximum image value instead of the dimensions. E.g. a random image with a maximum of 0.1 will compress more than one with a maximum of 1.0 $\endgroup$ – Simon Woods Sep 7 '13 at 10:24
  • 1
    $\begingroup$ Not fully related but funny: xkcd.com/688 $\endgroup$ – Dr. belisarius Sep 7 '13 at 19:20

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Browse other questions tagged or ask your own question.