Back
I need to make a picture and store it as a document using python.
I anticipate that the output should be a picture with shading squares of black or white, and the image should have 6x6 squares of random white or dark color blocks.
Try the following code, to solve the problem:
#Import all libraries we will useimport randomimport numpy as npimport cv2#let's create a 6 x 6 matrix with all pixels in black colorimg = np.zeros((6,6,3),np.uint8)#let's use "for" cycle to change colorspace of pixel in a random wayfor x in range(6): for y in range(6): #We use "0" for black color (do nothing) and "1" for white color (change pixel value to [255,255,255]) value = random.randint(0,1) if value == 1: img[x,y] = [255,255,255]#save our image as a "png" imagecv2.imwrite("6_x_6.png",img)
#Import all libraries we will use
import random
import numpy as np
import cv2
#let's create a 6 x 6 matrix with all pixels in black color
img = np.zeros((6,6,3),np.uint8)
#let's use "for" cycle to change colorspace of pixel in a random way
for x in range(6):
for y in range(6):
#We use "0" for black color (do nothing) and "1" for white color (change pixel value to [255,255,255])
value = random.randint(0,1)
if value == 1:
img[x,y] = [255,255,255]
#save our image as a "png" image
cv2.imwrite("6_x_6.png",img)
Are you Interested to learn the concepts of python in detail? Join Python course fast!!
31k questions
32.8k answers
501 comments
693 users