libvips / pyvips

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

Image stretching #484

Closed his0car closed 1 month ago

his0car commented 1 month ago

When tiling an image and stitching it back together, the images are distorted (looks like resampling and stretching or something like that). I have not made sure that PIL or something is at fault.

Code for tiling:

image = pyvips.Image.new_from_file(
        path, memory=True
    )  # Load directly into memory instead of tmp disk

    image.dzsave(
        output_dir,
        suffix=f".png",
        overlap=0,
        depth="one",
        # compression=c,
        tile_size=224,
        skip_blanks=False,
        properties=False,
    )

Code for stitching:

image_dim = 224
array = np.zeros((rows*image_dim, cols*image_dim, 3), dtype='uint8')

for r in tqdm(range(rows)):
    for c in range(cols):
        path = tile_path / f'{c}_{r}.png'
        tile = cv2.imread(str(path), cv2.IMREAD_COLOR)
        tile = cv2.cvtColor(tile, cv2.COLOR_BGR2RGB)

        if tile.shape[0] != image_dim or tile.shape[1] != image_dim:
            continue

        array[r*image_dim:(r+1)*image_dim, c*image_dim:(c+1)*image_dim] = tile

Visual inspection: I overlayed the images as good as it gets on the left side of the image. stretching

I overlay the images and switch from one to another: https://imgur.com/JxHGlUa

jcupitt commented 1 month ago
image = pyvips.Image.new_from_file(
        path, memory=True

I wouldn't use memory for slide images. You'll find it's slow and will use huge amounts of RAM.

You can stitch quickly with arrayjoin, eg.:

tiles = [pyvips.Image.new_from_file(filename, access="sequential") 
         for filename in filenames]
joined = pyvips.Image.arrayjoin(tiles, across=20)