Navdeep Rana
Navdeep Rana

Reputation: 121

splicing Image with wand, python doesn't work

So I want to splice an image into two, for which I wrote this code.

from wand.image import Image
from wand.display import display
with Image(filename="test.png") as im :
  im.trim(color=None,fuzz=0)
  x,y = im.size
  xh,yh = int(x/2),int(y/2)
  print(x,y,xh,yh)
  im1 = im[0:xh,0:y]
  print(im1.size)
  display(im1)

The size of image is (1156,242) and hence the spliced image should be (578,242), but it's (553,235) instead. Anyone knows why? Here is the test image.

Upvotes: 0

Views: 164

Answers (1)

emcconville
emcconville

Reputation: 24439

This is the effect of using wand.image.Image.trim. A simple "repage" is all that's needed.

from wand.image import Image

with Image(filename="test.png") as im :
  im.trim(color=None,fuzz=0)
  im.reset_coords()  # <= Same as `-repage'
  x,y = im.size
  xh,yh = int(x/2),int(y/2)
  print(x,y,xh,yh)
  im1 = im[0:xh,0:y]
  print(im1.size)
  display(im1)

I don't have the doc links handy at the moment, but a quick search for ImageMagick's -repage command line option should help describe the process.

Upvotes: 1

Related Questions