Display a histogram of image brightness values in python

Purpose

It is a memorandum when displaying the histogram of the brightness value of the image with python

code

Display a histogram of a color image

histgram.py


from PIL import Image
import matplotlib.pyplot as plt
import numpy as np

def color_hist(filename):
    img = np.asarray(Image.open(filename).convert("RGB")).reshape(-1,3)
    plt.hist(img, color=["red", "green", "blue"], bins=128)
    plt.show()

color_hist("./test.jpg ")

hist1.png

Read a color image in grayscale and display it as a histogram

histgram.py


from PIL import Image
import matplotlib.pyplot as plt
import numpy as np

def color_hist(filename):
    img = np.asarray(Image.open(filename).convert("L")).reshape(-1,1)
    plt.hist(img, bins=128)
    plt.show()

color_hist("./test.jpg ")

hist2.png

The original image depth_02254.jpg

Postscript

When cropping an image

from PIL import Image
import matplotlib.pyplot as plt
import numpy as np
import cv2

def color_hist(filename):
    img = np.asarray(Image.open(filename).convert("L")).reshape(-1,1)
    plt.hist(img, bins=256)
    plt.show()

def cut_image(filename):
    im = cv2.imread(filename,0)
    dst = im[0:400,250:410] #y,x
    cv2.imwrite('./tmp.jpg',dst)

cut_image("./test.jpg ")
color_hist("./tmp.jpg ")

Coding Error measures

nothing special

reference

Pixel manipulation of images in Python Grayscale with Python Easy way to display a color histogram of an image in Python

Recommended Posts

Display a histogram of image brightness values in python
Display a list of alphabets in Python 3
Waveform display of audio in Python
Find the average / standard deviation of the brightness values in the image
Display pyopengl in a browser (python + eel)
How to send a visualization image of data created in Python to Typetalk
Get the caller of a function in Python
Rewriting elements in a loop of lists (Python)
Make a joyplot-like plot of R in python
Output in the form of a python array
Get a glimpse of machine learning in Python
A well-prepared record of data analysis in Python
Image format in Python
Swapping values in Python
A list of stumbling blocks in Django's image upload
A collection of code often used in personal Python
Write a log-scale histogram on the x-axis in python
Group by consecutive elements of a list in Python
A collection of Excel operations often used in Python
A reminder about the implementation of recommendations in Python
Python OpenCV tried to display the image in text.
Take a screenshot in Python
Create a function in Python
Create a dictionary in Python
Display UTM-30LX data in Python
Python --Check type of values
Equivalence of objects in Python
Tweet with image in Python
Make a bookmarklet in Python
Image Processing Collection in Python
Draw a heart in Python
Implementation of quicksort in Python
Transfer parameter values in Python
[Django3] Display a web page in Django3 + WSL + Python virtual environment
Get the number of specific elements in a python list
How to develop in a virtual environment of Python [Memo]
Grayscale image is displayed as a color image in OpenCV / Python
[Note] Import of a file in the parent directory in Python
How to get a list of built-in exceptions in python
Python: Create a dictionary from a list of keys and values
A memo of writing a basic function in Python using recursion
A set of script files that do wordcloud in Python3
Find the eigenvalues of a real symmetric matrix in Python
How to display a specified column of files in Linux (awk)
Maybe in a python (original title: Maybe in Python)
Pixel manipulation of images in Python
Write a binary search in Python
[Python] A memo of frequently used phrases (by myself) in Python scripts
Create an instance of a predefined class from a string in Python
A good description of Python decorators
Image capture of firefox using python
[python] Manage functions in a list
How to determine the existence of a selenium element in Python
Hit a command in Python (Windows)
Implemented image segmentation in python (Union-Find)
A brief summary of Graphviz in python (explained only for mac)
Try to get a list of breaking news threads in Python.
Create a DI Container in Python
[Python] A memorandum of beautiful soup4
Draw a scatterplot matrix in python
Division of timedelta in Python 2.7 series