73
80

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 5 years have passed since last update.

【Python】画像の切り出し方法【OpenCV】

Posted at

pythonでopencvを使って画像をトリミングしたい場合、いつもググってしまう為自分用メモ兼誰かの役に立てば。

いつものレナさんに50ピクセルで補助線を引いた画像を例に切り出し。
lenasan.jpg

import cv2

# 画像読み込み
img = cv2.imread("lerasan.jpg")

# img[top : bottom, left : right]
# サンプル1の切り出し、保存
img1 = img[0 : 50, 0: 50]
cv2.imwrite("out_sample1.jpg", img1)

# サンプル2の切り出し、保存
img2 = img[50 : 150, 100 : 250]
cv2.imwrite("out_sample2.jpg", img2)

結果はこんな感じ。
サンプル1 sample1.jpg

サンプル2 sample2.jpg

73
80
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
73
80

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?