def converter(filepath): out_filePath= r'L:\temp\xyz\converted_v2.jpg' in_buf_data = oiio.ImageBuf(filepath) in_buf_roi = oiio.get_roi(in_buf_data.spec()) in_buf_rgb = oiio.ImageBufAlgo.channels(in_buf_data, (0, 1, 2)) # Remove other channels(multi-channels exr) in_array_rgb = in_buf_rgb.get_pixels() colourspace_acescg = colour.RGB_COLOURSPACES['ACEScg'] colourspace_srgb = colour.RGB_COLOURSPACES['sRGB'] # conversion = colour.XYZ_to_sRGB(in_array_rgb) conversion = colour.XYZ_to_RGB(in_array_rgb, colourspace_acescg.whitepoint, colourspace_acescg.whitepoint, colourspace_acescg.XYZ_to_RGB_matrix) conversion_srgb = colour.RGB_to_RGB(conversion, colourspace_acescg, colourspace_srgb, 'Bradford', apply_cctf_encoding= True) converted_array_rgba = conversion_srgb in_buf_rgb.set_pixels(in_buf_roi, converted_array_rgba) # Replace the buffer with the converted pixels if in_buf_data.spec().alpha_channel > 0: # If image has an alpha channel in_buf_alpha = oiio.ImageBufAlgo.channels(in_buf_data, (3,)) # copy the Alpha channel in_buf_rgba = oiio.ImageBufAlgo.channel_append(in_buf_rgb, in_buf_alpha) # Merge the alpha channel back else: in_buf_rgba = in_buf_rgb in_buf_rgba.specmod().attribute("compression", "jpg:100") in_buf_rgba.specmod().attribute("oiio:ColorSpace", 'sRGB') bitdepth = 'uint8' in_buf_rgba.set_write_format(bitdepth) in_buf_rgba.write(out_filePath)
def __init__(self, in_img_path, out_bitdepth, in_cs, out_cs, odt, resources_path, compression, cctf, cctf_encoding, out_file_path): """ Args: in_img_path: path/name.ext of the file out_bitdepth: bitdepth for the output file: uint8,uint16,half,float, original in_cs: source colorspace [0] item from the IDT dico out_cs: target colorspace : key from the ODT dico compression: Compression method for exr: none, rle, zip, zips, piz, pxr24, b44, b44a, dwaa, or dwab odt: the Output Display Transform to apply (False if not wanted) cctf: Bool : Apply in cs decoding colour component transferfunction/electro-optical transferfunction. """ self.cctf = cctf self.cctf_encoding = cctf_encoding self.resources_path = resources_path self.in_img_path = in_img_path self.out_bitdepth = out_bitdepth self.in_cs = in_cs self.out_cs = out_cs self.odt = odt self.compression = compression self.out_filePath = out_file_path oiio.attribute("threads", 0) oiio.attribute("exr_threads", 0)
def remove_model(img): buf = oiio.ImageBuf(img) Make = buf.spec().get_string_attribute("Make") Model = buf.spec().get_string_attribute("Model") print(f"Removing Make: {Make}, Model: {Model}") buf.spec().attribute("Make", "") buf.spec().attribute("Model", "") buf.write(img)
def image_dimensions(image_filepath): """Get image dimensions. params: image_filepath: str returns int, int: width, height """ img_src = oiio.ImageBuf(image_filepath) rgb_img_src = oiio.ImageBuf() oiio.ImageBufAlgo.channels(rgb_img_src, img_src, (0, 1, 2)) img_width = rgb_img_src.spec().full_width img_height = rgb_img_src.spec().full_height return img_width, img_height
def report_size(source): try: buf = oiio.ImageBuf(source) spec = buf.spec() w = spec.width h = spec.height m = max(w, h) return m except BaseException as e: print(f"Get size failed: {e}")
def inplace_resize(source, goal_width=100, goal_height=100): #resize to 100 x 100 print(f"working on {source}") try: buf = oiio.ImageBuf(source) spec = buf.spec() w = spec.width h = spec.height # https://openimageio.readthedocs.io/en/latest/imagebuf.html if (w == 0 or h == 0): #not a good photo return False if float(h) > 0: aspect = float(w) / float(h) if aspect >= 1.0: # source image is landscape (or square) goal_height = int(h * goal_height / w) else: # source image is portrait goal_width = int(w * goal_width / h) # resized = oiio.ImageBuf(spec) resized = oiio.ImageBuf( oiio.ImageSpec(goal_width, goal_height, spec.nchannels, spec.format)) oiio.ImageBufAlgo.resize(resized, buf) #orientaion resized.orientation = buf.orientation #make/model Make = buf.spec().get_string_attribute("Make") Model = buf.spec().get_string_attribute("Model") resized.spec().attribute("Make", Make) resized.spec().attribute("Model", Model) resized.write(source) except BaseException as e: print(f"Resize failed: {e}") return True
def image_processing(self): logging.info(f"\n Image processing: {self.in_img_path} \n -IDT:{self.in_cs}, cctf_decoding:{self.cctf} \n" f" --OutColorspace: {self.out_cs} \n" f" ---Export: Output:'{self.out_filePath}', Bitdepth:{self.out_bitdepth} \n ----ODT:{self.odt} ") # Read Image in_buf_data = oiio.ImageBuf(self.in_img_path) in_buf_roi = oiio.get_roi(in_buf_data.spec()) if in_buf_data.has_error: logging.error(f"Error in conversion: Read: {in_buf_data.geterror()}") return_list = [False, in_buf_data.geterror()] return return_list in_buf_rgb = oiio.ImageBufAlgo.channels(in_buf_data, (0, 1, 2)) # Remove other channels(multi-channels exr) in_array_rgb = in_buf_rgb.get_pixels() converted_array_rgba = self.pixel_processing(in_array_rgb, cctf_decoding=self.cctf, cctf_encoding=self.cctf_encoding) in_buf_rgb.set_pixels(in_buf_roi, converted_array_rgba) # Replace the buffer with the converted pixels if in_buf_rgb.has_error: logging.error(f"Error in conversion: buf_rgb: {in_buf_rgb.geterror()}") return_list = [False, in_buf_rgb.geterror()] return return_list if in_buf_data.spec().alpha_channel > 0: # If image has an alpha channel in_buf_alpha = oiio.ImageBufAlgo.channels(in_buf_data, (3,)) # copy the Alpha channel in_buf_rgba = oiio.ImageBufAlgo.channel_append(in_buf_rgb, in_buf_alpha) # Merge the alpha channel back else: in_buf_rgba = in_buf_rgb in_buf_rgba.specmod().attribute("compression", self.compression.lower()) in_buf_rgba.specmod().attribute("oiio:ColorSpace", self.out_cs if not ODT_DICO.get(self.odt) else self.odt) bitdepth = self.bitdepth_picker(in_buf_data.nativespec().format, self.out_bitdepth) in_buf_rgba.set_write_format(bitdepth) if in_buf_rgba.has_error: logging.error(f"Error in conversion: attributes: {in_buf_rgba.geterror()}") return_list = [False, in_buf_rgba.geterror()] return return_list in_buf_rgba.write(self.out_filePath) # self.convert_progress.emit() return_list = [True] return return_list
def convert(img): filename, ext = os.path.splitext(img) outfile = filename + ".png" if ext == '.png': return if ext in ['.PNG']: os.rename(img, outfile) else: buf = oiio.ImageBuf(img) buf.write(outfile) os.remove(img)