def SetField(self, tag, value, count=None):

        if isinstance(tag, str):
            tag = eval('TIFFTAG_' + tag.upper())
        t = tifftags.get(tag)
        if t is None:
            print 'Warning: no tag %r defined' % (tag)
            return
        data_type, convert = t
        if data_type == ctypes.c_float:
            data_type = ctypes.c_double

        if tag == TIFFTAG_COLORMAP:
            # ColorMap passes 3 values each a c_uint16 pointer
            try:
                r_arr,g_arr,b_arr = value
            except (TypeError, ValueError):
                print "Error: TIFFTAG_COLORMAP expects 3 uint16* arrays as a list/tuple of lists"
                r_arr,g_arr,b_arr = None,None,None
            if r_arr is None:
                return

            bps = self.GetField("BitsPerSample")
            if bps is None:
                print "Warning: BitsPerSample is required to get ColorMap, assuming 8 bps..."
                bps = 8
            num_cmap_elems = 1 << bps
            data_type = data_type * num_cmap_elems
            r_ptr = data_type(*r_arr)
            g_ptr = data_type(*g_arr)
            b_ptr = data_type(*b_arr)
            libtiff.TIFFSetField.argtypes = libtiff.TIFFSetField.argtypes[:2] + [ctypes.POINTER(data_type)]*3
            r = libtiff.TIFFSetField(self, tag, r_ptr, g_ptr, b_ptr)
        else:
            if issubclass(data_type, (ctypes.Array, tuple, list)):
                data = data_type(*value)
            elif issubclass(data_type, ctypes._Pointer): # does not include c_char_p
                # convert to the base type, ctypes will take care of actually
                # sending it by reference
                base_type = data_type._type_
                if isinstance(value, collections.Iterable):
                    data = base_type(*value)
                else:
                    data = base_type(value)
            else:
                data = data_type(value)

            # TODO: for most of the tags, count is len(value), so it shouldn't be needed
            if count is None:
                libtiff.TIFFSetField.argtypes = libtiff.TIFFSetField.argtypes[:2] + [data_type]
                r = libtiff.TIFFSetField(self, tag, data)
            else:
                libtiff.TIFFSetField.argtypes = libtiff.TIFFSetField.argtypes[:2] + [ctypes.c_uint, data_type]
                r = libtiff.TIFFSetField(self, tag, count, data)
        return r
Set TIFF field value with tag.
dataset/ETHPy150Open pearu/pylibtiff/libtiff/libtiff_ctypes.py/TIFF.SetField