• 使用 Python脚本在3DMAX中加载图像和读取图像中的像素值


    如何使用Python在3dmax中加载和显示图像文件?我们先看下面的代码:

    *测试的3dmax文件和图像文件位于同一目录中。

    1. from MaxPlus import BitmapManager
    2. image_file_path = r'je_gray_02_4k.exr'
    3. bmp_storage = MaxPlus.Factory.CreateStorage(17)
    4. bmp_info = bmp_storage.GetBitmapInfo()
    5. bmp_info.SetName(image_file_path)
    6. bmp = BitmapManager.Load(bmp_info)
    7. bmp.Display()

    打开3dmax,点击3dmax主菜单->脚本->新建脚本,将上面的Python代码复制粘贴到MAXScript脚本编辑器窗口中,点击脚本编辑器窗口上面的“语言”菜单,选择“Python”。然后,同时按下“Ctrl+e”键执行Python脚本,运行结果如下图:    

    下面一步一步解释上面的脚本:

    1.导入加载图像文件所需的BitmapManager类。

    2.设置包含图像文件路径的变量

    3.呼叫MaxPlus。Factory类的CreateStorage方法来启动BitmapStorage对象。

    这太尴尬了。。

    很可能我只是没有找到正确的方法。。

    除了启动BitmapStorage对象并引用其BitmapInfo对象外,貌似找不到任何其他方法来独立启动加载图像所需的BitmapInfo。(BitmapInfo类没有构造函数。)

    4.获取对BitmapStorage对象中包含的BitmapInfo对象的引用。

    5.设置BitmapInfo对象的名称属性(完整文件路径)。

    6.加载图像。

    7.在3dmax的图像查看器窗口中显示图像。

    下面是BitmapStorage格式常量容器类的示例代码:

    1. class BitmapTypes(object):
    2.      BMM_NO_TYPE = 0 # Not allocated yet
    3.      BMM_LINE_ART = 1 # 1-bit monochrome image
    4.      BMM_PALETTED = 2 # 8-bit paletted image. Each pixel value is an index into the color table.
    5.      BMM_GRAY_8 = 3 # 8-bit grayscale bitmap.
    6.      BMM_GRAY_16 = 4 # 16-bit grayscale bitmap.
    7.      BMM_TRUE_16 = 5 # 16-bit true color image.
    8.      BMM_TRUE_32 = 6 # 32-bit color: 8 bits each for Red, Green, Blue, and Alpha.    
    9.      BMM_TRUE_64 = 7 # 64-bit color: 16 bits each for Red, Green, Blue, and Alpha.
    10.      BMM_TRUE_24 = 8 # 24-bit color: 8 bits each for Red, Green, and Blue. Cannot be written to.
    11.      BMM_TRUE_48 = 9 # 48-bit color: 16 bits each for Red, Green, and Blue. Cannot be written to.
    12.      BMM_YUV_422 = 10 # This is the YUV format - CCIR 601. Cannot be written to.
    13.      BMM_BMP_4 = 11 # Windows BMP 16-bit color bitmap. Cannot be written to.
    14.      BMM_PAD_24 = 12 # Padded 24-bit (in a 32 bit register). Cannot be written to.
    15.      BMM_LOGLUV_32 = 13 BMM_LOGLUV_24 = 14
    16.      BMM_LOGLUV_24A = 15 BMM_REALPIX_32 = 16 # The 'Real Pixel' format.
    17.      BMM_FLOAT_RGBA_32 = 17 # 32-bit floating-point per component (non-compressed),
    18.      RGB with or without alpha
    19.      BMM_FLOAT_GRAY_32 = 18 # 32-bit floating-point (non-compressed), monochrome/grayscale
    20.      BMM_FLOAT_RGB_32 = 19
    21.      BMM_FLOAT_A_32 = 20

    如何从图像中读取像素值?看下面的Python代码:

    1. bmp_storage = bmp.GetStorage()
    2. hdr_pixel = bmp_storage.GetHDRPixel(3000,200)
    3. print(hdr_pixel)

    将这段代码追加到之前代码的末尾,完整代码如下:

    1. from MaxPlus import BitmapManager
    2. image_file_path = r'je_gray_02_4k.exr'
    3. bmp_storage = MaxPlus.Factory.CreateStorage(17)
    4. bmp_info = bmp_storage.GetBitmapInfo()
    5. bmp_info.SetName(image_file_path)
    6. bmp = BitmapManager.Load(bmp_info)
    7. bmp.Display()
    8.           
    9. #reading pixels
    10. bmp_storage = bmp.GetStorage()
    11. hdr_pixel = bmp_storage.GetHDRPixel(3000,200)
    12. print(hdr_pixel)

    执行Python代码,结果如下:    

    代码释义:

    1.获取对位图的BitmapStorage对象的引用。

    *在这种情况下,重写我们之前创建的BitmapStorage对象只是为了获得BitmapInfo对象。。

    2.读取像素值。

    提示:复制和粘贴此示例中的脚本时,请注意格式的缩进。

  • 相关阅读:
    41、基于深度学习的自适应线性预测(matlab)
    机器视觉工程师努力工作确实不一定涨工资,但是努力工作,确实有很大可能涨工资
    Year 2038 problem
    代码检查过程中为什么需要涉及到编译呢?
    使用 vue-element-admin 开发后台管理系统【安装】
    Ansible循环与判断
    华为云CDN,如何推动互联网行业健康发展?
    Spring的BeanDefinition的作用和使用方法
    【尚硅谷 Vue学习笔记】P5 hello小案例 P6分析Hello案例 P7模板语法 P8数据绑定
    Mybatis-Plus实体类继承Model的使用
  • 原文地址:https://blog.csdn.net/mufenglaoshi/article/details/134558246