We are about to switch to a new forum software. Until then we have removed the registration on this forum.
hi, can anybody please help me to translate this code from python to processing?
import Image
import math
def image_entropy(img):
"""calculate the entropy of an image"""
histogram = img.histogram()
histogram_length = sum(histogram)
samples_probability = [float(h) / histogram_length for h in histogram]
return -sum([p * math.log(p, 2) for p in samples_probability if p != 0])
img = Image.open('headshot.jpg')
print image_entropy(img)
Answers
Sorry i cant paste the python code, here in this link you can see the code: brainacle.com/calculating-image-entropy-with-python-how-and-why.html
To format code, just edit your post, highlight your code, and then press the code button- it looks like a C.
What exactly does this code do? Generally you don't simply translate code from one language to another line by line. Instead, you take the entire thing, write down what it does in English, and then use that English description as an algorithm to implement in the second language.
So your first step is writing down exactly what this program does. Step 2 is figuring out how to do that in Processing.
http://forum.processing.org/two/discussion/8045/how-to-format-code-and-text