pearu/pylibtiff

python 3 compatibility

Evenfire opened this issue · 5 comments

Hi!

My issue
While trying to open and read an image, python raises the following error: NameError: name 'xrange' is not defined (see below for a more complete traceback). The issue is very clear. There is a version problem as xrange is now the default range builtin function under the name "range" in python 3.

My libtiff install
I built a dedicated docker image (see dockerfile below) and tried installing "libtiff" in 3 different ways.

  • with pip install -r requirements.txt (requirements.txt only contains libtiff==0.4.2 as numpy is installed before) [in the dockerfile - it uses pip3]
  • by cloning this repo and running setup.py install [in a docker container running the mentioned image where libtiff was not install at build time]
  • with pip3 install libtiff [in a docker container running the mentioned image where libtiff was not install at build time]

In all 3 cases the install is successful but the same error is raised.

My conclusion
There is no complete support for python3 in this project.
Am I wrong ? Did i miss something ?
I have been looking around to find evidence of a mistake on my part but did not find any (I might be a bad google user...).
I am willing to look into this issue further and make a PR to fix it if needed!

Complete traceback

Traceback (most recent call last):
  File "../tests/bug_libtiff/bug_libtiff.py", line 5, in <module>
    img = TIFF.open(file, mode='r').read_image()
  File "/usr/lib/python3.6/site-packages/libtiff/libtiff_ctypes.py", line 530, in read_image
    return self.read_tiles(typ)
  File "/usr/lib/python3.6/site-packages/libtiff/libtiff_ctypes.py", line 1028, in read_tiles
    read_plane(full_image, tmp_tile)
  File "/usr/lib/python3.6/site-packages/libtiff/libtiff_ctypes.py", line 996, in read_plane
    for y in xrange(0, num_irows, num_trows):
NameError: name 'xrange' is not defined

Code

from libtiff import TIFF
file = '/home/me/problem.tif'
img = TIFF.open(file, mode='r').read_image()

Command
(while running the mentioned image with libtiff install using 1 of the 3 mentioned procedures)

python3 bug_libtiff.py

Dockerfile

FROM ubuntu:16.04

########################################
#       Dockerfile parameters 
########################################

# Set default values
ARG PYTHON_VERSION=3.6.2

########################################
#             Image labels
########################################

LABEL author="pkirsch"


########################################
#        Container preparation
########################################
RUN apt-get update
RUN apt-get install -y build-essential curl


########################################
#               Python
########################################

# Requirements
RUN apt-get install -y \
    libreadline-gplv2-dev \
    libncursesw5-dev \
    libssl-dev \
    libsqlite3-dev \
    tk-dev \
    libgdbm-dev \
    libc6-dev \
    libbz2-dev \
    graphviz

# Fetch
WORKDIR /tmp
RUN curl -L https://www.python.org/ftp/python/${PYTHON_VERSION}/Python-${PYTHON_VERSION}.tar.xz | tar xfJ -

# Configure
WORKDIR /tmp/Python-${PYTHON_VERSION}
RUN \
  ./configure \
    --prefix=/usr \
    --with-system-ffi \
    --enable-shared

# Make in parallel with 2x the number of processors
RUN make -j $(( 2 * $(cat /proc/cpuinfo | egrep ^processor | wc -l) )) && make install

# Make a `python` alias for `python3`
RUN \
  ln -s /usr/bin/python3 /usr/bin/python && \
  ln -s /usr/bin/pip3 /usr/bin/pip

########################################
#     Python packages installation    
########################################

# Install numpy
RUN pip install --upgrade pip && pip install numpy==1.15.4

# Install python lib in a requirements-cpu.txt
WORKDIR /tmp
COPY ./requirements-cpu.txt /tmp/requirements-cpu.txt
RUN pip install --upgrade pip && pip install -r requirements-cpu.txt

########################################
#       Ubuntu install Cleaning  
########################################

RUN rm -r /tmp/Python-${PYTHON_VERSION}
RUN rm /tmp/requirements-cpu.txt
RUN rm -rf /var/lib/apt/lists/*

########################################
#         Add code in the image
########################################
WORKDIR /workspace

########################################
#              Entrypoint 
########################################
ENTRYPOINT ["python3"]
pearu commented

Resolved by PR #92 .

Hello, will you please help me resolve the same problem in google colab?

Hello, will you please help me resolve the same problem in google colab?

NO

Three years later and this is still an issue after running pip install libtiff. Can the package be updated in PyPI?

pearu commented

Three years later and this is still an issue after running pip install libtiff. Can the package be updated in PyPI?

Use:

pip install bitarray numpy
pip install pylibtiff

(must use separate pip commands, also notice the py prefix in pylibtiff but the installed package name will still be libtiff)