Skip to content

Commit 5580cf1

Browse files
committed
Added support for saving colormaps
1 parent 11b2085 commit 5580cf1

File tree

3 files changed

+35
-4
lines changed

3 files changed

+35
-4
lines changed

PIL/TiffTags.py

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -434,16 +434,13 @@ def _populate():
434434
269 # this has been in our tests forever, and works
435435
}
436436

437-
LIBTIFF_CORE.remove(320) # Array of short, crashes
438-
LIBTIFF_CORE.remove(301) # Array of short, crashes
439-
#LIBTIFF_CORE.remove(532) # Array of long, crashes
440-
441437
LIBTIFF_CORE.remove(330) # subifd, requires extra support for uint64 payload
442438

443439
LIBTIFF_CORE.remove(255) # We don't have support for subfiletypes
444440
LIBTIFF_CORE.remove(322) # We don't have support for tiled images in libtiff
445441
LIBTIFF_CORE.remove(323) # Tiled images
446442
LIBTIFF_CORE.remove(333) # Ink Names either
443+
LIBTIFF_CORE.remove(301) # Transfer Function. No support as of yet.
447444

448445
# Note to advanced users: There may be combinations of these
449446
# parameters and values that when added properly, will work and

Tests/test_file_libtiff.py

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -194,6 +194,7 @@ def test_additional_metadata(self):
194194
del(core_items[tag])
195195
except:
196196
pass
197+
del(core_items[320]) # colormap is special, tested below
197198

198199
# Type codes:
199200
# 2: "ascii",
@@ -354,6 +355,19 @@ def test_cmyk_save(self):
354355
im2 = Image.open(out)
355356
self.assert_image_equal(im, im2)
356357

358+
def test_palette_save(self):
359+
im = hopper('P')
360+
out = self.tempfile('temp.tif')
361+
TiffImagePlugin.WRITE_LIBTIFF = True
362+
im.save(out)
363+
TiffImagePlugin.WRITE_LIBTIFF = False
364+
365+
reloaded = Image.open(out)
366+
# colormap/palette tag
367+
self.assertTrue(len(reloaded.tag_v2[320]), 768)
368+
self.assert_image_equal(im, reloaded)
369+
370+
357371
def xtest_bw_compression_w_rgb(self):
358372
""" This test passes, but when running all tests causes a failure due
359373
to output on stderr from the error thrown by libtiff. We need to

encode.c

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -936,6 +936,26 @@ PyImaging_LibTiffEncoderNew(PyObject* self, PyObject* args)
936936
break;
937937
case TIFFTAG_COLORMAP:
938938
/* 3x uint16 * arrays of r,g,b palette values, len=2^^bpp */
939+
{
940+
int stride = 256;
941+
TRACE(("Setting ColorMap\n"));
942+
if (len != 768) {
943+
PyErr_SetString(PyExc_ValueError, "Requiring 768 items for for Colormap");
944+
return NULL;
945+
}
946+
arrav = calloc(len, sizeof(uint16));
947+
if (arrav) {
948+
for (i=0;i<len;i++) {
949+
((uint16 *)arrav)[i] = (uint16)PyInt_AsLong(PyTuple_GetItem(value,i));
950+
}
951+
status = ImagingLibTiffSetField(&encoder->state,
952+
tag,
953+
arrav,
954+
((uint16 *)arrav) + stride,
955+
((uint16 *)arrav) + 2 * stride);
956+
free(arrav);
957+
}
958+
}
939959
break;
940960
case TIFFTAG_SUBIFD:
941961
/* int short length, uint32* data */

0 commit comments

Comments
 (0)