11
10

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.

Numpy.ndarrayを切り抜きして画像として保存する

Posted at

OpenCVやCaffeなどの画像処理系ライブラリで画像イメージをNdarrayに変換して扱うことが多いですが、これをndarrayとして切抜きして、画像として保存するのに一瞬迷ったので備忘までに

image_crop.py
import numpy as np
import scipy as sp
import Image

# 画像座標は以下のように変数に格納
# x: x軸の開始座標
# y: y軸の開始座標
# w: x軸からcropしたいpix数
# h: y軸からcropしたいpix数

# PILを使う場合
pic = Image.open(input_file_path)
imgar = np.asarray(pic.crop((x, y, x+w, y+h)))
sp.misc.imsave('test.jpg', imgar)

# ndarrayそのまま扱いたい場合
# PILとx,yが逆になっていることを注意
imgar = nd[y: y+h, x: x+w]
sp.misc.imsave('test.jpg', imgar)

11
10
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
11
10

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?