0

From Python 2.7, I'm using pylibtiff developed by Pearu Peterson. I have installed libtiff3.dll in my path etc. I know that pylibtiff and libtiff are more than 5 years old, but it allows me to control the writing of TIFF files more than I can do with e.g. tiffile.py developed by Christoph Gohlke. I prefer to deal with the data row by row and this is possible when I use RowsPerStrip=1.

Thus I'm able to read TIFF files strip by strip but not to write in the same way. I'd like to write my data to disk in the same way, with data in a buffer of limited size. Eventually I hope to apply my code to large files. The second time the method TIFFWriteEncodedStrip is invoked via the libtiff method WriteEncodedStrip, a "WindowsError: exception: integer divide by zero" occurs. What am I doing wrong?

from libtiff import TIFF
width = 600
height = 600
tif = TIFF.open('filename.tif', mode='w')
tif.SetField("ImageWidth", width)
tif.SetField("ImageLength", height)

# Set CCITTFAX3 settings etc.
tif.SetField("ImageWidth", width)
tif.SetField("ImageLength", height)
tif.GetField("RowsPerStrip", 1)
...

for i in range(height):
    # fill buffer
    buf = ...
    bytesPerStrip = math.ceil(width / 8)
    tif.WriteEncodedStrip(i, buf.ctypes.data, int(bytesPerStrip))

tif.WriteDirectory()
tif.close()
Dobedani
  • 508
  • 5
  • 20
  • please give more information about the errors. – luoluo Aug 28 '15 at 15:21
  • Is this the entire script? That error doesn't make sense to me either – FirebladeDan Aug 28 '15 at 15:23
  • Ok, this is what I can add: method WriteEncodedStrip is defined in source file libtiff_ctypes.py on line 803. It's not the entire script, but the buffer is a proper numpy array of 600 length. I've tried giving it shape (600, 1) but to no avail. – Dobedani Aug 28 '15 at 15:31
  • 2
    Are you sure `tif.*G*etField("RowsPerStrip", 1)` is correct (ie. **GET** not SET)? – Harald K Aug 28 '15 at 19:06
  • @haraldK: thank you for pointing out this mistake of mine. Besides the method WriteStrip needed this as the last argument: – Dobedani Aug 31 '15 at 16:40
  • size = width * samples_per_pixel * data_sequence.itemsize; WriteStrip(i, data_sequence.ctypes.data, int(size)); I'm now able to write TIFF the way I want, although I'm now trying to see how I can retrieve a colormap from a source file - e.g. in BMP format - so that I can add a proper colormap to my TIFF files also and see exactly the same colors. I'll post a new question ... – Dobedani Aug 31 '15 at 16:49

0 Answers0