Skip to content

[Request] Return autocrop() coordinates (cropped area X, Y, W and H) #801

Open

Description

@tgrajewski

Is your feature request related to a problem? Please describe.
I'm generating texture atlasses from other smaller images. I would like to crop the smaller images from surrounding transparency, which .autocrop() does fine, but I would need the X and Y coordinates of the cropped image relative to its original size.

Describe the solution you'd like
Since .autocrop() returns image instance, it can't return the coordinates, so maybe cropped instances could have .cropX, .cropY, .cropWidth, .cropHeight properties? Anything that would give me the crop X and Y coords will be fine (without scanning the images again).

Alternatively, there could be a new method, maybe named boundingBox() or something. This new method would return crop coordinates and size without cropping the image. That method would need to do the same calculations and accept the same arguments as .autocrop(). The result of the new .boundingBox() call could be passed to the .crop(x, y, w, h) method to do actual cropping.

Describe alternatives you've considered
I don't see a way with current API to get the crop coords.

Activity

  1. changed the title [-]autocrop() should return somehow crop coordinates[/-] [+]autocrop() could return somehow crop coordinates[/+] on Oct 3, 2019
  2. changed the title [-]autocrop() could return somehow crop coordinates[/-] [+]autocrop() could return somehow cropped area coordinates[/+] on Oct 3, 2019
  3. changed the title [-]autocrop() could return somehow cropped area coordinates[/-] [+][Request] Return autocrop() coordinates (cropped area X, Y, W and H)[/+] on Oct 3, 2019
  4. modjke commented on Dec 18, 2019

    @modjke

    You can do that with a little hack, but i agree that this feature is usefull

    const jimp = require('jimp')
    
    jimp.prototype.__crop = jimp.prototype.crop
    jimp.prototype.crop = function (x, y, w, h, cb) {  
      this.cropArea = { x, y, w, h }
      return this.__crop(x, y, w, h, cb)
    }
    
    jimp.read('image.png', (error, image) => {  
      image.autocrop(0, (error, image) => {    
        image.cropArea // cropArea = { x, y, w, h }
      })
    })
    
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions