使用python制作艺术图片,还能自动做成视频,要什么自行车
> 首先Python环境和库! > 直接一步步来就行
import cv2 import numpy as np import random # 设置视频写入器 fourcc = cv2.VideoWriter_fourcc(*'mp4v') out = cv2.VideoWriter('art.mp4', fourcc, 20.0, (640, 480)) # 设置画布 canvas = np.zeros((480, 640, 3), dtype=np.uint8) # 定义颜色 colors = [(255, 0, 0), (0, 255, 0), (0, 0, 255), (255, 255, 0), (0, 255, 255), (255, 0, 255)] # 定义形状 shapes = ['circle', 'rectangle', 'triangle'] # 定义大小 sizes = [10, 20, 30, 40, 50] # 定义帧数 num_frames = 100 # 在每个帧上的画布上绘制随机形状 for i in range(num_frames): # 随机选择形状、颜色和大小 shape = random.choice(shapes) color = random.choice(colors) size = random.choice(sizes) # 随机选择形状的位置 x = random.randint(0, 640-size) y = random.randint(0, 480-size) # 在画布上绘制形状 if shape == 'circle': cv2.circle(canvas, (x+size//2, y+size//2), size//2, color, -1) elif shape == 'rectangle': cv2.rectangle(canvas, (x, y), (x+size, y+size), color, -1) elif shape == 'triangle': pts = np.array([[x+size//2, y], [x, y+size], [x+size, y+size]], np.int32) cv2.fillPoly(canvas, [pts], color) # 将帧写入视频 out.write(canvas) # 释放视频写入器并销毁所有窗口 out.release() cv2.destroyAllWindows() # 导入必要的库 from PyQt5.QtWidgets import QApplication, QWidget, QVBoxLayout, QHBoxLayout, QLabel, QPushButton, QComboBox, QColorDialog from PyQt5.QtGui import QPainter, QColor, QPen from PyQt5.QtCore import Qt class SettingsWindow(QWidget): def __init__(self): super().__init__() # 设置窗口标题和大小 self.setWindowTitle("设置") self.setFixedSize(400, 300) # 创建参数按钮的布局 self.parameter_layout = QVBoxLayout() # 创建图形样式标签 self.style_label = QLabel("图形样式:") self.parameter_layout.addWidget(self.style_label) # 创建图形样式选项的组合框 self.style_combo = QComboBox() self.style_combo.addItems(["实线", "虚线", "点线"]) self.parameter_layout.addWidget(self.style_combo) # 创建图形颜色标签 self.color_label = QLabel("图形颜色:") self.parameter_layout.addWidget(self.color_label) # 创建选择图形颜色的按钮 self.color_button = QPushButton() self.color_button.setText("选择颜色") self.color_button.clicked.connect(self.select_color) self.parameter_layout.addWidget(self.color_button) # 创建按钮的布局 self.button_layout = QHBoxLayout() # 创建保存设置的按钮 self.save_button = QPushButton() self.save_button.setText("保存") self.save_button.clicked.connect(self.save_settings) self.button_layout.addWidget(self.save_button) # 创建取消设置的按钮 self.cancel_button = QPushButton() self.cancel_button.setText("取消") self.cancel_button.clicked.connect(self.cancel_settings) self.button_layout.addWidget(self.cancel_button) # 创建主布局并添加参数和按钮布局 self.main_layout = QVBoxLayout() self.main_layout.addLayout(self.parameter_layout) self.main_layout.addLayout(self.button_layout) # 设置窗口的主布局 self.setLayout(self.main_layout) # 设置图形样式和颜色的默认值 self.graphic_style = Qt.SolidLine self.graphic_color = QColor(0, 0, 0) def select_color(self): # 打开颜色对话框并设置所选颜色 color = QColorDialog.getColor() if color.isValid(): self.graphic_color = color def save_settings(self): # 根据所选选项设置图形样式 if self.style_combo.currentText() == "实线": self.graphic_style = Qt.SolidLine elif self.style_combo.currentText() == "虚线": self.graphic_style = Qt.DashLine elif self.style_combo.currentText() == "点线": self.graphic_style = Qt.DotLine # 关闭设置窗口 self.close() def cancel_settings(self): # 将图形样式和颜色重置为默认值 self.graphic_style = Qt.SolidLine self.graphic_color = QColor(0, 0, 0) # 关闭设置窗口 self.close() class Graphic(QWidget): def __init__(self): super().__init__() # 设置窗口标题和大小 self.setWindowTitle("图形") self.setFixedSize
能够直接保存为本地视频,是不是很方便,代码已经跑过绝对跑得通!
博客园支援贴,严禁转载到除博客园任何的平台,作者会去查,请你善良!
随便聊天,聊啥都行的那种,请加V:cydeeporigin 谢绝任何广告和推销,非诚勿扰!

浙公网安备 33010602011771号