Data analysis and presentation-Implementation of hand-drawn images and hand-drawn Data Analysis
NumPy database entry NumPy Data Access and function example: the array of the image indicates the RGB color mode of the image.
Generally, an image uses the RGB color mode, that is, the color of each pixel is composed of red (R), Green (G), and blue (B.
The RGB color channels are changed and superimposed to obtain various colors. The values are 0-255. RGB colors include all colors that human vision can perceive.
PIL (Python Image Library) Library
The PIL library is a third-party library with powerful image processing capabilities. The Installation Method in the command line is as follows:pip install pillow
from PIL import Image
Image is the class (object) that represents an Image in the PIL library)
An image is a two-dimensional matrix composed of pixels. Each element is an RGB value.
Example: convert an array to an ndarray
In [1]: from PIL import ImageIn [2]: import numpy as npIn [3]: im = np.array(Image.open("E:/tiger.jpg"))In [4]: print(im.shape,im.dtype)(435, 428, 3) uint8
An image is a three-dimensional array with dimensions of height, width, and pixel RGB values.
Image Transformation
After reading the image, obtain the pixel RGB value and save it as a new file.
Example: Modify the image and save it separately
In [5]: B = [255,255,255]-imIn [6]: new_im = Image. fromarray (B. astype ('int8') In [7]: new_im.save ("E:/tiger1.jpg") In [8]: a = np. array (Image. open ("E:/tiger.jpg "). convert ('l') # Use grayscale to represent the Image In [9]: B = 255-a In [10]: im = Image. fromarray (B. astype ('int8') In [11]: im. save ("E:/tiger2.jpg") In [12]: c = (100/255) * a + 150 # interval conversion In [13]: im = Image. fromarray (c. astype ('int8') In [14]: im. save ("E:/tiger3.jpg") In [15]: d = 255 * (a/255) ** 2 # pixel square In [16]: im = Image. fromarray (d. astype ('int8') In [17]: im. save ("E:/tiger4.jpg ")
Tiger.jpg tiger1.jpg tiger2.jpg tiger3.jpg tiger4.jpg
Analysis of "hand-drawn effects of images"
Hand-drawn effect features:
- Black/white/gray
- Heavy boundary lines
- Same or similar colors tend to be white
- Slight light source Effect
Implementation Code for hand-drawn images:
From PIL import Imageimport numpy as npa = np. asarray (Image. open ('e:/tiger.jpg '). convert ('l ')). astype ('float') depth = 10. # (0-100) grad = np. gradient (a) # obtain the gradient values of the gray scale of the image grad_x, grad_y = grad # obtain the gradient values of the horizontal and vertical image grad_x = grad_x * depth/100 respectively. grad_y = grad_y * depth/100.A = np. sqrt (grad_x ** 2 + grad_y ** 2 + 1 .) uni_x = grad_x/Auni_y = grad_y/Auni_z = 1. /Avec_el = np. pi/2.2 # The angle of the light source. The radian value is vec_az = np. pi/4. # azimuth angle of the light source. The radian value dx = np. cos (vec_el) * np. cos (vec_az) # The influence of the light source on the X axis dy = np. cos (vec_el) * np. sin (vec_az) # effect of light source on Y axis dz = np. sin (vec_el) # effect of light source on Z axis B = 255 * (dx * uni_x + dy * uni_y + dz * uni_z) # normalization of light source B = B. clip (0,255) im = Image. fromarray (B. astype ('int8') # reconstruct the image im. save ('e:/tiger0.jpg ')
Tiger0.jpg