通过Image和ImageFile库访问一张贴图的信息
from PIL import Image
from PIL import ImageFile
import os
#这行代码主要解决贴图读取错误的问题
ImageFile.LOAD_TRUNCATED_IMAGES = True
tex = []
def ir(path):
print("正在读取贴图并进行分类")
#利用递归,检索文件夹下所有文件。
parents = os.listdir(path)
for parent in parents:
child = os.path.join(path, parent)
if os.path.isdir(child):
ir(child)
else:
if os.path.splitext(child)[1] == ".png":
tex.append(child)
print("text path : {}".format(child))
else:
pass
def sortProcessing(lis2,pd_t):
count = 0
for i in lis2:
count += 1
print("已经处理{}张贴图".format(count))
#根据贴图种类,缩小的尺寸不同
im = Image.open(i)
if im.size[0] >= 1024 or im.size[1] >= 1024 :
print("image size before :{}".format(im.size))
_width = round(im.size[0]/2)
_height = round(im.size[1]/2)
new_im = im.resize((_width,_height),resample = Image.Resampling.LANCZOS)
new_im.save(i)
print("image size before :{}".format(new_im.size))
return 0
def fixed4(list):
count = 0
for i in list:
count += 1
print("已经处理{}张贴图".format(count))
#根据贴图种类,缩小的尺寸不同
im = Image.open(i)
if im.size[0] % 4 != 0 or im.size[1] % 4 != 0:
print("image size before :{}".format(im.size))
_width = im.size[0] - im.size[0]%4
_height = im.size[1] - im.size[1]%4
new_im = im.resize((_width,_height),resample = Image.Resampling.LANCZOS)
new_im.save(i)
print("image size before :{}".format(new_im.size))
return 0
def fixed22(list):
count = 0
for i in list:
count += 1
print("已经处理{}张贴图".format(count))
#根据贴图种类,缩小的尺寸不同
im = Image.open(i)
print("image size before :{}".format(im.size))
_width = im.size[0] + 200
_height = im.size[1] + 50
new_im = im.resize((_width,_height),resample = Image.Resampling.LANCZOS)
new_im.save(i)
print("image size before :{}".format(new_im.size))
return 0
path1 = r"/Users/hcg/Downloads/frame"
ir(path1)
# sortProcessing(tex,(1024,1024))
# fixed4(tex)
fixed22(tex)
print("处理完毕")