Python处理Texture

通过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("处理完毕")

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
在图像分割之后,可以对得到的分割结果进行特征处理。特征处理可以帮助我们提取图像中的有用信息,用于后续的分析和应用。下面是一些常见的图像分割后的特征处理方法,使用Python实现: 1. 颜色特征处理:可以通过计算每个分割区域的颜色直方图或颜色统计信息来表示该区域的颜色特征。可以使用OpenCV库中的函数来计算直方图或统计信息。 ```python import cv2 import numpy as np # 计算颜色直方图 def compute_histogram(image): hist = cv2.calcHist([image], [0, 1, 2], None, [256, 256, 256], [0, 256, 0, 256, 0, 256]) return hist # 计算颜色统计信息 def compute_color_statistics(image): mean = np.mean(image, axis=(0, 1)) std = np.std(image, axis=(0, 1)) return mean, std # 示例:计算颜色直方图和统计信息 image = cv2.imread('segmented_image.png') hist = compute_histogram(image) mean, std = compute_color_statistics(image) ``` 2. 纹理特征处理:可以通过计算每个分割区域的纹理特征,如灰度共生矩阵(GLCM)或局部二值模式(LBP)来表示。可以使用scikit-image库来计算纹理特征。 ```python from skimage.feature import greycomatrix, greycoprops import numpy as np # 计算灰度共生矩阵 def compute_glcm(image): gray = cv2.cvtColor(image, cv2.COLOR_BGR2GRAY) glcm = greycomatrix(gray, [1], [0], 256, symmetric=True, normed=True) return glcm # 计算纹理特征 def compute_texture_features(glcm): contrast = greycoprops(glcm, 'contrast') energy = greycoprops(glcm, 'energy') correlation = greycoprops(glcm, 'correlation') return contrast, energy, correlation # 示例:计算纹理特征 image = cv2.imread('segmented_image.png') glcm = compute_glcm(image) contrast, energy, correlation = compute_texture_features(glcm) ``` 3. 形状特征处理:可以通过计算每个分割区域的形状特征,如面积、周长、凸包等来表示。可以使用OpenCV库中的函数来计算形状特征。 ```python import cv2 # 计算形状特征 def compute_shape_features(contour): area = cv2.contourArea(contour) perimeter = cv2.arcLength(contour, True) hull = cv2.convexHull(contour) solidity = area / cv2.contourArea(hull) return area, perimeter, solidity # 示例:计算形状特征 image = cv2.imread('segmented_image.png') contours, _ = cv2.findContours(image, cv2.RETR_EXTERNAL, cv2.CHAIN_APPROX_SIMPLE) shape_features = [] for contour in contours: area, perimeter, solidity = compute_shape_features(contour) shape_features.append((area, perimeter, solidity)) ``` 这些是一些常见的图像分割后的特征处理方法的示例。根据具体的需求和应用场景,还可以使用其他特征处理方法来提取更多有用的信息。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值