@@ -142,6 +142,19 @@ def test_cmyk(self):
142
142
)
143
143
assert k > 0.9
144
144
145
+ def test_rgb (self ):
146
+ def getchannels (im ):
147
+ return tuple (v [0 ] for v in im .layer )
148
+
149
+ im = hopper ()
150
+ im_ycbcr = self .roundtrip (im )
151
+ assert getchannels (im_ycbcr ) == (1 , 2 , 3 )
152
+ assert_image_similar (im , im_ycbcr , 17 )
153
+
154
+ im_rgb = self .roundtrip (im , keep_rgb = True )
155
+ assert getchannels (im_rgb ) == (ord ("R" ), ord ("G" ), ord ("B" ))
156
+ assert_image_similar (im , im_rgb , 12 )
157
+
145
158
@pytest .mark .parametrize (
146
159
"test_image_path" ,
147
160
[TEST_FILE , "Tests/images/pil_sample_cmyk.jpg" ],
@@ -423,25 +436,28 @@ def getsampling(im):
423
436
return layer [0 ][1 :3 ] + layer [1 ][1 :3 ] + layer [2 ][1 :3 ]
424
437
425
438
# experimental API
426
- im = self .roundtrip (hopper (), subsampling = - 1 ) # default
427
- assert getsampling (im ) == (2 , 2 , 1 , 1 , 1 , 1 )
428
- im = self .roundtrip (hopper (), subsampling = 0 ) # 4:4:4
429
- assert getsampling (im ) == (1 , 1 , 1 , 1 , 1 , 1 )
430
- im = self .roundtrip (hopper (), subsampling = 1 ) # 4:2:2
431
- assert getsampling (im ) == (2 , 1 , 1 , 1 , 1 , 1 )
432
- im = self .roundtrip (hopper (), subsampling = 2 ) # 4:2:0
433
- assert getsampling (im ) == (2 , 2 , 1 , 1 , 1 , 1 )
434
- im = self .roundtrip (hopper (), subsampling = 3 ) # default (undefined)
435
- assert getsampling (im ) == (2 , 2 , 1 , 1 , 1 , 1 )
436
-
437
- im = self .roundtrip (hopper (), subsampling = "4:4:4" )
438
- assert getsampling (im ) == (1 , 1 , 1 , 1 , 1 , 1 )
439
- im = self .roundtrip (hopper (), subsampling = "4:2:2" )
440
- assert getsampling (im ) == (2 , 1 , 1 , 1 , 1 , 1 )
441
- im = self .roundtrip (hopper (), subsampling = "4:2:0" )
442
- assert getsampling (im ) == (2 , 2 , 1 , 1 , 1 , 1 )
443
- im = self .roundtrip (hopper (), subsampling = "4:1:1" )
444
- assert getsampling (im ) == (2 , 2 , 1 , 1 , 1 , 1 )
439
+ for subsampling in (- 1 , 3 ): # (default, invalid)
440
+ im = self .roundtrip (hopper (), subsampling = subsampling )
441
+ assert getsampling (im ) == (2 , 2 , 1 , 1 , 1 , 1 )
442
+ for subsampling in (0 , "4:4:4" ):
443
+ im = self .roundtrip (hopper (), subsampling = subsampling )
444
+ assert getsampling (im ) == (1 , 1 , 1 , 1 , 1 , 1 )
445
+ for subsampling in (1 , "4:2:2" ):
446
+ im = self .roundtrip (hopper (), subsampling = subsampling )
447
+ assert getsampling (im ) == (2 , 1 , 1 , 1 , 1 , 1 )
448
+ for subsampling in (2 , "4:2:0" , "4:1:1" ):
449
+ im = self .roundtrip (hopper (), subsampling = subsampling )
450
+ assert getsampling (im ) == (2 , 2 , 1 , 1 , 1 , 1 )
451
+
452
+ # RGB colorspace
453
+ for subsampling in (- 1 , 0 , "4:4:4" ):
454
+ # "4:4:4" doesn't really make sense for RGB, but the conversion
455
+ # to an integer happens at a higher level
456
+ im = self .roundtrip (hopper (), keep_rgb = True , subsampling = subsampling )
457
+ assert getsampling (im ) == (1 , 1 , 1 , 1 , 1 , 1 )
458
+ for subsampling in (1 , "4:2:2" , 2 , "4:2:0" , 3 ):
459
+ with pytest .raises (OSError ):
460
+ self .roundtrip (hopper (), keep_rgb = True , subsampling = subsampling )
445
461
446
462
with pytest .raises (TypeError ):
447
463
self .roundtrip (hopper (), subsampling = "1:1:1" )
0 commit comments