質問をすることでしか得られない、回答やアドバイスがある。

15分調べてもわからないことは、質問しよう!

新規登録して質問してみよう
ただいま回答率
85.48%
Python

Pythonは、コードの読みやすさが特徴的なプログラミング言語の1つです。 強い型付け、動的型付けに対応しており、後方互換性がないバージョン2系とバージョン3系が使用されています。 商用製品の開発にも無料で使用でき、OSだけでなく仮想環境にも対応。Unicodeによる文字列操作をサポートしているため、日本語処理も標準で可能です。

Q&A

0回答

1352閲覧

画像が保存されない...

tadapolice

総合スコア21

Python

Pythonは、コードの読みやすさが特徴的なプログラミング言語の1つです。 強い型付け、動的型付けに対応しており、後方互換性がないバージョン2系とバージョン3系が使用されています。 商用製品の開発にも無料で使用でき、OSだけでなく仮想環境にも対応。Unicodeによる文字列操作をサポートしているため、日本語処理も標準で可能です。

0グッド

0クリップ

投稿2020/06/02 13:52

フォルダから画像(.BMP)を読み込んで,ノイズを付加した後に画像を保存したいです.
実行しても保存先に指定したフォルダに何もありません.
解決方法や原因わかりますでしょうか?```

#画像を読み込む関数
def load_images(inputpath, imagesize, type_color):
imglist = []

for root, dirs, files in os.walk(inputpath): for fn in sorted(files): bn, ext = os.path.splitext(fn) if ext not in [".bmp", ".BMP", ".jpg", ".JPG", ".jpeg", ".JPEG", ".png", ".PNG"]: continue filename = os.path.join(root, fn) if type_color == 'Color': # カラー画像の場合 testimage = cv2.imread(filename, cv2.IMREAD_COLOR) # サイズ変更 height, width = testimage.shape[:2] testimage = cv2.resize(testimage, (imagesize, imagesize), interpolation = cv2.INTER_AREA) #主に縮小するのでINTER_AREA使用 testimage = np.asarray(testimage, dtype=np.float64) # チャンネル,高さ,幅に入れ替え.data_format="channels_first"を使うとき必要 #testimage = testimage.transpose(2, 0, 1) # チャンネルをbgrの順からrgbの順に変更 testimage = testimage[::-1] elif type_color == 'Gray': # グレースケール画像の場合 testimage = cv2.imread(filename, cv2.IMREAD_GRAYSCALE) # サイズ変更 height, width = testimage.shape[:2] testimage = cv2.resize(testimage, (512, 512), interpolation = cv2.INTER_AREA) #主に縮小するのでINTER_AREA使用 # チャンネルの次元がないので1次元追加する testimage = np.asarray([testimage], dtype=np.float64) testimage = np.asarray(testimage, dtype=np.float64).reshape((1,512,512)) # 高さ,幅,チャンネルに入れ替え.data_format="channels_last"を使うとき必要 testimage = testimage.transpose(1, 2, 0) imglist.append(testimage) imgsdata = np.asarray(imglist, dtype=np.float32) return imgsdata, sorted(files) # 画像リストとファイル名のリストを返す

#ノイズを付加
def generate_noise(imagelist):
imagelist_out=[]

for i in range(0,len(imagelist)):
image_temp=imagelist[i]+np.random.normal(loc=0.0,scale=50.0,size=imagelist[i].shape)
image_temp=np.clip(image_temp,0,255)
imagelist_out.append(image_temp)
imgsdata=np.asarray(imagelist_out,dtype=np.float32)

return imgsdata

#画像を保存
def save_images(savepath, filenamelist, imagelist):
for i, fn in enumerate(filenamelist):
filename = os.path.join(savepath, fn)
testimage = imagelist[i].reshape(512,512)
#testimage = np.delete(testimage, 2, 1)
cv2.imwrite(filename, testimage)

#画像を読み込む,ノイズを付加,保存
image_train, image_train_filenames = load_images("drive/My Drive/denoise/image/train", (512, 512), 'Gray')
image_test, image_test_filenames = load_images("drive/My Drive/denoise/image/test", (512, 512), 'Gray')

image_train=generate_noise(image_train)
image_test=generate_noise(image_test)

save_images("drive/My Drive/denoise/train_noise",image_train_filenames,image_train)
save_images("drive/My Drive/denoise/test_noise",image_test_filenames,image_test)

コード

気になる質問をクリップする

クリップした質問は、後からいつでもMYページで確認できます。

またクリップした質問に回答があった際、通知やメールを受け取ることができます。

バッドをするには、ログインかつ

こちらの条件を満たす必要があります。

hope_mucci

2020/06/04 07:44

コードは<code>ボタンを使って正しく整形してください。ちゃんとプレビューを確認すること。pythonはインデント命なのでこの状態ではコードテストできません。下記のヘルプを読んでコード部分を修正してください。 https://teratail.com/help/question-tips#questionTips3-5
guest

あなたの回答

tips

太字

斜体

打ち消し線

見出し

引用テキストの挿入

コードの挿入

リンクの挿入

リストの挿入

番号リストの挿入

表の挿入

水平線の挿入

プレビュー

まだ回答がついていません

会員登録して回答してみよう

アカウントをお持ちの方は

15分調べてもわからないことは
teratailで質問しよう!

ただいまの回答率
85.48%

質問をまとめることで
思考を整理して素早く解決

テンプレート機能で
簡単に質問をまとめる

質問する

関連した質問