python-pillow/Pillow — pull request #976 by hugovk (2014-10-26 16:52:45+00:00)
Title: Fix saving TIFF with PageNumber total = 0

Possible fix for https://github.com/python-pillow/Pillow/issues/973.

TIFF tag 297 is PageNumber and consists of two numbers. The first is the current page number. The second is the total number of pages, but can be zero if the total is not available.

http://www.awaresystems.be/imaging/tiff/tifftags/pagenumber.html

Here's a test case with a test image with a second value of zero:
```python
outfile = self.tempfile("temp.tif")
infile = "Tests/images/total-pages-zero.tif"
im = Image.open(infile)
im.save("test.tif")
```

This triggers a divide-by-zero error:
```
Traceback (most recent call last):
File "D:\projects\github\Pillow\tests\test_file_tiff.py", line 318, in test_page_number_x_0
im.save("test.tif")
File "C:\Python27\lib\site-packages\PIL\Image.py", line 1685, in save
save_handler(self, fp, filename)
File "C:\Python27\lib\site-packages\PIL\TiffImagePlugin.py", line 1165, in _save
atts[k] = float(v[0])/float(v[1])
ZeroDivisionError: float division by zero
```

This causes the problem:
```python
if type(v) == tuple and len(v) == 2:
# one rational tuple
# flatten to float,
# following tiffcp.c->cpTag->TIFF_RATIONAL
atts[k] = float(v[0])/float(v[1])
continue
```

The previous clause deals with tuples larger than two like this:

```python
if type(v) == tuple and len(v) > 2:
# List of ints?
if type(v[0]) in (int, float):
atts[k] = list(v)
continue
```

Should 2-tuples also be dealt with with this clause?

This fix just modifies it to also accept 2-tuples where the second value is zero:
```python
if (type(v) == tuple and
(len(v) > 2 or
(len(v) == 2 and v[1] == 0))):
# List of ints?
# Avoid divide by zero in next if-clause
if type(v[0]) in (int, float):
atts[k] = list(v)
continue
```