Write a program that creates an image of green and white horizontal stripes. Your program should ask the user for the size of your image, the name of the output file, and create a .png file of stripes. For example, if the user enters 10, your program should create a 10x10 image, alternating between green and white stripes.
A sample run of the program:
Enter the size: 10 Enter output file: 10stripes.png
Another sample run of the program:
Enter the size: 50 Enter output file: 50stripes.png
To create an image from scratch:
1 Import the libraries.
import matplotlib.pyplot as plt
import numpy as np
2 Create the image–
easy to set all color 1 to 0% (black): img = np.zeros( (num,num,3) )
2 to 100% (white): img = np.ones( (num,num,3) )
3 Do stuff to the pixels to make your image
4 You can display your image:
plt.imshow(img)
plt.show()
5 And save your image:
plt.imsave(’myImage.png’, img)
Python code:
import matplotlib.pyplot as plt
import numpy as np
size = int(input("Enter the size: "))
fname = str(input("Enter output file: "))
#Initialize Image as white color with value 1 in all the pixel
img = np.ones( (size,size,3) )
for i in range(1,size,2):
for j in range(size):
img[i][j][1] = 1
img[i][j][0] = 0
img[i][j][2] = 0
plt.imsave(fname, img)
Get Answers For Free
Most questions answered within 1 hours.