libvips / pyvips

python binding for libvips using cffi
MIT License
648 stars 49 forks source link

Problems with Pyramid generation #99

Open jpadfield opened 5 years ago

jpadfield commented 5 years ago

Using pyvips to generate Pyramidal Tiffs seems to corrupt the right hand side for some image types.

jpadfield commented 5 years ago

I am updating an old python/vips based pyramidal tiff generator from the olf python bindings to pyvips. The code runs well but is outputting a partially corrupted image for some image types:

My test is with a smallish image: Bands: 1 Coding: none Format: ushort Type: grey16 Width: 2400 Height: 2400

A pyramid is generated with out error but the right hand side of the image comes out distorted. I am not sure if I am doing something wrong or if this is a bug.

Thanks

Screenshot from 2019-05-17 14-17-37

#!/usr/bin/python

import sys, os
reload(sys)  
sys.setdefaultencoding('utf8')

import pyvips

def PYR_simple(pathIn, pathOut):
    if not os.path.isfile(pathOut):
        if os.path.isfile(pathIn):          
            try:
                im = pyvips.Image.magickload(pathIn)
                imBands = im.bands
                imBFmt = im.format
                imCoding = im.coding
                imType = im.interpretation
                imx = im.width
                imy = im.height
                fields = im.get_fields()
                print "Bands: "+str(imBands)
                print "Coding: "+str(imCoding)
                print "Format: "+str(imBFmt)
                print "Type: "+str(imType)
                print "Width: "+str(imx)
                print "Height: "+str(imy)

                im.write_to_file(pathOut+'[tile=True,pyramid=True]')                
            except pyvips.Error, e:
                print "With VIPS functions: " + pathIn
                print str(e)

fullPath = sys.argv[1] 

if os.path.isfile(fullPath):
    fp = os.path.split(fullPath) 
    f = os.path.splitext(fp[1]) 

    outPath = "."
    pathOut = outPath+"/"+f[0] + "-PYR.tif"

    PYR_simple (fp[1], pathOut)
else:
    print "Bad File: "+fullPath
jcupitt commented 5 years ago

Hi Joe, do you have a sample input image I could test with? Are these PSBs?

I tried with a 2400 x 2400 grey16 TIFF and it seemed to work OK with libvips 8.8. I'll try with 8.7.

jpadfield commented 5 years ago

Here is a smaller crop that seems to have the same problem Thanks

c0600.tif.tar.gz

jcupitt commented 5 years ago

I see the problem here too -- it looks like a bug in imagemagick with 16-bit tiled TIFF images.

If I change this line:

                im = pyvips.Image.magickload(pathIn)

To be:

                                im = pyvips.Image.new_from_file(pathIn, access="sequential")

It seems to work. It should make your program quite a bit quicker too.

jpadfield commented 5 years ago

Thanks :-) I will give it a go

jcupitt commented 5 years ago

I would change the middle bit to this, fwiw:

            im = pyvips.Image.new_from_file(pathIn, access="sequential")
            print("Bands: {}".format(im.bands))
            print("Coding: {}".format(im.coding))
            print("Format: {}".format(im.format))
            print("Type: {}".format(im.interpretation))
            print("Width: {}".format(im.width))
            print("Height: {}".format(im.height))
            im.write_to_file(pathOut, tile=True, pyramid=True)

And switch to py3, if you can.