Gradation image generation with Python [1] | np.linspace

Introduction

This time, I will generate a gradation image with python by referring to this article.

Gradient image generation

This time, we will generate an image using two functions.

gradation_2d_color


def gradation_2d_color(start, stop, size, is_horizontal):
    if is_horizontal:
        return np.tile(np.linspace(start, stop, size[1]), (size[0], 1))
    else:
        return np.tile(np.linspace(start, stop, size[0]), (size[1], 1)).T

Using np.linspace, create a one-dimensional array of length height (or width) that stores the values of arithmetic progressions that divide the range from start to stop by the number of height (or width), and create this with np.tile. Creates a two-dimensional array of length width (or height) that is duplicated and stored by the number of width (or height). ・ Start: Start pixel value ・ Stop: End pixel value ・ Size: Image size [height, width] ・ Is_horizontal: True → Horizontal gradation change / False → Vertical gradation change

gradation_3d_img


def gradation_3d_img(start_list, stop_list, size, is_horizontal_list):
    result = np.zeros((size[0], size[1], len(start_list)), dtype=np.float)

    for i, (start, stop, is_horizontal) in enumerate(zip(start_list, stop_list, is_horizontal_list)):
        result[:, :, i] = gradation_2d_color(start, stop, size, is_horizontal)

    return result

Pass the list corresponding to each rgb. The first element is the r component, the second element is the g component, and the third element is the b component. -Start_list: List of start pixel values -Stop_list: List of end pixel values ・ Size: List of image sizes ・ Is_horizontal_list: List of changing directions

Use these functions to generate a gradient image.

main


    size = [400,500] #height, width
    #Horizontally black → white
    img = gradation_3d_img([0,0,0], [255,255,255], size, [True,True,True])

    img = img.astype(np.uint8)
    img = cv2.cvtColor(img, cv2.COLOR_RGB2BGR)
    cv2.imwrite('out.png', img)

Output image

I tried other parameters with various values, with the image size set to (400,500).

The color changes continuously from `rgb (0,0,0)` to `rgb (255,255,255)` in the horizontal direction. The color changes continuously from `rgb (0,0,0)` to `rgb (255,255,255)` in the vertical direction. The color changes continuously from `rgb (0,255,255)` to `rgb (255,255,255)` in the vertical direction. The color changes continuously from `rgb (255,0,0)` to `rgb (0,255,255)` in the horizontal direction. When the change direction of r is set to the vertical direction ↓, it now changes diagonally. Specifically, the color change from `rgb (0,0,0)` to` rgb (255,255,255) `in the diagonally lower right ↘︎ direction, and` rgb (255,0,0) `in the diagonally upper right ↗︎ direction. The color is changing from to `rgb (0,255,255)`. Next, if the change direction of r and g is set to the vertical direction ↓ The color changes from `rgb (255,255,0)` to `rgb (0,0,255)` in the diagonally upward ↗︎ direction.

Future tasks

This gradation is expressed using np.linspace which generates arithmetic progression. I think you can express the gradation in the sense that the numerical value changes continuously. However, another question has arisen that the gradation change can be expressed numerically. Next time I will write about it.

Recommended Posts

Gradation image generation with Python [1] | np.linspace
Image processing with Python
[Small story] Test image generation with Python / OpenCV
Image processing with Python (Part 2)
Image editing with python OpenCV
Sorting image files with Python (2)
Sorting image files with Python (3)
Image processing with Python (Part 1)
Tweet with image in Python
Sorting image files with Python
Image processing with Python (Part 3)
Image caption generation with Chainer
[Python] Image processing with scikit-image
JPEG image generation by specifying quality with Python + OpenCV
Cut out an image with python
[Python] Using OpenCV with Python (Image Filtering)
[Python] Using OpenCV with Python (Image transformation)
Image processing with Python 100 knocks # 3 Binarization
Let's do image scraping with Python
Password generation in texto with python
CSRF countermeasure token generation with Python
Find image similarity with Python + OpenCV
Image processing with Python 100 knocks # 2 Grayscale
Send image with python, save with php
Basics of binarized image processing with Python
Image processing with Python 100 knock # 10 median filter
HTML email with image to send with python
Create a dummy image with Python + PIL.
Image processing with Python 100 knocks # 8 Max pooling
Introduction to Python Image Inflating Image inflating with ImageDataGenerator
Image processing with Python & OpenCV [Tone Curve]
Image processing with Python 100 knock # 12 motion filter
Image acquisition from camera with Python + OpenCV
Drawing with Matrix-Reinventor of Python Image Processing-
Easily try automatic image generation with DCGAN-tensorflow
Easy image processing in Python with Pillow
Image processing with Python 100 knocks # 7 Average pooling
Light image processing with Python x OpenCV
Image processing with Python 100 knocks # 9 Gaussian filter
Sample to convert image to Wavelet with Python
FizzBuzz with Python3
Scraping with Python
python image processing
Statistics with python
Scraping with Python
Twilio with Python
Integrate with Python
Play with 2016-Python
AES256 with python
python starts with ()
Bingo with python
Zundokokiyoshi with python
Excel with Python
Microcomputer with Python
Cast with python
I tried "smoothing" the image with Python + OpenCV
Convert PDF to image (JPEG / PNG) with Python
I tried "differentiating" the image with Python + OpenCV
Create polka dot wallpaper with Python Image Library
Color page judgment of scanned image with python
Image processing from scratch with python (5) Fourier transform