我编写了以下代码来循环浏览文件夹中的所有图像,创建其负片并将其保存在新的类似名称下。
我怎样才能将它们翻译为右边5个像素?
代码:
from PIL import Image
import PIL.ImageOps
import glob
files = glob.glob('path/*.JPG') # Use *.* if you're sure all are images
for f in files:
print(1)
image = Image.open(f)
inverted_image = PIL.ImageOps.invert(image)
out = f[:f.rfind('.')]
inverted_image.save('%s-n.JPG'%out)
我在ImageOps中搜索了一个翻译函数但找不到。还有其他办法吗?
答案 0 :(得分:2)
您可以采取以下方法。这会创建一个大5像素的新图像,并将原始图像粘贴到新图像偏移5像素:
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)
如果您想要反转图像,请更改如下:
shifted_image.paste(inverted_image, (shift, 0))