pytiff is a lightweight library for reading chunks from a tiff file

Overview

pytiff

pytiff is a lightweight library for reading chunks from a tiff file. While it supports other formats to some extend, it is focused on reading tiled greyscale/rgb images, that can also be bigtiffs. Writing tiff files is now partly supported. It is able to save uncompressed greyscale images in tiled or scanline format. More information can be found in the documentation. Pytiff supports numpy like slicing and uses numpy arrays to handle image data.

The libtiff library is wrapped using the Cython package.

develop : Build Status master: Build Status coverage: Coverage Status doc: Documentation Status

Dependencies

  • libtiff C library (>= 4.0 for bigtiff access)
  • Cython >= 0.23
  • numpy

Installation

Installation on Linux / Mac

Just use pip

pip install pytiff

or from sources: clone the repo and call setup.py.

git clone https://github.com/FZJ-INM1-BDA/pytiff.git
cd pytiff
pip install -r requirements.txt
pip install . # or python setup.py install

Installation on Windows

Recommended: Use Anaconda

* Install Anaconda or Miniconda
* Install [Microsoft Visual C++ Build Tools](http://landinghub.visualstudio.com/visual-cpp-build-tools)
* Install Visual C++ using Visual Studio (development headers..)
* Set your INCLUDE environment variable to "path\to\anaconda\Library\include"
* Set LIB environment variable to point to "tiff.lib" or Copy tiff.lib from "Anaconda\Library\lib\" into the installation directory
* Start Visual Studio Development CMD
* `python setup.py install`

Development

For development:

git clone https://github.com/FZJ-INM1-BDA/pytiff.git
cd pytiff
# git checkout develop
pip install -r requirements.txt
pip install -e . # or python setup.py develop

can be used, so that no reinstallation is needed for every update. If new updates are pulled the cython part has to be recompiled.

#compile cython part
python setup.py build_ext --inplace

Usage

A small example how pytiff can be used:

Reading:

import pytiff

with pytiff.Tiff("test_data/small_example_tiled.tif") as handle:
  part = handle[100:200, :]

Writing data from pages into a multipage tiff:

import pytiff
with pytiff.Tiff("test_data/tmp.tif", "w") as handle:
  for p in pages:
    handle.write(p, method="scanline")
Comments
  • Encode and decode tags with encoding parameter

    Encode and decode tags with encoding parameter

    I've changed the type casting of tags as strings decoded using an encoding parameter passed to pytiff.Tiff. This will prevent an error on unicode tags in python 3 and will resolve issue #16. This new behavior will allow users to decode the tag values into their preferred encoding. Setting tags also uses the encoding parameter.

    opened by thejohnhoffer 2
  • Missing dependency: Cython

    Missing dependency: Cython

    I found out after installing pytiff that the package cython was missing as a dependency because I did not install it previously.

    The error message (under Windows and Python 3.7.2) with pytiff-0.8.0 said "Cython modules not available". Could you please make sure that during the installation the installer setup checks the availabillity of cython?

    opened by strpeter 2
  • Problems with sliced arrays and method = 'scanline'

    Problems with sliced arrays and method = 'scanline'

    I encountered this proplem as I tried to save a 3D np.array as a stack of tifs.

    What I essentially did was this:

    img_stack = np.zeros((100,100,50), dtype = 'uint8')
    img_stack[30:60,30:60,20:30] = 255
    
    for ind in range(50):
        img_slice = img_stack[:,:,ind]
        with pytiff.Tiff(file_path.format(ind), 'w') as handle:
            handle.write(img_slice, method='scanline')
    

    This produces same strange artefacts, when used on a regular image stack. On this particular 'image stack' it should produce all black tifs, if the problem is reproduceable on other machines.

    The problem does not occur, when the flag method is set to 'tile'. I fixed this py replacing the line img_slice = img_stack[:,:,ind] with img_slice = img_stack[:,:,ind].copy()

    opened by Sifeiros 1
  • pip install pytiff fails when numpy is not available

    pip install pytiff fails when numpy is not available

    pip install pytiff
    Downloading/unpacking pytiff Downloading pytiff-0.7.0.tar.gz (1.3MB): 1.3MB downloaded Running setup.py (path:/tmp/pip-build-3abmxk7b/pytiff/setup.py) egg_info for package pytiff Traceback (most recent call last): File "/tmp/pip-build-3abmxk7b/pytiff/setup.py", line 13, in import numpy ImportError: No module named 'numpy'

    During handling of the above exception, another exception occurred:
    
    Traceback (most recent call last):
      File "<string>", line 17, in <module>
      File "/tmp/pip-build-3abmxk7b/pytiff/setup.py", line 15, in <module>
        raise Exception("Numpy is needed for installation")
    Exception: Numpy is needed for installation
    Complete output from command python setup.py egg_info:
    Traceback (most recent call last):
    

    File "/tmp/pip-build-3abmxk7b/pytiff/setup.py", line 13, in

    import numpy
    

    ImportError: No module named 'numpy'

    During handling of the above exception, another exception occurred:

    Traceback (most recent call last):

    File "", line 17, in

    File "/tmp/pip-build-3abmxk7b/pytiff/setup.py", line 15, in

    raise Exception("Numpy is needed for installation")
    

    Exception: Numpy is needed for installation

    opened by ylep 1
  • test_data/bigtif_example_tiled.tif is not a Bigtiff

    test_data/bigtif_example_tiled.tif is not a Bigtiff

    I was looking for an example of a 'tiled' Bigtiff file, so naturally I opened test_data/bigtif_example_tiled.tif from this repo:

    In [90]: hex( ord( open('test_data/bigtif_example_tiled.tif', 'r').read(4)[2] ) )
    Out[90]: '0x2a'
    

    Whoops, the third byte of a Bigtiff file should be 0x2b, not 0x2a.[1]

    [1]: http://bigtiff.org/

    opened by stuarteberg 1
  • Only pad if it is really necessary.

    Only pad if it is really necessary.

    I noticed that pytiff spends a lot of time for padding data before writing it to file. In the current implementation, padding is always applied, even if the padding is zero in all dimensions.

    I modified the implementation to only pad when padding is actually necessary. In the process, I noticed that pytiff seems to "abuse" the padding to convert the incoming buffer from Fortran memory layout to C memory layout. Thus, if no padding is applied, we have to do this conversion explicitly.

    The check for padding is not the prettiest, but using np.sum instead introduces a significant overhead.

    In my tests (writing a chunked TIFF file, 6000x7000 pixels, plus pyramid), the change reduces the time for pytiff's __setitem__ from 8.93 seconds to 0.807 seconds (so ~10 fold speedup). The results are identical (verified by checksum).

    opened by ch-schiffer 0
  • Not abel to load file paths with symlinks

    Not abel to load file paths with symlinks

    Pytiff gives an OSError: file not found when opening a new image with pytiff.Tiff('filename.tif') in case that the filename path is a symlink. I could overcome this problem by doing from os import readlink; pytiff.Tiff(readlink('filename.tif')); however this is not the expected behaviour.

    opened by dickscheid 1
  • Fatal Python error: deallocating NotImplemented

    Fatal Python error: deallocating NotImplemented

    I am trying to read a large Aperio Imagescope image (BigTIFF format). I can read the image without issues and it is very fast. However, after I am finished, I am running into an error that

     Fatal Python error: deallocating NotImplemented
    

    Has this issue ever come up before? Any way to avoid it? Thanks

    opened by hhoeflin 1
  • Non-ascii data in string tags causes a UnicodeDecodeError on Python 3

    Non-ascii data in string tags causes a UnicodeDecodeError on Python 3

    Although TIFF string tags are only supposed to contain 7-bit ASCII characters, many tools write values in UTF-8 or other encodings that aren't 7-bit clean. OME-TIFF/BioFormats is one such tool, where the XML stored in the ImageDescription tag is explicitly encoded as UTF-8. On Python 3, pytiff.Tiff._read_ascii raises a UnicodeDecodeError upon reading such values. On Python 2, where the treatment of string encoding/decoding isn't as rigorous, the problem is effectively ignored.

    Would you accept a patch to fix this? I think the simplest approach is to always decode strings as UTF-8, perhaps only under Python 3. This actually mirrors the way _set_tag already performs UTF-8 string encoding, only under Python 3. I would also be willing to implement a more flexible approach with a user-controlled encoding if you think that's a better option.

    opened by jmuhlich 4
  • pytiff fails to install on Windows

    pytiff fails to install on Windows

    This issue is related to the one entitled "Missing dependency: Cython", which was closed. I'm very happy to hear about pytiff! It is exactly what I have been looking for, already for some time. At first I installed pytiff as part of my Python 3.6.5 instance (32-bits). When I tried to run it, I got the error "Cython modules not available". Because I read that this problem had been fixed for Python 3.7, I decided to do a fresh install of Python 3.7.4. After installing, I even upgraded pip. Unfortunately, I have to conclude that the issue has not yet been resolved fully. I'm attaching the error messages. I hope this can be resolved, so that I can use pytiff without hesitations as one of the required packages in a package I'm developing myself. pytiff_issue.txt

    opened by sbhoek 13
  • loading 3D .tiff files

    loading 3D .tiff files

    I have image stacks which I am trying to load into pytiff. However, it looks like it only loads the first slice:

        with pytiff.Tiff(input_fpath) as handle:
            img_array = handle
            print(img_array.shape)  # (15, 15) instead of (15,15,15)
    
    opened by crypdick 1
Releases(0.8.0)
  • 0.8.0(Jul 31, 2018)

    Updated Tags

    • Tags now use enums instead of simple strings (pytiff.tags).
    • Fields with a scalar do now return a scalar instead of a numpy array of length 1.

    Page Handling

    • you can no longer iterate over a Tiff object.
    • instead a Tiff object has a tif.pages attribute, that returns a list with each page.
    • the manual way via calling tif.set_page(1) is still valid.
    Source code(tar.gz)
    Source code(zip)
Owner
Big Data Analytics group
Big Data Analytics group at the Institute of Neuroscience and Medicine (INM-1), Forschungszentrum Juelich
Big Data Analytics group
Python interface for reading and appending tar files

Python interface for reading and appending tar files, while keeping a fast index for finding and reading files in the archive. This interface has been

Lawrence Livermore National Laboratory 1 Nov 12, 2021
gitfs is a FUSE file system that fully integrates with git - Version controlled file system

gitfs is a FUSE file system that fully integrates with git. You can mount a remote repository's branch locally, and any subsequent changes made to the files will be automatically committed to the remote.

Presslabs 2.3k Jan 8, 2023
A python script to convert an ucompressed Gnucash XML file to a text file for Ledger and hledger.

README 1 gnucash2ledger gnucash2ledger is a Python script based on the Github Gist by nonducor (nonducor/gcash2ledger.py). This Python script will tak

Thomas Freeman 0 Jan 28, 2022
This is a file deletion program that asks you for an extension of a file (.mp3, .pdf, .docx, etc.) to delete all of the files in a dir that have that extension.

FileBulk This is a file deletion program that asks you for an extension of a file (.mp3, .pdf, .docx, etc.) to delete all of the files in a dir that h

Enoc Mena 1 Jun 26, 2022
Python package to read and display segregated file names present in a directory based on type of the file

tpyfilestructure Python package to read and display segregated file names present in a directory based on type of the file. Installation You can insta

Tharun Kumar T 2 Nov 28, 2021
Search for files under the specified directory. Extract the file name and file path and import them as data.

Search for files under the specified directory. Extract the file name and file path and import them as data. Based on that, search for the file, select it and open it.

G-jon FujiYama 2 Jan 10, 2022
Small-File-Explorer - I coded a small file explorer with several options

Petit explorateur de fichier / Small file explorer Pour la première option (création de répertoire) / For the first option (creation of a directory) e

Xerox 1 Jan 3, 2022
Pti-file-format - Reverse engineering the Polyend Tracker instrument file format

pti-file-format Reverse engineering the Polyend Tracker instrument file format.

Jaap Roes 14 Dec 30, 2022
Generates a clean .txt file of contents of a 3 lined csv file

Generates a clean .txt file of contents of a 3 lined csv file. File contents is the .gml file of some function which stores the contents of the csv as a map.

Alex Eckardt 1 Jan 9, 2022
PaddingZip - a tool that you can craft a zip file that contains the padding characters between the file content.

PaddingZip - a tool that you can craft a zip file that contains the padding characters between the file content.

phithon 53 Nov 7, 2022
Extract longest transcript or longest CDS transcript from GTF annotation file or gencode transcripts fasta file.

Extract longest transcript or longest CDS transcript from GTF annotation file or gencode transcripts fasta file.

laojunjun 13 Nov 23, 2022
File-manager - A basic file manager, written in Python

File Manager A basic file manager, written in Python. Installation Install Pytho

Samuel Ko 1 Feb 5, 2022
Two scripts help you to convert csv file to md file by template

Two scripts help you to convert csv file to md file by template. One help you generate multiple md files with different filenames from the first colume of csv file. Another can generate one md file with several blocks.

null 2 Oct 15, 2022
A simple Python code that takes input from a csv file and makes it into a vcf file.

Contacts-Maker A simple Python code that takes input from a csv file and makes it into a vcf file. Imagine a college or a large community where each y

null 1 Feb 13, 2022
Simple, convenient and cross-platform file date changing library. 📝📅

Simple, convenient and cross-platform file date changing library.

kubinka0505 15 Dec 18, 2022
Powerful Python library for atomic file writes.

Powerful Python library for atomic file writes.

Markus Unterwaditzer 313 Oct 19, 2022
Object-oriented file system path manipulation

path (aka path pie, formerly path.py) implements path objects as first-class entities, allowing common operations on files to be invoked on those path

Jason R. Coombs 1k Dec 28, 2022
An object-oriented approach to Python file/directory operations.

Unipath An object-oriented approach to file/directory operations Version: 1.1 Home page: https://github.com/mikeorr/Unipath Docs: https://github.com/m

Mike Orr 506 Dec 29, 2022
File support for asyncio

aiofiles: file support for asyncio aiofiles is an Apache2 licensed library, written in Python, for handling local disk files in asyncio applications.

Tin Tvrtković 2.1k Jan 1, 2023