画像をトリミングして保存するためのpython PILライブラリを使用してトラブル

StackOverflow https://stackoverflow.com/questions/1076638

  •  21-08-2019
  •  | 
  •  

質問

イムはかなり高解像度の画像をトリミングし、その完了を確認するために結果を保存しようとします。しかし、私は、保存方法をどのように使用するかにかかわらず、次のエラーを得続ける:SystemError: tile cannot extend outside image

from PIL import Image

# size is width/height
img = Image.open('0_388_image1.jpeg')
box = (2407, 804, 71, 796)
area = img.crop(box)

area.save('cropped_0_388_image1', 'jpeg')
output.close()
役に立ちましたか?

解決

ボックスはので、多分あなたは(2407 + 71 804、2407、+ 796 804)のもの(下、右、上、左)とは?

編集:すべての4点の座標を左端、上端、右端と下端に上部/左隅から測定され、そのコーナーから距離を記述している

あなたのコードは、位置2407,804から300x200の領域を取得するには、次のようになります:

left = 2407
top = 804
width = 300
height = 200
box = (left, top, left+width, top+height)
area = img.crop(box)

他のヒント

これを試してください:

これは、画像をトリミングするための簡単なコードだし、それは魔法のように動作します。)

import Image

def crop_image(input_image, output_image, start_x, start_y, width, height):
    """Pass input name image, output name image, x coordinate to start croping, y coordinate to start croping, width to crop, height to crop """
    input_img = Image.open(input_image)
    box = (start_x, start_y, start_x + width, start_y + height)
    output_img = input_img.crop(box)
    output_img.save(output_image +".png")

def main():
    crop_image("Input.png","output", 0, 0, 1280, 399)

if __name__ == '__main__': main()

この場合には、入力画像が1280×800ピクセルであり、croped左上隅から始まる1280×399pxである。

ライセンス: CC-BY-SA帰属
所属していません StackOverflow
scroll top