260413
|
Before Width: | Height: | Size: 165 KiB After Width: | Height: | Size: 176 KiB |
|
|
@ -9,8 +9,7 @@ import numpy
|
|||
from pathlib import Path
|
||||
import random
|
||||
from typing import List, Dict, Optional, Union, Literal, Tuple
|
||||
|
||||
from numpy._core import int64
|
||||
from uuid import uuid4
|
||||
|
||||
|
||||
class Canvas:
|
||||
|
|
@ -31,7 +30,8 @@ class Canvas:
|
|||
:param canvas_width: 画布宽度
|
||||
"""
|
||||
# 构建所有素材文件夹的根目录路径
|
||||
self.materials_folder_path = Path(materials_folder_path)
|
||||
# self.materials_folder_path = Path(materials_folder_path)
|
||||
self.materials_folder_path = Path("/Users/liubiren/Python/短视频合成自动化/纯图")
|
||||
|
||||
self.canvas_height, self.canvas_width = canvas_height, canvas_width
|
||||
# 创建画布(默认尺寸为 1920x1080,底色为白色)
|
||||
|
|
@ -162,49 +162,14 @@ class Canvas:
|
|||
# 写回画布
|
||||
self.canvas[y1:y2, x1:x2] = area
|
||||
|
||||
def _insert_text(
|
||||
self,
|
||||
text: str,
|
||||
):
|
||||
"""
|
||||
插入文字
|
||||
:param text: 文字内容
|
||||
"""
|
||||
# 字体
|
||||
font = cv2.FONT_HERSHEY_SIMPLEX
|
||||
# 字体缩放比例
|
||||
font_scale = 2
|
||||
# 文字厚度
|
||||
thickness = 3
|
||||
|
||||
# 获取文字尺寸
|
||||
(text_width, text_height), _ = cv2.getTextSize(
|
||||
text=text, fontFace=font, fontScale=font_scale, thickness=thickness
|
||||
)
|
||||
|
||||
cv2.putText(
|
||||
img=self.canvas,
|
||||
text=text,
|
||||
org=(
|
||||
(self.canvas_width - text_width) // 2,
|
||||
(self.canvas_height - text_height) // 2,
|
||||
),
|
||||
fontFace=font,
|
||||
fontScale=font_scale,
|
||||
color=(0, 0, 0, 255),
|
||||
thickness=thickness,
|
||||
lineType=cv2.LINE_AA,
|
||||
)
|
||||
|
||||
def _save(
|
||||
self,
|
||||
image_path: str,
|
||||
) -> None:
|
||||
def _save(self) -> None:
|
||||
"""
|
||||
保存图片
|
||||
:param image_path: 图片保存路径
|
||||
:return: None
|
||||
"""
|
||||
# 构建图片路径
|
||||
image_path = self.materials_folder_path / "成品" / f"{uuid4().hex.upper()}.jpg"
|
||||
|
||||
# 编码为二进制数据
|
||||
success, image_encoded = cv2.imencode(
|
||||
".jpg", img=self.canvas, params=[cv2.IMWRITE_JPEG_QUALITY, 95]
|
||||
|
|
@ -256,9 +221,6 @@ class Canvas:
|
|||
# 粘贴按钮素材到画布
|
||||
self._paste_material(material=button)
|
||||
|
||||
# 插入文字
|
||||
self._insert_text(text="还未登录")
|
||||
|
||||
# 箭头素材
|
||||
arrow = self._read_material(
|
||||
material_path=random.choice(materials_paths["箭头"])
|
||||
|
|
@ -274,9 +236,7 @@ class Canvas:
|
|||
self._paste_material(material=declaration, position=(None, 1640))
|
||||
|
||||
# 保存图片
|
||||
self._save(
|
||||
image_path=f"draw_{current_count}.jpg",
|
||||
)
|
||||
self._save()
|
||||
current_count += 1
|
||||
|
||||
|
||||
|
|
|
|||
|
After Width: | Height: | Size: 16 KiB |
|
After Width: | Height: | Size: 148 KiB |
|
After Width: | Height: | Size: 128 KiB |
|
After Width: | Height: | Size: 73 KiB |
|
After Width: | Height: | Size: 69 KiB |
|
After Width: | Height: | Size: 72 KiB |
|
After Width: | Height: | Size: 64 KiB |
|
After Width: | Height: | Size: 45 KiB |
|
After Width: | Height: | Size: 51 KiB |
|
After Width: | Height: | Size: 51 KiB |
|
After Width: | Height: | Size: 91 KiB |
|
After Width: | Height: | Size: 51 KiB |
|
After Width: | Height: | Size: 60 KiB |
|
After Width: | Height: | Size: 49 KiB |
|
After Width: | Height: | Size: 68 KiB |
|
After Width: | Height: | Size: 39 KiB |
|
After Width: | Height: | Size: 38 KiB |
|
After Width: | Height: | Size: 94 KiB |
|
After Width: | Height: | Size: 52 KiB |
|
After Width: | Height: | Size: 67 KiB |
|
After Width: | Height: | Size: 87 KiB |
|
After Width: | Height: | Size: 66 KiB |
|
After Width: | Height: | Size: 57 KiB |
|
After Width: | Height: | Size: 88 KiB |
|
After Width: | Height: | Size: 68 KiB |
|
After Width: | Height: | Size: 77 KiB |
|
After Width: | Height: | Size: 94 KiB |
|
After Width: | Height: | Size: 64 KiB |
|
After Width: | Height: | Size: 80 KiB |
|
After Width: | Height: | Size: 85 KiB |
|
After Width: | Height: | Size: 85 KiB |
|
After Width: | Height: | Size: 63 KiB |
|
After Width: | Height: | Size: 108 KiB |
|
After Width: | Height: | Size: 76 KiB |
|
After Width: | Height: | Size: 70 KiB |
|
After Width: | Height: | Size: 86 KiB |
|
After Width: | Height: | Size: 106 KiB |
|
After Width: | Height: | Size: 54 KiB |
|
After Width: | Height: | Size: 73 KiB |
|
After Width: | Height: | Size: 90 KiB |
|
After Width: | Height: | Size: 60 KiB |
|
After Width: | Height: | Size: 47 KiB |
|
After Width: | Height: | Size: 34 KiB |
|
After Width: | Height: | Size: 55 KiB |
|
After Width: | Height: | Size: 47 KiB |
|
After Width: | Height: | Size: 64 KiB |
|
After Width: | Height: | Size: 50 KiB |
|
After Width: | Height: | Size: 56 KiB |
|
After Width: | Height: | Size: 68 KiB |
|
After Width: | Height: | Size: 70 KiB |
|
After Width: | Height: | Size: 47 KiB |
|
After Width: | Height: | Size: 78 KiB |
|
After Width: | Height: | Size: 93 KiB |
|
After Width: | Height: | Size: 92 KiB |
|
After Width: | Height: | Size: 86 KiB |
|
After Width: | Height: | Size: 81 KiB |
|
After Width: | Height: | Size: 41 KiB |
|
After Width: | Height: | Size: 52 KiB |
|
After Width: | Height: | Size: 44 KiB |
|
After Width: | Height: | Size: 88 KiB |
|
After Width: | Height: | Size: 58 KiB |
|
After Width: | Height: | Size: 46 KiB |
|
After Width: | Height: | Size: 64 KiB |
|
After Width: | Height: | Size: 107 KiB |
|
After Width: | Height: | Size: 84 KiB |
|
After Width: | Height: | Size: 79 KiB |
|
After Width: | Height: | Size: 122 KiB |
|
After Width: | Height: | Size: 46 KiB |
|
After Width: | Height: | Size: 61 KiB |
|
After Width: | Height: | Size: 65 KiB |
|
After Width: | Height: | Size: 68 KiB |
|
After Width: | Height: | Size: 81 KiB |
|
After Width: | Height: | Size: 94 KiB |
|
After Width: | Height: | Size: 52 KiB |
|
After Width: | Height: | Size: 63 KiB |
|
After Width: | Height: | Size: 57 KiB |
|
After Width: | Height: | Size: 48 KiB |
|
After Width: | Height: | Size: 43 KiB |
|
After Width: | Height: | Size: 56 KiB |
|
After Width: | Height: | Size: 62 KiB |
|
After Width: | Height: | Size: 77 KiB |
|
After Width: | Height: | Size: 59 KiB |
|
After Width: | Height: | Size: 58 KiB |
|
After Width: | Height: | Size: 66 KiB |
|
After Width: | Height: | Size: 63 KiB |
|
After Width: | Height: | Size: 54 KiB |
|
After Width: | Height: | Size: 46 KiB |
|
After Width: | Height: | Size: 64 KiB |
|
After Width: | Height: | Size: 61 KiB |
|
After Width: | Height: | Size: 85 KiB |
|
After Width: | Height: | Size: 52 KiB |
|
After Width: | Height: | Size: 68 KiB |
|
After Width: | Height: | Size: 90 KiB |
|
After Width: | Height: | Size: 49 KiB |
|
After Width: | Height: | Size: 48 KiB |
|
After Width: | Height: | Size: 64 KiB |
|
After Width: | Height: | Size: 42 KiB |
|
After Width: | Height: | Size: 78 KiB |