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

Crop by bigger frame wraps offset when geometry is set #367

Closed
CthulhuDen opened this Issue Mar 26, 2018 · 5 comments

Comments

2 participants
@CthulhuDen
Copy link

CthulhuDen commented Mar 26, 2018

I guess this code is supposed to handle negative left/top parameters as offset from right/bottom border of the image, however it interferes with that code few lines above which automatically computes left/top from gravity. So when say requested width is bigger then source image width, left first becomes negative, then wraps around and only part of original image remains after crop.

I suggest you add max(0, ...) construct to code which computes left/right to resolve this.

@emcconville

This comment has been minimized.

Copy link
Owner

emcconville commented Dec 19, 2018

Can you provide a test case? The negative left/top is not to offset from right/bottom, but transitions from authentic to virtual pixels. The C-API supports this by making width & height unsigned, but x & y signed.

MagickBooleanType MagickCropImage(MagickWand *wand,
  const size_t width,const size_t height,const ssize_t x,const ssize_t y)
@emcconville

This comment has been minimized.

Copy link
Owner

emcconville commented Dec 20, 2018

Ah. I think I understand what the issue is. The abs_ local function doing a sanity check on x/y offsets; however, it might not be needed when pre-calculating values based on gravity. So a fix may be as simple as...

diff --git a/wand/image.py b/wand/image.py
index f88bc4e..08c90a7 100644
--- a/wand/image.py
+++ b/wand/image.py
@@ -2188,8 +2188,8 @@ class BaseImage(Resource):
             elif n > m:
                 raise ValueError(repr(n) + ' > ' + repr(m))
             return m + n if n < 0 else n
-        left = abs_(left, self.width, 0)
-        top = abs_(top, self.height, 0)
+        left = abs_(left, self.width, 0) if gravity is None else left
+        top = abs_(top, self.height, 0) if gravity is None else top
         if width is None:
             right = abs_(right, self.width)
             width = right - left

Still need to identify some test cases.

@CthulhuDen

This comment has been minimized.

Copy link

CthulhuDen commented Dec 20, 2018

I believe original issue appeared when area to crop by was bigger then the original image. I'll try to reproduce the issue.

@CthulhuDen

This comment has been minimized.

Copy link

CthulhuDen commented Dec 20, 2018

Sorry for not proper test case, but here is repro:

from wand.image import Image

if __name__ == '__main__':
    img = Image(filename="tests/assets/sasha.jpg")
    img.crop(width=300, height=300, gravity="center")
    img.save(filename="sasha-cropped.jpg")

With your suggested fix, it works as intended.

@emcconville

This comment has been minimized.

Copy link
Owner

emcconville commented Dec 20, 2018

Thanks for following up on this! I can integrate the example into a test case, and will have a patch pushed to master shortly.

@emcconville emcconville added this to the 0.5.0 milestone Dec 20, 2018

@emcconville emcconville added the bug label Dec 20, 2018

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment