Rafał Sardaw
Rafał Sardaw

Reputation: 121

Small change in colors during JPEG compression

It looks like default library under Ubuntu changes colors a bit during the compression. I tried to set quality and sampling but I see no improvements, anyone ever challenged similar issue?

subsampling = 0 , quality = 100

#CORRECT COLORS FROM NPARRAY
cv2.imshow("Object cam:{}".format(self.camera_id), self.out)

print(self.out.item(1,1,0)) # B 
print(self.out.item(1,1,1)) # G
print(self.out.item(1,1,2)) # R

self.out=cv2.cvtColor(self.out,  cv2.COLOR_BGR2RGB)

#from PIL import Image
im = Image.fromarray(self.out)
r, g, b = im.getpixel((1, 1))
## just printing pixel and they are matching  
print(r, g, b) 
## WRONG COLORS
im.save(self.out_ramdisk_img,format='JPEG', subsampling=0, quality=100)

enter image description here

JPEG image should have the same colors as in imshow, but it's a bit more purple.

Upvotes: 0

Views: 4368

Answers (1)

user3344003
user3344003

Reputation: 21627

That is a natural result of JPEG compression. JPEG uses floating point arithmetic to calculate integer pixel values. This occurs in several stages of JPEG compression. Thus, small pixel value changes are expected.

When you have blanket changes in color they are usually the result input color values that are outside the gamut of the YCbCr color space. Such values get clamped.

Upvotes: 1

Related Questions