Skip to content
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() gets wrong coordinates after trim() #472

Closed
erozen opened this issue Mar 31, 2020 · 4 comments
Closed

crop() gets wrong coordinates after trim() #472

erozen opened this issue Mar 31, 2020 · 4 comments

Comments

@erozen
Copy link

@erozen erozen commented Mar 31, 2020

After a trim() call is made, crop() results in incorrect sized images when using left or top coordinates close to the corner.

Repro:
Create a blank image, put some text on it, then cut it up in to quarters - correct functionality.
Create a blank image, put some text on it, trim it, then cut it up in to quarters - incorrect functionality.

Repro code:

#!/usr/bin/python3

from wand.drawing import Drawing
from wand.image import Image

draw=Drawing()
img=Image(width = 500, height = 500)

draw.font = '/usr/share/fonts/opentype/freefont/FreeSansBold.otf'
draw.font_size = 675
draw.text( 0, img.height, "A")

draw(img)
print("Original full size:    " + str( img[:, :] ))
print("Original top left:     " + str( img[:int( img.width / 2 ), :int( img.height / 2 ) ] ))
print("Original bottom right: " + str( img[ int( img.width / 2 ):, int( img.height / 2 ):] ))

img.trim()
print("Trimmed full size:     " + str( img[:, :] ))
print("Trimmed top left:      " + str( img[:int( img.width / 2 ), :int( img.height / 2 ) ] ))
print("Trimmed bottom right:  " + str( img[ int( img.width / 2 ):, int( img.height / 2 ):] ))

# The 'A' trims down to 458x492 - a quarter should be 229x246
print("Trimmed hard-coded top left:     " + str( img[  0:229,   0:246] ))
print("Trimmed hard-coded bottom right: " + str( img[229:458, 246:492] ))

Output:

Original full size:    <wand.image.Image: 0a595b4 '' (500x500)>
Original top left:     <wand.image.Image: 48f355c '' (250x250)>
Original bottom right: <wand.image.Image: b54cc59 '' (250x250)>
Trimmed full size:     <wand.image.Image: c185e46 '' (458x492)>
Trimmed top left:      <wand.image.Image: 7177447 '' (222x237)>
Trimmed bottom right:  <wand.image.Image: 9629632 '' (229x246)>
Trimmed hard-coded top left:     <wand.image.Image: 7177447 '' (222x237)>
Trimmed hard-coded bottom right: <wand.image.Image: 9629632 '' (229x246)>
@erozen
Copy link
Author

@erozen erozen commented Apr 1, 2020

Never got to the bottom of this, but found a workaround: adding a rotate(360) after the trim() seems to reset whatever needs to be reset.

@emcconville
Copy link
Owner

@emcconville emcconville commented Apr 2, 2020

The method Image.trim() does not reset coordinates automatically. This is expected behavior, as ImageMagick is respecting paging. Simply call Image.reset_coords() after Image.trim()

with Image(width = 500, height = 500) as img:
  with Drawing() as draw:
    # ... build drawing context ...
   draw(img)
   img.trim()
   img.reset_coords()

adding a rotate(360) after the trim() seems to reset whatever needs to be reset

Correct. Because Image.rotate() method has a reset_coords=True kwarg. It may be possible to add such an argument to Image.trim(), but that would default to False, as (in my opinion) this method is used often by GIF animators, and resetting coordinates would be destructive.

@erozen
Copy link
Author

@erozen erozen commented Apr 3, 2020

Great thanks. I messed around with reset_coords a bit, but only on the crop - it clearly needed to happen beforehand. Would be a useful addition to the trim statement, or even a note in the doc saying it's a necessary second step. I'll look at the code and see if i can raise create a pull request.

@emcconville emcconville added this to the Wand 0.6.0 milestone Apr 16, 2020
@emcconville
Copy link
Owner

@emcconville emcconville commented Apr 16, 2020

Closing this as the reset_coords=True argument should work just fine

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

No branches or pull requests

2 participants