How do I edit color space of an existing image?

Q: My app is coming along, and I'm beginning to discover the power or
the library, as well
as come up with ideas how to implement it in some of my other existing
apps, for now though, I would like to know if there is a way of
changing certain entries in object. I'm sure there is as I have seen
evidence of it, I think, but I just have not been able to accomplish
it myself.

What I want to do is take an image object and change the colorspace
entry.

Say the object has ... \Colorspace 9 0 R.... I create a new spot
colorspace that is object number 86. Can I pull the images object,
and tell it to use \Colorspace 86 0 R instead and
then push that object back into the page without creating a new object
and
blowing up the size of the file.
-----
A: You can replace the color space in an existing image object as
follows:

Obj my_cs = ... create color space
my_image.GetSDFObj().Put("ColorSpace", my_cs);

The above line will replace the color space entry in the image
dictionary. The change will be automatically applied to all pages that
are referencing the image. Please keep in mind is that this will only
work if the two (the old and the new) color spaces are compatible
(i.e. have the same numbers of samples, bits per sample, etc.). So
replacing a spot color space with another sport color space with the
same number of components is fine, however replacing a RGB with a CMYK
color space will result in a corrupt image (in this case you would
need to replace the entire image object by re-sampling the data,
creating a new image xobject, and swapping the two objects).