def get_rawpixels_from_file(filename, scale_image=1): """ Using OpenImageIO get raw pixels from an image file for previewing purposes (uint8). """ import math # TODO: Migrate it outside callbacks.py try: import OpenImageIO as oiio except: print "Cant' find OpenImageIO." return None, None, None source = oiio.ImageBuf(str(filename)) if not source: return None, None, None # OIIO to get raw uint pixels rgb w = int(math.ceil(source.oriented_width * scale_image)) h = int(math.ceil(source.oriented_height * scale_image)) dest = oiio.ImageBuf(oiio.ImageSpec(w, h, 3, oiio.UINT8)) # DeLinearize optionally if source.spec().format in (oiio.TypeDesc(oiio.HALF), oiio.TypeDesc(oiio.FLOAT)): oiio.ImageBufAlgo.colorconvert(source, source, "linear", "sRGB") dest.copy(source, oiio.UINT8) roi = oiio.ROI(0, w, 0, h, 0, 1, 0, 3) pixels = dest.get_pixels(oiio.UINT8, roi) return pixels, w, h
def is_set_format_test(): print "Starting ImageSpec::set_format() tests..." desc = oiio.TypeDesc(oiio.BASETYPE.UNKNOWN, oiio.AGGREGATE.SCALAR, oiio.VECSEMANTICS.NOXFORM) desc2 = oiio.TypeDesc(oiio.BASETYPE.UINT8, oiio.AGGREGATE.VEC2, oiio.VECSEMANTICS.COLOR) spec = oiio.ImageSpec() #test1 (uses a simple TypeDesc as an argument) try: spec.set_format(desc) print "Test 1 passed" except: print "Test 1 failed" # test 2 (uses another TypeDesc as an argument) try: spec.set_format(desc2) print "Test 2 passed" except: print "Test 2 failed" # test 3 a = "not a proper argument" try: spec.set_format(a) print "Test 3 failed (works with a wrong argument)" except: print "Test 3 passed" print
def copy_image(in_filename, out_filename, method="image", memformat=oiio.FLOAT, outformat=oiio.UNKNOWN): input = oiio.ImageInput.open(in_filename) if not input: print 'Could not open "' + filename + '"' print "\tError: ", oiio.geterror() print return outspec = input.spec() if (outformat != oiio.UNKNOWN): outspec.format = oiio.TypeDesc(outformat) output = oiio.ImageOutput.create(out_filename) if not output: print "Could not create ImageOutput for", out_filename return ok = output.open(out_filename, outspec, oiio.Create) if not ok: print "Could not open", out_filename return ok = copy_subimage(input, output, method, memformat) input.close() output.close() if ok: print "Copied", in_filename, "to", out_filename, "as", method
def td_data_members_test(): print "Starting TypeDesc data members tests..." desc = oiio.TypeDesc() # test 1 if desc.basetype == 0: print "Test 1 passed" else: print "Test 1 failed" # test 2 if desc.aggregate == 1: print "Test 2 passed" else: print "Test 2 failed" # test 3 if desc.vecsemantics == 0: print "Test 3 passed" else: print "Test 3 failed" # test 4 if desc.arraylen == 0: print "Test 4 passed" else: print "Test 4 failed" print
def oiio_get_buf(source, hash=None, force=False): """Check and load a source image with OpenImageIO's format reader. Args: source (str): Path to an OpenImageIO compatible image file. hash (str): Specify the hash manually, otherwise will be generated. force (bool): When `true`, forces the buffer to be re-cached. Returns: ImageBuf: An `ImageBuf` instance or `None` if the image cannot be read. """ common.check_type(source, str) if hash is None: hash = common.get_hash(source) if not force and ImageCache.contains(hash, BufferType): return ImageCache.value(hash, BufferType) # We use the extension to initiate an ImageInput with a format # which in turn is used to check the source's validity if '.' not in source: return None ext = source.split('.').pop().lower() if ext not in get_oiio_extensions(): return None i = OpenImageIO.ImageInput.create(ext) if not i or not i.valid_file(source): i.close() return None # If all went well, we can initiate an ImageBuf config = OpenImageIO.ImageSpec() config.format = OpenImageIO.TypeDesc(OpenImageIO.FLOAT) buf = OpenImageIO.ImageBuf() buf.reset(source, 0, 0, config=config) if buf.has_error: print(buf.geterror()) return None ImageCache.setValue(hash, buf, BufferType) return buf
def is_init_test(): print "Starting ImageSpec constructors tests..." desc = oiio.TypeDesc() # test 1 spec1 = None spec1 = oiio.ImageSpec() # the default arg is TypeDesc::UNKNOWN if spec1 != None: print "Test 1 passed" else: print "Test 1 failed" # test 2 - give it a typedesc! spec2 = None try: spec2 = oiio.ImageSpec(desc) if spec2 != None: print "Test 2 passed" else: print "Test 2 failed (returns None)" except: print "Test 2 failed (raises argument mismatch exception)" # test 3 spec3 = None try: spec3 = oiio.ImageSpec(800, 600, 3, desc) if spec3 != None: print "Test 3 passed" else: print "Test 3 failed (returns None)" except: print "Test 3 failed (raises argument mismatch exception)" # test 4 test4 = "failed" a = "not a proper argument" try: spec4 = oiio.ImageSpec(a) except: test4 = "passed" print "Test 4", test4 print
###################################################################### # main test starts here try: # Test that all the enum values exist basetype_enum_test() aggregate_enum_test() vecsemantics_enum_test() print("") # Exercise the different constructors, make sure they create the # correct TypeDesc (also exercises the individual fields, c_str(), # conversion to string). breakdown_test(oiio.TypeDesc(), "(default)") breakdown_test(oiio.TypeDesc(oiio.UINT8), "UINT8") breakdown_test(oiio.TypeDesc(oiio.HALF, oiio.VEC3, oiio.COLOR), "HALF, VEC3, COLOR") breakdown_test(oiio.TypeDesc(oiio.FLOAT, oiio.SCALAR, oiio.NOXFORM, 6), "FLOAT, SCALAR, NOXFORM, array of 6") breakdown_test(oiio.TypeDesc(oiio.FLOAT, oiio.VEC3, oiio.POINT, 2), "FLOAT, VEC3, POINT, array of 2") print("") # Test construction from a string descriptor breakdown_test(oiio.TypeDesc("float[2]"), "float[2]") breakdown_test(oiio.TypeDesc("normal"), "normal") breakdown_test(oiio.TypeDesc("uint16"), "uint16") print("")