简体   繁体   中英

raise ValueError('image width cannot be zero') ValueError: image width cannot be zero

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

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM