Image processing with Python 100 knocks # 2 Grayscale

Introduction

Hi, I'm Ramu. Image processing implements the familiar grayscale.

2nd: Grayscale

Grayscale is often used as a pre-process for various image processing. If you want to learn image processing, be sure to know it. Grayscale is a method of expressing an image using black, white, and shades of gray, which are intermediate colors. Monochrome images have only two black and white colors, while grayscale images usually use 256 colors.

Each pixel is calculated by the following formula. $ P = 0.2126 R + 0.7152 G + 0.0722 B $

grayscale.py


import cv2
import matplotlib.pyplot as plt
import numpy as np
plt.gray()

def grayscale(img):
  #Array creation for grayscale images
  dst = np.zeros((img.shape[0], img.shape[1]))

  #Grayscale
  dst[:,:] = (0.2126*img[:,:,2] + 0.7152*img[:,:,1] + 0.0722*img[:,:,0]).astype(np.uint8)

  return dst


#Image reading
img = cv2.imread('image.jpg')

#Grayscale
gray = grayscale(img)

#Save image
cv2.imwrite('result.jpg', gray)
#Image display
plt.imshow(gray)
plt.show()

ファイル名 ファイル名

The image on the left is the input image, and the image on the right is the output image. You have created a solid grayscale image.

in conclusion

If you have any questions, please feel free to contact us. imori_imori's Github has the official answer, so please check that as well. ..

Recommended Posts

Image processing with Python 100 knocks # 2 Grayscale
Image processing with Python 100 knocks # 3 Binarization
Image processing with Python
Image processing with Python 100 knocks # 8 Max pooling
Image processing with Python 100 knocks # 7 Average pooling
Image processing with Python 100 knocks # 9 Gaussian filter
[Python] Image processing with scikit-image
python image processing
Image processing 100 knocks ①
Basics of binarized image processing with Python
Image processing with Python 100 knock # 10 median filter
Grayscale by matrix-Reinventor of Python image processing-
Image processing with Python 100 knock # 12 motion filter
Drawing with Matrix-Reinventor of Python Image Processing-
Easy image processing in Python with Pillow
Light image processing with Python x OpenCV
Image processing with MyHDL
First Python image processing
Image Processing with PIL
Image processing with Python 100 knocks # 4 Binarization of Otsu (discriminant analysis method)
Getting started with Python with 100 knocks on language processing
Image processing from scratch with python (5) Fourier transform
Image processing from scratch with python (4) Contour extraction
100 Language Processing with Python Knock 2015
Image processing with PIL (Pillow)
"Apple processing" with OpenCV3 + Python3
Image editing with python OpenCV
Acoustic signal processing with Python (2)
Acoustic signal processing with Python
Sorting image files with Python (3)
100 image processing knocks !! (001 --010) Carefully and carefully
Sorting image files with Python
Image Processing Collection in Python
Notes on HDR and RAW image processing with Python
[Chapter 5] Introduction to Python with 100 knocks of language processing
[Chapter 2] Introduction to Python with 100 knocks of language processing
[Chapter 4] Introduction to Python with 100 knocks of language processing
Cut out an image with python
Real-time image processing basics with opencv
[Python] Using OpenCV with Python (Image Filtering)
[Python] Easy parallel processing with Joblib
100 Language Processing Knock with Python (Chapter 1)
[Python] Using OpenCV with Python (Image transformation)
100 Language Processing Knock with Python (Chapter 3)
Personal notes for python image processing
Image processing 100 knocks Q9, Q10 (filter) speedup
Let's do image scraping with Python
[Let's play with Python] Image processing to monochrome and dots
Find image similarity with Python + OpenCV
Send image with python, save with php
Gradation image generation with Python [1] | np.linspace
HTML email with image to send with python
Image processing by Python 100 knock # 1 channel replacement
Create an image processing viewer with PySimpleGUI
Create a dummy image with Python + PIL.
Introduction to Python Image Inflating Image inflating with ImageDataGenerator
Periodically perform arbitrary processing with Python Twisted
Let Heroku do background processing with Python
100 Language Processing Knock with Python (Chapter 2, Part 2)
Use cryptography library cryptography with Docker Python image
3. Natural language processing with Python 2-1. Co-occurrence network