Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

VipsJpeg: Bogus JPEG colorspace -tiff2vips: decompress error tile 0 x 0 #520

Closed
petoor opened this issue Feb 14, 2025 · 5 comments
Closed

Comments

@petoor
Copy link

petoor commented Feb 14, 2025

Hello.

I have a problem regarding loading a tiff image into memory.


img = pyvips.Image.new_from_file(vips_filename = "field.tif", access='sequential')
img.numpy() # Throws   VipsJpeg: Bogus JPEG colorspace -tiff2vips: decompress error tile 0 x 0
# img.colourspace("srgb").numpy() - # also doesnt work

The image is readable by for instance Dask using

from dask.array.image import imread

Could you point me to what im doing wrong?
Versions:
vips: vips-8.16.0 (build from source)
pyvips : 2.2.3
Running Ubuntu 2024.04

The image in question :
Created by open drone map
field_image.zip

@petoor
Copy link
Author

petoor commented Feb 16, 2025

For good measure, I've made a dockerfile where i can reproduce the error.
(Using the packaged libvips version (8.15.2))

Create file pyvips_test.py and copy the field.tif image into same folder.
**pyvips_test.py **

import pyvips
img = pyvips.Image.new_from_file(vips_filename = "field.tif", access='sequential')
img.numpy() # Throws   VipsJpeg: Bogus JPEG colorspace -tiff2vips: decompress error tile 0 x 0
# img.colourspace("srgb").numpy() - # also doesnt work
print(img)

Dockerfile


RUN apt-get update && apt-get upgrade -y && apt-get -y install \
    build-essential ninja-build wget libglib2.0-dev libjpeg-turbo8-dev libtiff5-dev libarchive-dev libvips python3-pip

RUN python3 -m pip install numpy pyvips --break-system-packages

COPY pyvips_test.py pyvips_test.py
COPY field.tif field.tif

ENTRYPOINT ["python3", "pyvips_test.py"]

Building
docker build -t pyvipstest .

Finally running:
docker run pyvipstest

Yields

pyvips.error.Error: unable to write to memory
  VipsJpeg: Bogus JPEG colorspace
tiff2vips: decompress error tile 0 x 0
VipsJpeg: Bogus JPEG colorspace
tiff2vips: decompress error tile 512 x 0
VipsJpeg: Bogus JPEG colorspace
tiff2vips: decompress error tile 1024 x 0

@jcupitt
Copy link
Member

jcupitt commented Feb 16, 2025

Hello @petoor,

I tried with tiffcp:

$ tiffcp field.tif x.tif
TIFFReadDirectory: Warning, Unknown field with tag 33550 (0x830e) encountered.
TIFFReadDirectory: Warning, Unknown field with tag 33922 (0x8482) encountered.
TIFFReadDirectory: Warning, Unknown field with tag 34735 (0x87af) encountered.
TIFFReadDirectory: Warning, Unknown field with tag 34737 (0x87b1) encountered.
TIFFReadDirectory: Warning, Unknown field with tag 42112 (0xa480) encountered.
JPEGLib: Bogus input colorspace.
x.tif: Error, can't write tile at 0 0.

So your image can't be read by libtiff. I suppose dask have their own tiff reader which allows a bigger range of colourspaces. Or maybe this is something specific to GDAL?

I tried tiffinfo as well:

$ tiffinfo field.tif 
TIFFReadDirectory: Warning, Unknown field with tag 33550 (0x830e) encountered.
TIFFReadDirectory: Warning, Unknown field with tag 33922 (0x8482) encountered.
TIFFReadDirectory: Warning, Unknown field with tag 34735 (0x87af) encountered.
TIFFReadDirectory: Warning, Unknown field with tag 34737 (0x87b1) encountered.
TIFFReadDirectory: Warning, Unknown field with tag 42112 (0xa480) encountered.
=== TIFF directory 0 ===
TIFF Directory at offset 0x8 (8)
  Image Width: 5506 Image Length: 6289
  Tile Width: 512 Tile Length: 512
  Bits/Sample: 8
  Sample Format: unsigned integer
  Compression Scheme: JPEG
  Photometric Interpretation: RGB color
  Extra Samples: 1<unassoc-alpha>
  Samples/Pixel: 4
  Planar Configuration: single image plane
  Tag 33550: 0.015538,0.015540,0.000000
  Tag 33922: 0.000000,0.000000,0.000000,562759.412575,6125026.769753,0.000000
  Tag 34735: 1,1,0,7,1024,0,1,1,1025,0,1,1,1026,34737,8,0,2049,34737,8,8,2054,0,1,9102,3072,0,1,32632,3076,0,1,9001
  Tag 34737: unknown|unknown|
  GDAL Metadata: <GDALMetadata>
  <Item name="DESCRIPTION" sample="0" role="description">red</Item>
  <Item name="DESCRIPTION" sample="1" role="description">green</Item>
  <Item name="DESCRIPTION" sample="2" role="description">blue</Item>
</GDALMetadata>

  JPEG Tables: (73 bytes)

It's a four-band image, I suppose RGBA, but with JPEG (RGB and CMYK only) compression. I don't think many TIFF readers will be able to handle this.

@petoor
Copy link
Author

petoor commented Feb 18, 2025

Hi @jcupitt

Thank you very much for pointing me in the right direction.

gdal_translate -co COMPRESS=JPEG -co TILED=YES

will return the bogus error, however, stripping alpha channel will result in a tif readable by pyvips.

gdal_translate -b 1 -b 2 -b 3 -co COMPRESS=JPEG -co TILED=YES
or for even better compression.
gdal_translate -b 1 -b 2 -b 3 -co COMPRESS=JPEG -co PHOTOMETRIC=YCBCR -co TILED=YES

In case anyone is having the same question in the future.

@petoor petoor closed this as completed Feb 18, 2025
@petoor
Copy link
Author

petoor commented Feb 18, 2025

Or straight from the documentation : https://gdal.org/en/stable/programs/gdal_translate.html
gdal_translate rgba.tif withmask.tif -b 1 -b 2 -b 3 -mask 4 -co COMPRESS=JPEG -co PHOTOMETRIC=YCBCR
In case anyone want to keep the alpha channel

@jcupitt
Copy link
Member

jcupitt commented Feb 18, 2025

Ah, that's helpful. Thanks for the update @petoor

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants