Commits

Fredrik Lundh committed 8b41c8b

Added string aliases for subsampling modes

Comments (0)

Files changed (2)

PIL/JpegImagePlugin.py

 # 2003-09-13 fl   Extract COM markers
 # 2009-09-06 fl   Added icc_profile support (from Florian Hoech)
 # 2009-03-06 fl   Changed CMYK handling; always use Adobe polarity (0.6)
+# 2009-03-08 fl   Added subsampling support (from Justin Huff).
 #
 # Copyright (c) 1997-2003 by Secret Labs AB.
 # Copyright (c) 1995-1996 by Fredrik Lundh.
 
     dpi = info.get("dpi", (0, 0))
 
+    subsampling = info.get("subsampling", -1)
+    if subsampling == "4:4:4":
+        subsampling = 0
+    elif subsampling == "4:2:2":
+        subsampling = 1
+    elif subsampling == "4:1:1":
+        subsampling = 2
+
     # get keyword arguments
     im.encoderconfig = (
         info.get("quality", 0),
         info.has_key("optimize"),
         info.get("streamtype", 0),
         dpi[0], dpi[1],
-        info.get("subsampling", -1)
+        subsampling,
         )
 
     ImageFile._save(im, fp, [("jpeg", (0,0)+im.size, 0, rawmode)])

Tests/test_file_jpeg.py

     assert_equal(test(0), None) # square pixels
 
 def test_subsampling():
+    def getsampling(im):
+        layer = im.layer
+        return layer[0][1:3] + layer[1][1:3] + layer[2][1:3]
     # experimental API
     im = roundtrip(lena(), subsampling=-1) # default
-    assert_equal(im.layer, [('\x01', 2, 2, 0), ('\x02', 1, 1, 1), ('\x03', 1, 1, 1)])
+    assert_equal(getsampling(im), (2, 2, 1, 1, 1, 1))
     im = roundtrip(lena(), subsampling=0) # 4:4:4
-    assert_equal(im.layer, [('\x01', 1, 1, 0), ('\x02', 1, 1, 1), ('\x03', 1, 1, 1)])
+    assert_equal(getsampling(im), (1, 1, 1, 1, 1, 1))
     im = roundtrip(lena(), subsampling=1) # 4:2:2
-    assert_equal(im.layer, [('\x01', 2, 1, 0), ('\x02', 1, 1, 1), ('\x03', 1, 1, 1)])
+    assert_equal(getsampling(im), (2, 1, 1, 1, 1, 1))
     im = roundtrip(lena(), subsampling=2) # 4:1:1
-    assert_equal(im.layer, [('\x01', 2, 2, 0), ('\x02', 1, 1, 1), ('\x03', 1, 1, 1)])
+    assert_equal(getsampling(im), (2, 2, 1, 1, 1, 1))
     im = roundtrip(lena(), subsampling=3) # default (undefined)
-    assert_equal(im.layer, [('\x01', 2, 2, 0), ('\x02', 1, 1, 1), ('\x03', 1, 1, 1)])
+    assert_equal(getsampling(im), (2, 2, 1, 1, 1, 1))
+
+    im = roundtrip(lena(), subsampling="4:4:4")
+    assert_equal(getsampling(im), (1, 1, 1, 1, 1, 1))
+    im = roundtrip(lena(), subsampling="4:2:2")
+    assert_equal(getsampling(im), (2, 1, 1, 1, 1, 1))
+    im = roundtrip(lena(), subsampling="4:1:1")
+    assert_equal(getsampling(im), (2, 2, 1, 1, 1, 1))
+
+    assert_exception(TypeError, lambda: roundtrip(lena(), subsampling="1:1:1"))
 
 def test_truncated_jpeg():
     def test(junk):