Python3 创建指定尺寸的空白图片并写入内容
要创建一个指定尺寸的空文件并通过 cv.imwrite 写入内容,你可以使用 OpenCV 库来处理图片。下面是一个示例代码:
import cv2
import numpy as np
# 指定图片尺寸
width = 640
height = 480
# 创建空白图片
blank_image = np.zeros((height, width, 3), np.uint8)
# 写入内容
cv2.putText(blank_image, 'Hello, World!', (50, 200), cv2.FONT_HERSHEY_SIMPLEX, 2, (255, 255, 255), 2)
cv2.rectangle(blank_image, (200, 100), (400, 400), (0, 255, 0), 3)
cv2.circle(blank_image, (320, 240), 50, (0, 0, 255), -1)
# 保存图片
cv2.imwrite('blank_image.png', blank_image)
在上述代码中,我们使用 np.zeros 函数创建了一个指定尺寸的空白图片,然后使用 cv2.putText、cv2.rectangle 和 cv2.circle 函数在图片上绘制了一些内容。最后,使用 cv2.imwrite 函数将图片保存为文件,命名为 blank_image.png。
你可以根据需要修改尺寸、写入的内容以及保存的文件名。
原文地址: https://www.cveoy.top/t/topic/qnJx 著作权归作者所有。请勿转载和采集!