下采样大型纹理文件
This commit is contained in:
parent
453afa971e
commit
8e01d63c7a
@ -5,6 +5,7 @@ from typing import Dict
|
|||||||
import pandas as pd
|
import pandas as pd
|
||||||
import shutil
|
import shutil
|
||||||
import time
|
import time
|
||||||
|
import cv2
|
||||||
|
|
||||||
|
|
||||||
class MergeObj:
|
class MergeObj:
|
||||||
@ -211,7 +212,7 @@ class MergeObj:
|
|||||||
return materials
|
return materials
|
||||||
|
|
||||||
def copy_and_rename_texture(self, src_dir: str, dst_dir: str, grid_id: tuple) -> dict:
|
def copy_and_rename_texture(self, src_dir: str, dst_dir: str, grid_id: tuple) -> dict:
|
||||||
"""复制并重命名纹理文件
|
"""复制并重命名纹理文件,对大于100MB的文件进行下采样
|
||||||
Args:
|
Args:
|
||||||
src_dir: 源纹理目录
|
src_dir: 源纹理目录
|
||||||
dst_dir: 目标纹理目录
|
dst_dir: 目标纹理目录
|
||||||
@ -224,15 +225,36 @@ class MergeObj:
|
|||||||
|
|
||||||
for file in os.listdir(src_dir):
|
for file in os.listdir(src_dir):
|
||||||
if file.lower().endswith(('.png', '.jpg', '.jpeg')):
|
if file.lower().endswith(('.png', '.jpg', '.jpeg')):
|
||||||
# 生成新的文件名:grid_0_1_texture.png
|
# 生成新的文件名
|
||||||
new_name = f"grid_{grid_id[0]}_{grid_id[1]}_{file}"
|
new_name = f"grid_{grid_id[0]}_{grid_id[1]}_{file}"
|
||||||
src_path = os.path.join(src_dir, file)
|
src_path = os.path.join(src_dir, file)
|
||||||
dst_path = os.path.join(dst_dir, new_name)
|
dst_path = os.path.join(dst_dir, new_name)
|
||||||
|
|
||||||
# 复制文件
|
# 检查文件大小(以字节为单位)
|
||||||
|
file_size = os.path.getsize(src_path)
|
||||||
|
if file_size > 100 * 1024 * 1024: # 大于100MB
|
||||||
|
self.logger.info(f"纹理文件 {file} 大于100MB,进行4倍下采样")
|
||||||
|
# 读取图像
|
||||||
|
img = cv2.imread(src_path, cv2.IMREAD_UNCHANGED)
|
||||||
|
if img is not None:
|
||||||
|
# 计算新的尺寸(长宽各变为1/4)
|
||||||
|
new_size = (img.shape[1] // 4, img.shape[0] // 4)
|
||||||
|
# 使用双三次插值进行下采样
|
||||||
|
resized_img = cv2.resize(img, new_size, interpolation=cv2.INTER_CUBIC)
|
||||||
|
# 保存压缩后的图像
|
||||||
|
if file.lower().endswith('.png'):
|
||||||
|
cv2.imwrite(dst_path, resized_img, [cv2.IMWRITE_PNG_COMPRESSION, 9])
|
||||||
|
else:
|
||||||
|
cv2.imwrite(dst_path, resized_img, [cv2.IMWRITE_JPEG_QUALITY, 95])
|
||||||
|
else:
|
||||||
|
self.logger.warning(f"无法读取图像文件: {src_path}")
|
||||||
shutil.copy2(src_path, dst_path)
|
shutil.copy2(src_path, dst_path)
|
||||||
|
else:
|
||||||
|
# 文件大小未超过100MB,直接复制
|
||||||
|
shutil.copy2(src_path, dst_path)
|
||||||
|
|
||||||
texture_map[file] = new_name
|
texture_map[file] = new_name
|
||||||
self.logger.debug(f"复制纹理文件: {file} -> {new_name}")
|
self.logger.debug(f"处理纹理文件: {file} -> {new_name}")
|
||||||
|
|
||||||
return texture_map
|
return texture_map
|
||||||
|
|
||||||
|
Loading…
Reference in New Issue
Block a user