Skip to content

This issue was moved to a discussion.

You can continue the conversation there. Go to discussion →

Letterboxing Images and Iterating VipsImage pixel data #2260

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

Closed
Transigent opened this issue May 23, 2021 · 1 comment
Closed

Letterboxing Images and Iterating VipsImage pixel data #2260

Transigent opened this issue May 23, 2021 · 1 comment

Comments

@Transigent
Copy link

Transigent commented May 23, 2021

Hi and sorry about the noobness of this question, I am fairly new to C and VIPS.

I am successfully using libvips in a VS2017 C++ project to load thumbnails from files on disk, using the C API. Its so FAST compared to the existing opencv implementation! Thanks for the great library.

My questions are several:

1) Existing code requires 640 x 640 thumbnails, I am currently using code like this:
vips_thumbnail(FileName, &out, 640)
which gets me nearly what I need except that only the longest axis is 640, I need to supply the image data letterboxed (or pillarboxed) ie. so that it fills the whole 640 x 640 but centered and maintaining aspect ratio. I saw a weserv enhancement discussing this weserv/images#80 (comment) but I couldn't find if this has been implemented in libvips or in the thumbnailer. If not is there another fast way?

2) Theres probably an obvious answer but I could not find how to directly access the image data in the VipsImage object. The documentation description of VipsImage states that "VIPS images are three-dimensional arrays, the dimensions being width, height and bands" but I cant find examples that show how to iterate through the data, the closest I could find is a region example here which I dont know how to transmogrify so it works with an VipsImage. Is there an example of this somewhere?

3) I want to use libvips to do what an existing block of opencv code does using a cv::Mat. It iterates through the (BGR) pixel data to build a new array organized like this :

  • All R pixel data, followed by all G pixel data, then B pixel data.
  • Pixel values are changed (from uchar) to floats with values from 0 to 1
    (It is formatted like this for feeding to a neural network)

At first I thought I would do the transformation manually like the original code but I have seen some really powerful code snippets like this and it occurs that libvips might already have the tools to reformat the data the way I need it. Is there any inbuilt functionality to do that?

Thanks for any advice!

@jcupitt
Copy link
Member

jcupitt commented May 23, 2021

Hello @Transigent,

libvips has very efficient operation compositing -- you can run one operation after the other, and behind the scenes they join together into a fast and low-memory combined operation. So the idea behind the design of the API is that operations only implement the minimum functionality, and you get complex effects by combining primitives.

thumbnail shrinks to a bounding box. Use gravity to extend an image within a new canvas, so you need (in python, for brevity):

thumb = pyvips.Image.thumbnail(filename, 640)
boxed = thumb.gravity("centre", 640, 640, extend="white")

You can't really iterate over pixels in libvips, since images don't really exist. Everything is a delayed computation and pixels only exist on demand. You can either implement a new vips operation, or render the whole image to an area of memory and then treat it like any other array. Have a look at vips_image_write_to_memory().

You can split your image up like this (in Python, again):

# rescale to float 0 - 1
image = image / 255.0
# split bands to an array of images
bands = image.bandsplit()
# join those images vertically
image = pyvips.Image.arrayjoin(bands, across=1)

Then write to memory to make a huge float array.

You'll find python is as fast as C for libvips, so it might be more convenient.

@jcupitt jcupitt closed this as completed May 23, 2021
@libvips libvips locked and limited conversation to collaborators May 23, 2021

This issue was moved to a discussion.

You can continue the conversation there. Go to discussion →

Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants