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
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
Python virtual filesystem for SQLite to read from and write to S3

Python virtual filesystem for SQLite to read from and write to S3

Department for International Trade 70 Jan 04, 2023
Import Python modules from any file system path

pathimp Import Python modules from any file system path. Installation pip3 install pathimp Usage import pathimp

Danijar Hafner 2 Nov 29, 2021
Organize the files into the relevant sub-folders

This program can be used to organize files in a directory by their file extension. And move duplicate files to a duplicates folder.

Thushara Thiwanka 2 Dec 15, 2021
A wrapper for DVD file structure and ISO files.

vs-parsedvd DVDs were an error. A wrapper for DVD file structure and ISO files. You can find me in the IEW Discord server

7 Nov 17, 2022
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
This python project contains a class FileProcessor which allows one to grab a file and get some meta data and header information from it

This python project contains a class FileProcessor which allows one to grab a file and get some meta data and header information from it. In the current state, it outputs a PrettyTable to txt file as

Joshua Wren 1 Nov 09, 2021
Python code snippets for extracting PDB codes from .fasta files

Python_snippets_for_bioinformatics Python code snippets for extracting PDB codes from .fasta files If you have a single .fasta file for all protein se

Sofi-Mukhtar 3 Feb 09, 2022
Organizer is a python program that organizes your downloads folder

Organizer Organizer is a python program that organizes your downloads folder, it can run as a service and so will start along with the system, and the

Gustavo 2 Oct 18, 2021
Utils for streaming large files (S3, HDFS, gzip, bz2...)

smart_open ā€” utils for streaming large files in Python What? smart_open is a Python 3 library for efficient streaming of very large files from/to stor

RARE Technologies 2.7k Jan 06, 2023
A python script to pull the transactions of an Algorand wallet and put them into a CSV file.

AlgoCSV A python script to pull the transactions of an Algorand wallet and put them into a CSV file. Dependancies: Requests Main features: Groups: Com

21 Jun 25, 2022
Nintendo Game Boy music assembly files parser into musicxml format

GBMusicParser Nintendo Game Boy music assembly files parser into musicxml format This python code will get an file.asm from the disassembly of a Game

1 Dec 11, 2021
Some-tasks - Files for some of the tasks for the group sessions

Files for some of the tasks for the group sessions Here you can find some of the

<a href=[email protected] Computer Networks"> 0 Aug 25, 2022
Automatically generates a TypeQL script for doing entity and relationship insertions from a .csv file, so you don't have to mess with writing TypeQL.

Automatically generates a TypeQL script for doing entity and relationship insertions from a .csv file, so you don't have to mess with writing TypeQL.

3 Feb 09, 2022
Convert All TXT Files To One File.

AllToOne Convert All TXT Files To One File. Hi šŸ‘‹ , I'm Alireza A Python Developer Boy šŸ”­ Iā€™m currently working on my C# projects šŸŒ± Iā€™m currently Lea

4 Jun 07, 2022
A JupyterLab extension that allows opening files and directories with external desktop applications.

A JupyterLab extension that allows opening files and directories with external desktop applications.

martinRenou 0 Oct 14, 2021
Powerful Python library for atomic file writes.

Powerful Python library for atomic file writes.

Markus Unterwaditzer 313 Oct 19, 2022
Media file renamer and organizion tool

mnamer mnamer (media renamer) is an intelligent and highly configurable media organization utility. It parses media filenames for metadata, searches t

Jessy Williams 533 Dec 29, 2022
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 rem

Presslabs 2.3k Jan 08, 2023
Python function to stream unzip all the files in a ZIP archive: without loading the entire ZIP file or any of its files into memory at once

Python function to stream unzip all the files in a ZIP archive: without loading the entire ZIP file or any of its files into memory at once

Department for International Trade 206 Jan 02, 2023