画像 切り出し、トリミング - OpenCV、Python徹底解説
OpenCVで簡単にイメージファイルをトリミングする方法を紹介します。
基本形
img[top : bottom , left : right]
サンプル
import cv2
# 画像の読込
img = cv2.imread( 'src.png')
img = img[36:192,53:153]
# 画像の保存
cv2.imwrite( 'dest.jpg' ,img)
画像はこちらを使わさせていただいております。
https://unsplash.com/photos/T-0EW-SEbsE
よくあるエラー
IndexError: index 12 is out of bounds for axis 2 with size 3
数字の部分は状況により違います。
- 画像のサイズより大きなサイズを指定していないでしょうか?
img[top : bottom , left : right] とすべきところを
img[left : right , top : bottom ] としていたりしませんか? - img[top : bottom , left : right]のコロン:とカンマ,の位置を間違っていませんか?
img[top , bottom : left , right] だとうまくいきません。
関連
もっとPythonを学びたいなら
Python徹底解説
Python - Pandas徹底解説
おすすめ記事
Unchecked runtime.lastError: The message port closed before a response was received. / Chrome extension
Raspberry pi Liteの非X Window環境でJackdを動かす
Django チーター#1 - Python
数値による条件抽出 - Python Pandas
エラーメッセージ集 Pandas徹底解説 - Python
オープンデータで航空写真や標高データをPythonで作る方法
Supponsered
外部サイト
↓プログラムを学んでみたい場合、学習コースなどもおすすめです!