2016-06-02 2 views
0

フォルダ内のすべての画像をループし、ネガを作成して新しい類似の名前で保存するには、次のコードを記述しました。Pythonを使用して画像を逆変換して翻訳する

同じことを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に変換する機能で検索が、1つを見つけることができませんでした。他の方法はありますか?

答えて

2

次の方法をとることができます。あなたは次のように反転画像は、変更をシフトしたい場合は

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) 

:これは、5つのピクセルより大きく、5つのピクセルによって相殺新しいイメージにあなたの元の画像を貼り付け、新しいイメージを作成します

shifted_image.paste(inverted_image, (shift, 0)) 
+0

私は '入れますshift = 5'で実行したがエラー: 'NameError:name 'img'が定義されていない ' – RaviTej310

+0

'イメージ ' –

関連する問題