Skip to content

Instantly share code, notes, and snippets.

@adamgreig
Created April 30, 2011 00:43
Show Gist options
  • Save adamgreig/949290 to your computer and use it in GitHub Desktop.
Save adamgreig/949290 to your computer and use it in GitHub Desktop.
python histogram generator
# Output a colour histogram PNG from a supplied image
import sys
import matplotlib
from matplotlib.backends.backend_agg import FigureCanvasAgg
from matplotlib.figure import Figure
import numpy
import Image
if len(sys.argv) != 3:
print "usage:", sys.argv[0], "<image filename>", "<output filename>"
sys.exit()
im = Image.open(sys.argv[1])
a = numpy.asarray(im)
w, h, c = a.shape
a2 = a.reshape(w*h, c)
f = Figure(figsize=(3,1), dpi=96, frameon=False)
ax = f.add_axes((0,0,1,1))
ax.set_color_cycle([(1,0,0),(0,1,0),(0,0,1)])
ax.hist(a2, bins=20)
ax.axis('tight')
ax.set_xticks([])
ax.set_yticks([])
c = FigureCanvasAgg(f)
c.print_png(sys.argv[2])
@rawstar134
Copy link

This is the most straightforward trick to generate the histogram with python. This same article is found on medium platform and blogger platform. This is the best article for creating histogram: https://debuggingsolution.blogspot.com/2022/03/histogram-of-image-with-python.html

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment