You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
136 lines
4.1 KiB
136 lines
4.1 KiB
import platform,sys,redis,time,requests,json,atexit |
|
import os, sys, time, argparse, requests, json, re, oss2 |
|
import bpy |
|
import open3d as o3d |
|
import shutil |
|
import psutil |
|
import trimesh |
|
from PIL import Image |
|
from pyvirtualdisplay import Display |
|
# os.environ['DISPLAY'] = ':1' |
|
AccessKeyId = 'LTAI5tSReWm8hz7dSYxxth8f' |
|
AccessKeySecret = '8ywTDF9upPAtvgXtLKALY2iMYHIxdS' |
|
Endpoint = 'oss-cn-shanghai.aliyuncs.com' |
|
Bucket = 'suwa3d-website' |
|
oss_client = oss2.Bucket(oss2.Auth(AccessKeyId, AccessKeySecret), Endpoint, Bucket) |
|
|
|
# 创建虚拟显示 |
|
display = Display(visible=0, size=(500, 500)) |
|
display.start() |
|
|
|
# pid = sys.argv[1] |
|
# target_folder = '/data/datasets/complate/preview' |
|
# source_folder = f'/data/datasets/complate/objs/{pid}' |
|
|
|
# 定义函数来递归遍历文件夹下的所有文件 |
|
def list_files(directory): |
|
for root, dirs, files in os.walk(directory): |
|
for file in files: |
|
yield os.path.join(root, file) |
|
|
|
|
|
def trim_whitespace(image_path, output_path): |
|
# 打开图片 |
|
image = Image.open(image_path) |
|
|
|
# 获取图片的像素数据 |
|
data = image.getdata() |
|
|
|
# 检测图片边界 |
|
left = image.width |
|
right = 0 |
|
top = image.height |
|
bottom = 0 |
|
for x in range(image.width): |
|
for y in range(image.height): |
|
# 如果像素不是白色,则更新边界坐标 |
|
if data[x + y * image.width] != (255, 255, 255, 255): |
|
left = min(left, x) |
|
right = max(right, x) |
|
top = min(top, y) |
|
bottom = max(bottom, y) |
|
|
|
# 裁剪图片 |
|
image = image.crop((left, top, right + 1, bottom + 1)) |
|
|
|
# 保存裁剪后的图片 |
|
image.save(output_path) |
|
|
|
def remove_background(image_path, output_path): |
|
# 打开图片 |
|
image = Image.open(image_path) |
|
|
|
# 将图片转换为带有透明通道的 RGBA 模式 |
|
image = image.convert("RGBA") |
|
|
|
# 获取图片的像素数据 |
|
data = image.getdata() |
|
|
|
# 新的像素数据列表 |
|
new_data = [] |
|
|
|
# 将白色背景的像素转换为透明 |
|
for item in data: |
|
# 如果像素为白色,则设置为完全透明 |
|
if item[:3] == (255, 255, 255): |
|
new_data.append((255, 255, 255, 0)) |
|
else: |
|
new_data.append(item) |
|
|
|
# 更新图片的像素数据 |
|
image.putdata(new_data) |
|
|
|
# 保存图片 |
|
image.save(output_path, "PNG") |
|
|
|
#生成预览图 |
|
def createImage(pid): |
|
|
|
|
|
target_folder = '/data/datasets/complate/preview' |
|
source_folder = f'/data/datasets/complate/objs/{pid}' |
|
|
|
for file_path in list_files(source_folder): |
|
if ".obj" not in file_path: |
|
continue |
|
#print("文件路径:",file_path) |
|
#判断文件是否存在 |
|
if not os.path.exists(file_path): |
|
continue |
|
|
|
|
|
|
|
# 加载OBJ文件 |
|
mesh = trimesh.load_mesh(file_path) |
|
|
|
# 生成封面图 |
|
image = mesh.scene().save_image(resolution=[400, 300]) |
|
# 转换为Pillow的Image对象 |
|
# pil_image = Image.frombytes('RGB', (image.width, image.height), image.data) |
|
# # 保存封面图 |
|
# pil_image.save(f'{target_folder}/{pid}_preview.png') |
|
#保存封面图 |
|
with open(f'{target_folder}/{pid}_preview.png', 'wb') as f: |
|
f.write(image) |
|
|
|
time.sleep(2) |
|
print("处理封面图片.....") |
|
remove_background(f'{target_folder}/{pid}_preview.png',f'{target_folder}/{pid}_preview.png') |
|
print("封面图片处理完成.....") |
|
time.sleep(1) |
|
|
|
oss_client.put_object_from_file(f'print_ticket_view/{pid}_preview.png', f'{target_folder}/{pid}_preview.png') |
|
|
|
break |
|
# 关闭虚拟显示 |
|
#display.stop() |
|
# if __name__ == "__main__": |
|
# AccessKeyId = 'LTAI5tSReWm8hz7dSYxxth8f' |
|
# AccessKeySecret = '8ywTDF9upPAtvgXtLKALY2iMYHIxdS' |
|
# Endpoint = 'oss-cn-shanghai.aliyuncs.com' |
|
# Bucket = 'suwa3d-website' |
|
# oss_client = oss2.Bucket(oss2.Auth(AccessKeyId, AccessKeySecret), Endpoint, Bucket) |
|
# pid = sys.argv[1] |
|
# target_folder = '/data/datasets/complate/preview' |
|
# source_folder = f'/data/datasets/complate/objs/{pid}' |
|
# createImage() |