0

when l try to crop an image l get this following error. here is my code. I don't understand the error since my image is in a good shape in png format. what's wrong ?

from __future__ import print_function
    from wand.image import Image
    f = 'image1.png'
    with Image(filename=f) as img:
        print('width =', img.width)
        print('height =', img.height)
        #left,top,right,bottom
        x=img.crop(275, 347, 147, 239)
        x.size
        print(x.size)

l got this error

    x=img.crop(275, 347, 147, 239)
  File "/usr/local/lib/python2.7/dist-packages/wand/image.py", line 543, in wrapped
    result = function(self, *args, **kwargs)
  File "/usr/local/lib/python2.7/dist-packages/wand/image.py", line 1504, in crop
    raise ValueError('image width cannot be zero')
ValueError: image width cannot be zero
vincent
  • 1,558
  • 4
  • 21
  • 34

1 Answers1

1

Image.crop() takes left, top, right, and bottom offsets in the order. As the diagram in the docs shows, right and bottom offsets are from left and top, not right and bottom:

+--------------------------------------------------+
|              ^                         ^         |
|              |                         |         |
|             top                        |         |
|              |                         |         |
|              v                         |         |
| <-- left --> +-------------------+  bottom       |
|              |             ^     |     |         |
|              | <-- width --|---> |     |         |
|              |           height  |     |         |
|              |             |     |     |         |
|              |             v     |     |         |
|              +-------------------+     v         |
| <--------------- right ---------->               |
+--------------------------------------------------+

In your code img.crop(275, 347, 147, 239), 147 is on 275's left and 239 is above 347, and it's why ValueError is raised.

minhee
  • 5,688
  • 5
  • 43
  • 81