3
0

test_oiio.py 2.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566
  1. # coding:utf-8
  2. #!/usr/bin/python
  3. #
  4. # Copyright (c) Contributors to the Open 3D Engine Project.
  5. # For complete copyright and license terms please see the LICENSE at the root of this distribution.
  6. #
  7. # SPDX-License-Identifier: Apache-2.0 OR MIT
  8. #
  9. #
  10. # -- This line is 75 characters -------------------------------------------
  11. import OpenImageIO as oiio
  12. def convert(src):
  13. try:
  14. rgba = oiio.ImageBuf(src)
  15. spec = get_image_spec(rgba)
  16. # Normal output ------>
  17. rgb = oiio.ImageBufAlgo.channels(rgba, (0, 1, 2))
  18. normal_output = src.replace('ddna', 'Normal')
  19. # Roughness output ------>
  20. alpha = oiio.ImageBufAlgo.channels(rgba, (3,))
  21. roughness = oiio.ImageBufAlgo.invert(alpha)
  22. roughness_output = normal_output.replace('Normal', 'Roughness')
  23. write_image(rgba, normal_output, spec['format'])
  24. write_image(roughness, roughness_output, spec['format'])
  25. # logging.info('Output Normal Map: {}'.format(normal_output))
  26. # logging.info('Output Roughness Map: {}'.format(roughness_output))
  27. except Exception as e:
  28. logging.info('OIIO error in image conversion: {}'.format(e))
  29. return None
  30. def get_image_spec(target_image):
  31. spec = target_image.spec()
  32. info = {'resolution': (spec.width, spec.height, spec.x, spec.y), 'channels': spec.channelnames,
  33. 'format': str(spec.format)}
  34. if spec.channelformats :
  35. info['channelformats'] = str(spec.channelformats)
  36. info['alpha channel'] = str(spec.alpha_channel)
  37. info['z channel'] = str(spec.z_channel)
  38. info['deep'] = str(spec.deep)
  39. for i in range(len(spec.extra_attribs)):
  40. if type(spec.extra_attribs[i].value) == str:
  41. info[spec.extra_attribs[i].name] = spec.extra_attribs[i].value
  42. else:
  43. info[spec.extra_attribs[i].name] = spec.extra_attribs[i].value
  44. return info
  45. def write_image(image, filename, image_format):
  46. if not image.has_error:
  47. image.set_write_format(image_format)
  48. image.write(filename)
  49. if image.has_error:
  50. print("Error writing", filename, ":", image.geterror())
  51. ###########################################################################
  52. # Main Code Block, runs this script as main (testing)
  53. # -------------------------------------------------------------------------
  54. if __name__ == '__main__':
  55. # run a test
  56. convert('AA_Gun_01_ddna.tif')
  57. # validate()