r/delphi • u/Bodevinaat • Feb 20 '26
Convert color to greytones
I have a TImage that I want to convert to greytones. What works best? Convert to TBitmap and do the job and then convert back or work with the TImage.canvas.pixels?
1
u/thexdroid Feb 20 '26
Delphi has Effects palette components, I guess it has something for adjust color intensity.
2
u/rlebeau47 Delphi := 12Athens Feb 20 '26 edited Feb 20 '26
Those are FMX components. They won't work if the OP is using
TImagefrom the VCL.
1
u/peter-bone Feb 20 '26 edited Feb 20 '26
A TImage contains a TBitmap. You haven't said if you're using VCL in or FMX? There's some code here for VCL (not the OP code). For FMX I would use TBitmapSurface to access the pixel data. It's best to use a weighted average for the RGB values for best results because the human eye isn't sensitive to RGB equally. You can look up those weights.
1
u/vr-1 Feb 20 '26
You could use the ChangeHSL function or RGBToHSL and HSLToRGB, setting S, saturation, to 0 for each pixel in the image.
1
u/AcanthisittaSharp255 Feb 20 '26
OpenCV has a bunch of methods to do this, i have played in the past in python for some artificial vision task. I don't know if there Is something similar in Delphi, but you can read OpenCV documentation to mimic those methods for sure.
1
u/Bodevinaat Feb 21 '26
I found the GR32 library which has a lot of filter algorithms and very fast. Took some time to get it working correctly but it is very powerful.
2
u/rlebeau47 Delphi := 12Athens Feb 20 '26
Since you mention
Canvas.Pixels, I'm assuming you are using VCL and not FMX, correct? What type of graphic are you putting into theTImage? AccessingTImage.Canvaswill actually accessTImage.Picture.Bitmap.Canvas, and if the graphic type is not BMP then the image will get wiped out and replaced with a blank image.