Created
April 30, 2011 00:43
-
-
Save adamgreig/949290 to your computer and use it in GitHub Desktop.
python histogram generator
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# 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]) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
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