Are you scaling the images in your paint package or in css?
Also are you only displaying the images at 40px all the time?
If so then you should create images in your paint package at 40px from a good quality original image and then use those instead. Unless this is a responsive scenario there’s no point in scaling the image down in CSS as you are still loading the full size image with all its overheads.
If this is a responsive situation then it seems weird that you would let the image go from 900px down to 40px? Scaling an image in CSS will always lead to a slight loss in quality but you need to start from a good quality image to start with at a larger size than you need and then it will scale down better. However scaling from 900px down to 40px is never going to be perfect. You could scale from say 200px down to 40px without being too blurred but of you go from very large to very small then expect a loss of quality.
You could use the picture element or srcset and swap images as the size gets smaller or larger so the best image is supplied for the width required
Or switch to svg created images (for logos and the like) if possible and there will be no scaling problems.