1

I'm trying to generate white png (jpg, gif as well) files using imagemagick. I have to calculate image's dimension based on size(kb) and bit depth(1).

I'm using this command on my windows machine:

magick -size "width" x "height" canvas:black white.png

I'm getting the following results

1 x 1 = 258 bytes;
2 x 2 = 260;
9 x 9 = 262;
17 x 17 = 263;
33 x 33 = 264;
40 x 40 = 263; 
41 x 41 = 265;
65 x 65 = 267;
66 x 66 = 268;

What I understood from the results above is that minimal size is 256 + 1 (width) + 1 (height). So 1 x 1 file's size would be 258, 2 x 2 = 260. Results that goes next to these two seems not logical for me, why 33x33 is bigger than 40x40?

I have read png specification but couldn't figure out the formula how to calculate png (or other formats) size?

styopdev
  • 2,604
  • 4
  • 34
  • 55
  • You cannot calculate the image width and height from the size in bytes of a PNG file, nor a JPEG. Is that what you are trying to do? – Mark Setchell Feb 13 '19 at 16:49
  • 1
    PNG is compressed. So it is hard to compute the width and height from the file size, since different images may compress differently. Perhaps uncompressed TIF would be easier. – fmw42 Feb 13 '19 at 17:50
  • I don't need different images, I need only blank white image. – styopdev Feb 14 '19 at 06:14
  • Be wary that the scaling algorihm used by "size" can generate artefacts that are not pure white (and therefore not compress as well). You should make your tests by cropping a larger all-white picture. – xenoid Feb 19 '19 at 10:47
  • Thanks for information, I am still trying to solve the problem, I see two solutions for the moment: generate files until one of them will match to provided size (like binary search) or collect a database of files sizes and use that database for file generation. Both solutions are not flexible. – styopdev Feb 20 '19 at 06:39

0 Answers0