Qiita Teams that are logged in
You are not logged in to any team

Log in to Qiita Team
Community
OrganizationEventAdvent CalendarQiitadon (β)
Service
Qiita JobsQiita ZineQiita Blog
48
Help us understand the problem. What are the problem?

More than 1 year has passed since last update.

@ikanamazu

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

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

Why not register and get more from Qiita?
  1. We will deliver articles that match you
    By following users and tags, you can catch up information on technical fields that you are interested in as a whole
  2. you can read useful information later efficiently
    By "stocking" the articles you like, you can search right away
48
Help us understand the problem. What are the problem?