pyvips icon indicating copy to clipboard operation
pyvips copied to clipboard

Assigning new colour component to an existent image

Open anaidasdfg opened this issue 4 years ago • 2 comments

I was trying to apply an histogram processing only in the H component of HSV colour space, but the assignment of the result to the specified component image_hsv[0] is producing an error for the image object.

import pyvips

image = pyvips.Image.new_from_file('image.jpg', access='sequential')
image_hsv= image.colourspace("hsv")
result = image_hsv[0].hist_local(40, 40, max_slope=5)
image_hsv[0] = result

TypeError: 'Image' object does not support item assignment

Any idea of how to performe an operation in only one component and reassign it to the original image? Thank you

anaidasdfg avatar May 31 '21 08:05 anaidasdfg

Hello @anaidasdfg,

libvips doesn't have destructive assignment -- you can only make new images, you can't update existing images.

Instead, you need to take the SV channels and attach the new H, so for example:

hsv = image.colourspace("hsv")
new_h = hsv[0].hist_local(40, 40, max_slope=5)
hsv = new_h.bandjoin(hsv[1:3])

jcupitt avatar May 31 '21 16:05 jcupitt

You could also consider using LCh rather than HSV. LCh is quite a bit more accurate, though of course that may not be important in your application.

https://en.wikipedia.org/wiki/CIELAB_color_space#Cylindrical_model

jcupitt avatar May 31 '21 16:05 jcupitt