I am searching how can I calculate the entropy of a image for a Capture The Flag. I know what is the entropy theory and I tried a pair of things:

  1. Convert the jpg image on binary image and calculate the entropy of binary text.
  2. Viewing the images and intuiting which has greater entropy

I don't know how can I solve it. What can I do?

  • 2
    $\begingroup$ Why did your two attempts described above fail? Or why where they insufficient? $\endgroup$
    – mikeazo
    Commented Dec 14, 2017 at 16:57
  • 4
    $\begingroup$ You can only estimate the entropy of images. I can create an image where each pixel is the result of some smart calculation that makes the image seem random but which contains not a single bit of entropy. So without knowing how the image has been created I don't think you can calculate all that much from it. $\endgroup$
    – Maarten Bodewes
    Commented Dec 14, 2017 at 17:06
  • 5
    $\begingroup$ An important question to ask is "what do you mean by entropy?". You cannot compute Shannon entropy (unless you know the probability distribution of images); so, what alternative definition do you intend to use? $\endgroup$
    – poncho
    Commented Dec 14, 2017 at 17:21
  • 4
    $\begingroup$ ‘Entropy of a single image’, rather than a distribution on images, doesn't make sense. The correct approach is clearly to split hairs at the CTF organizers for being imprecise with their terminology—no doubt such outside-the-box thinking is the intent of the CTF rather than computing some boring old rote formula that anyone can find copypasta of on a pseudonymous web forum. $\endgroup$ Commented Dec 14, 2017 at 20:49
  • $\begingroup$ These two tries are incorrect because I'm doing a CTF and achieved flags were incorrect. I think the way to win the CTF is calculate the entropy of the image bytes because they can bu calculated, aren't they? $\endgroup$ Commented Dec 15, 2017 at 8:44

1 Answer 1


You don't need the probability distribution of images to calculate the information entropy of an image. That isn't how the entropy calculation works. For example, we can certainly calculate the entropy value of a symbol - say [@] - it would have an entropy value of 0, if we add multiple symbols...[@#], then the entropy value of two distinct bits of information would be 1.0, if we add even more [@#$] then the entropy value of the three distinct bits would be 1.58 - interestingly, if you add in redundant characters to your string, then you will reduce its entropy value, because you are not adding anything new to the string that we did not already know.

Consequently we can apply the same principle to image analyses, we do it all the time. You would just vectorize the pixel data, read the flattened vector data into a dataframe, then do the entropy calc on the dataframe. In my case, I just use scipy library from Python (scipy.stats.entropy). We have analyzed many images using the technique, on average an image has an entropy somewhere between 3 and 4. You can test this concept, by calculating the entropy of a pure white image (it should approximate zero, or be equal to zero), versus a more complicated image with graphs, people, chairs, cars, etc...(should be upwards of 4)

A fun way to cross check the validity of this, is to compare the entropy value of an "image of a string" to the entropy value "of the string bits" themselves - they won't be exactly the same (the image of a string is slightly higher (especially in languages that have multiple separated symbols within a single character bit [i.e., farsi, kanji, etc...]) - but in English they are pretty close.

  • 2
    $\begingroup$ Welcome to Crypto.SE! This is not exactly correct. Computation of a Shanon entropy always involves a distribution and your case is no exception. Entropy of a over two elements is not always 1 bit of information. You get 1 bit only because you make the assumption that these elements are uniform. There is no reason that it is always the case. Now this assumption might be safe to make but regardless, we need a distribution to have a meaningful discussion on Shanon entropy. Have a look at the comments under the question for more discussion on this. $\endgroup$ Commented Apr 24, 2019 at 20:36
  • $\begingroup$ This doesn't actually give a true estimate for JPEGs which is in question here. It would only be useful for comparing two images relatively. Remember that a JPEG is simply a decoding mechanism for a data file called xxx.jpg. It's exactly the same sort of $ G: \{0,1\}^{|xxx.jpg|} \to \{0,1\}^{|image|}$ expansion as a PRNG. $\endgroup$
    – Paul Uszak
    Commented Apr 24, 2019 at 21:04
  • 2
    $\begingroup$ The concept of entropy is fundamentally a property of a probability distribution. What you are tacitly doing by computing the ‘entropy of a string’ is hypothesizing a family of distributions, inferring parameters for the family, and then computing the entropy of the resulting distribution. For example, you seem to be hypothesizing sequences of iid symbols, distributed by the frequencies in the sample–you're just not stating your modeling assumptions, and pretending they aren't there. More on the concepts behind what ‘entropy estimators’ do. $\endgroup$ Commented Apr 25, 2019 at 0:33
  • $\begingroup$ an image has an entropy somewhere between 3 and 4 in what units? Without a unit those numbers seem kind of arbitrary. $\endgroup$
    – Ella Rose
    Commented Apr 25, 2019 at 0:43
  • $\begingroup$ scipy.stats.entropy takes a probability distribution on a discrete space, and returns the entropy of that distribution. The nature of the elements of the space don't figure into entropy, so the input is just represented by an array of probabilities, as if you had labeled the elements from 0 to n. If you fed in images, presumably it interpreted each pixel value as an (unnormalized) probability and simply computed $\sum p_i \log p_i$ where the $p_i$ are the (normalized) pixel values, or something like that. $\endgroup$ Commented Apr 25, 2019 at 0:45

Not the answer you're looking for? Browse other questions tagged or ask your own question.