Use Python to invert and translate images

前端 未结 1 378
情话喂你
情话喂你 2021-01-17 02:37

I have written the following code to loop through all the images in a folder, create its negative and save it under a new similar name.

How can I do the same thing

相关标签:
1条回答
  • 2021-01-17 03:15

    You could take the following approach. This creates a new image 5 pixels bigger and pastes your original image into the new image offset by 5 pixels:

    from PIL import Image
    import PIL.ImageOps
    import glob
    
    shift = 5
    files = glob.glob('path/*.JPG') # Use *.* if you're sure all are images
    
    for f in files:
        image = Image.open(f)
        inverted_image = PIL.ImageOps.invert(image)
    
        out = f[:f.rfind('.')]
        inverted_image.save('%s-n.JPG'%out)
    
        # Shift the image 5 pixels
        width, height = image.size
        shifted_image = Image.new("RGB", (width+shift, height))
        shifted_image.paste(image, (shift, 0))
        shifted_image.save('%s-shifted.JPG' % out)
    

    If you want the inverted images shifted, change as follows:

        shifted_image.paste(inverted_image, (shift, 0))
    
    0 讨论(0)
提交回复
热议问题