Search code examples
pythonimagepython-imaging-library

How can I prevent ImageChops.offset from wrapping data?


I'm trying to use imagechops.offset to do basic scrolling of a foreground image. The only problem is, my professor doesn't want the scrolled data to wrap (data 'pushed' off-screen shouldn't come back on the other side).

Is there a way to do this with imagechops, or should I go ahead and use array operations?


Solution

  • Can't be done with ImageChops alone, but you don't need array operations either (not in your code). You can combine ImageChops.offset with paste as in:

    from PIL import Image, ImageChops
    
    x = Image.open('input.png')
    width, height = x.size
    c = ImageChops.offset(x, 10, 20)
    c.paste((255, 255, 255), (0, 0, 10, height))
    c.paste((255, 255, 255), (0, 0, width, 20))
    c.save('output.png')
    

    This example assumes you want to fill the wrapped area with a certain color.