1

I am trying to make an image mosaic generator using pyvips. So basically, given an image (called original in the following) create a new, bigger, image that resembles the original one except each pixel (or more realistically groups of pixels) are replaced by smaller distinct image tiles.
I was drawn to pyvips because it is said it can handle huge images and that it can process images without having to load them completely into memory. However, I am having an issue creating a blank mosaic to then populate with tile images.
In the code below I try joining tiles together row by row to create a mosaic but this code unfortunately eats through my RAM and always segfaults.

import os
import pyvips
from os.path import join
from scipy.spatial import cKDTree

class Mosaic(object):

    def __init__(self, dir_path, original_path, tree=None, averages=None):
        self.dir_path = dir_path
        self.original = original_path
        self.tree = tree
        if averages:
            self.averages = averages
        else:
            self.averages = {}

    def get_image(self, path):
        return pyvips.Image.new_from_file(path, access="sequential")

    def build_tree(self):
        for root, dirs, files in os.walk(self.dir_path):
            print('Loading images from', root, '...')
            for file_name in files:
                path = join(root, file_name)
                try:
                    image = pyvips.Image.new_from_file(path)
                    self.averages[self.avg_rgb(image)] = path
                except pyvips.error.Error:
                    print('File', path, 'not recognized as an image.')
        self.tree = cKDTree(self.averages.keys())
        print('Loaded', len(self.averages), 'images.')

    def avg_rgb(self, image):
        m = image.stats()
        return tuple(m(4,i)[0] for i in range(1,4))

    def get_tile_name(self, patch):
        avg = self.avg_rgb(patch)
        index = self.tree.query(avg)[1]
        return self.averages[tuple(self.tree.data[index])]

    def get_tile(self, x, y, step):
        patch = self.get_image(self.original).crop(x, y, step, step)
        patch_name = self.get_tile_name(patch)
        return pyvips.Image.new_from_file(patch_name, access="sequential")

    def make_mosaic(self, tile_num, tile_size, mosaic_path):
        original = self.get_image(self.original)
        mosaic = None
        step = min(original.height, original.width) / tile_num
        for y in range(0, original.height, step):
            mosaic_row = None
            print('Building row', y/step, '/', original.height/step)
            for x in range(0, original.width, step):
                tile = self.get_tile(x, y, step)
                tile = tile.resize(float(tile_size) / float(min(tile.width, tile.height)))
                tile = tile.crop(0, 0, tile_size, tile_size)
                #mosaic.draw_image(tile, x, y)
                mosaic_row = tile if not mosaic_row else mosaic_row.join(tile, "horizontal")
            mosaic = mosaic_row if not mosaic else mosaic.join(mosaic_row, "vertical")
        mosaic.write_to_file(mosaic_path)

I have also tried creating a mosaic by resizing the original image and then using draw_image like the following but this also crashes.

mosaic = self.get_image(self.original).resize(tile_size)

mosaic.draw_image(tile, x, y)

Finally, I have tried creating the mosaic from new_temp_file and I am having trouble writing to the temp image.

How can I make this mosaic program work?

Nakilon
  • 34,866
  • 14
  • 107
  • 142
Max Smith
  • 925
  • 1
  • 14
  • 25
  • libvips uses a recursive algorithm to work out which pixels to compute next. This means if your image pipeline gets very long, it can overflow the C stack and cause a crash. Have a look at `arrayjoin`: this is a libvips operation which can join huge numbers of images together in a single step. I've joined over 10,000 at once without problems. http://jcupitt.github.io/libvips/API/current/libvips-conversion.html#vips-arrayjoin – jcupitt Dec 18 '17 at 09:00
  • This is a good idea, however, I cannot get it working like in the docs. When I call arrayjoin on an image it's not accepting any arguments and seems to separate each band making a grayscale image of each band. i.e: I end up with 3 B&W images that are joined together..... – Max Smith Dec 21 '17 at 02:21
  • 1
    I'll try to make you a sample program in the next few days. – jcupitt Dec 24 '17 at 00:31
  • Thanks! my goal is to make a huge mosaic for a Christmas present! Happy Holidays – Max Smith Dec 24 '17 at 03:11
  • Oh dear :( my answer is probably too late. Happy hols anyway. – jcupitt Dec 25 '17 at 09:50
  • I fixed up my answer a bit, it should make huuuge mosaics quickly now. – jcupitt Dec 25 '17 at 20:31

1 Answers1

3

libvips uses a recursive algorithm to work out which pixels to compute next, so for very long pipelines you can overflow the C stack and get a crash.

The simplest solution would be to use arrayjoin. This is a libvips operator which can join many images in a single call:

http://jcupitt.github.io/libvips/API/current/libvips-conversion.html#vips-arrayjoin

There's an example on the libvips github of using it to join 30,000 images at once:

https://github.com/jcupitt/libvips/issues/471

(though that's using the previous version of the libvips Python binding)

I adapted your program to use arrayjoin, and changed the way it loaded images. I noticed you were also reloading the original image for each output tile, so removing that gave a nice speedup.

#!/usr/bin/python2

from __future__ import print_function
import os
import sys
import pyvips
from os.path import join
from scipy.spatial import cKDTree

class Mosaic(object):

    def __init__(self, dir_path, original_path, tile_size=128, tree=None, averages=None):
        self.dir_path = dir_path
        self.original_path = original_path
        self.tile_size = tile_size
        self.tree = tree
        if averages:
            self.averages = averages
        else:
            self.averages = {}

    def avg_rgb(self, image):
        m = image.stats()
        return tuple(m(4,i)[0] for i in range(1,4))

    def build_tree(self):
        for root, dirs, files in os.walk(self.dir_path):
            print('Loading images from', root, '...')
            for file_name in files:
                path = join(root, file_name)
                try:
                    # load image as a square image of size tile_size X tile_size
                    tile = pyvips.Image.thumbnail(path, self.tile_size,
                                                  height=self.tile_size,
                                                  crop='centre')
                    # render into memory
                    tile = tile.copy_memory()
                    self.averages[self.avg_rgb(tile)] = tile
                except pyvips.error.Error:
                    print('File', path, 'not recognized as an image.')
        self.tree = cKDTree(self.averages.keys())
        print('Loaded', len(self.averages), 'images.')

    def fetch_tree(self, patch):
        avg = self.avg_rgb(patch)
        index = self.tree.query(avg)[1]

        return self.averages[tuple(self.tree.data[index])]

    def make_mosaic(self, tile_num, mosaic_path):
        mosaic = None
        original = pyvips.Image.new_from_file(self.original_path)
        step = min(original.height, original.width) / tile_num
        tiles_across = original.width / step
        tiles_down = original.height / step
        tiles = []
        for y in range(0, tiles_down):
            print('Building row', y, '/', tiles_down)
            for x in range(0, tiles_across):
                patch = original.crop(x * step, y * step, 
                                      min(step, original.width - x * step), 
                                      min(step, original.height - y * step)) 
                tile = self.fetch_tree(patch) 
                tiles.append(tile)

        mosaic = pyvips.Image.arrayjoin(tiles, across=tiles_across)

        print('writing ', mosaic_path)
        mosaic.write_to_file(mosaic_path)

mosaic = Mosaic(sys.argv[1], sys.argv[2])
mosaic.build_tree()
mosaic.make_mosaic(200, sys.argv[3])

I can run it like this:

$ time ./mosaic2.py samples/ k2.jpg x.png
Loading images from samples/ ...
Loaded 228 images.
Building row 0 / 292
...
Building row 291 / 292
writing  x.png
real    7m19.333s
user    7m27.322s
sys     0m30.578s

making a 26496 x 37376 pixel image, in this case, and it runs in about 150mb of memory.

jcupitt
  • 10,213
  • 2
  • 23
  • 39
  • 1
    Merry Christmas, John! – Mark Setchell Dec 25 '17 at 13:15
  • This looks awesome! thank you so much. One quick question, for some reason I am getting a VipsOperation: class "thumbnail" not found when I try running the program even though everything is properly installed and updated. In fact I even get a function object when I call pyvips.Image.thumbnail but it fails when I try calling it. Any ideas/thing you could point me to in order to fix this? Thanks for your help, I really appreciate it. Merry Christmass! – Max Smith Dec 26 '17 at 01:59
  • 1
    I guess you have an old version of libvips installed -- `thumbnail` was added in 8.5. You can use `new_from_file` and `resize` instead, though it'll be a bit slower. Use `sequential` when you open the image for resizing. – jcupitt Dec 26 '17 at 10:12