user2196626
user2196626

Reputation: 1

RMagick can't crop a cropped image?

I'm working on a ruby project with RMagick where I load an image from a file, initialize objects with cropped parts, and then try to re-crop those sub-images. For the crop-of-a-crop image, it creates an image with 1x1 dimensions.

However, it works in the toy, trivial versions I create:

i = Image.read('sample.png')[0]
=> sample.png PNG 1000x800 1000x800+0+0 DirectClass 8-bit 47kb
si = i.crop(50, 50, 900, 700)
=> sample.png PNG 1000x800=>900x700 1000x800+50+50 DirectClass 8-bit
ssi = i.crop(50, 50, 800, 600)
=> sample.png PNG 1000x800=>800x600 1000x800+100+100 DirectClass 8-bit

etc., etc.

It seems to work indefinitely, at least out 4 generations.

However, my code definitely does not work. I've been unable to construct a trivial version that doesn't work, but this is the simplified relevant portion of my actual code.

https://gist.github.com/mikaylathompson/9ca5db7569d6bfba6008

doc = Document.new('sample.png')
doc.split
doc.sort

# This image is just fine
doc.tables[0].image
=> sample.png PNG 1000x800=>745x150 1000x800+125+350
doc.tables[0].divide_rows

# and this image failed
doc.tables[0].rows[0].image
=> sample.png PNG 1000x800=>1x1 1000x800-1-1 DirectClass 8-bit

# this fails too, so it's based on the image, not the dimensions
doc.tables[0].image.crop(50, 50, 100, 100)
=> sample.png PNG 1000x800=>1x1 1000x800-1-1 DirectClass 8-bit

Upvotes: 0

Views: 250

Answers (1)

Matt
Matt

Reputation: 269

You can tell crop() to not include the metadata dimensions (these seem to interfere with crops of crops) from the source image in the derived image by passing 'true' as the last parameter like so:

doc.tables[0].image.crop(50, 50, 100, 100, true)

Upvotes: 1

Related Questions