問題点
あるRGB画像を読み込み、RGBの値(輝度値)を全て50あげて保存するプログラムを作成しているのですが、
できあがった画像は一部のピクセルにおいて[0,0,0]になってしまい、結果的に2枚目の画像のような
虫食いになってしまいます。
各ピクセルのRGB値を出力してみるとやはり以下のとおりいくつかのピクセルのRGB値が[0,0,0]になっていました。どうすれば解決できるでしょうか。
環境
Python 3.8.5
Windows 10
結果
変換前:
変換後:
出力結果(一部)
[50 50 50]
[50 50 50]
[50 50 50]
[50 50 50]
[50 50 50]
[50 50 50]
[50 50 50]
[50 50 50]
[50 50 50]
[50 50 50]
[50 50 50]
[50 50 50]
[50 50 50]
[50 50 50]
[50 50 50]
[50 50 50]
[50 50 50]
[50 50 50]
[50 50 50]
[0 0 0]
[0 0 0]
[0 0 0]
[0 0 0]
[0 0 0]
[0 0 0]
[0 0 0]
[0 0 0]
[0 0 0]
[0 0 0]
[0 0 0]
[0 0 0]
[0 0 0]
[0 0 0]
[0 0 0]
[0 0 0]
[0 0 0]
[0 0 0]
[0 0 0]
[50 50 50]
[50 50 50]
[50 50 50]
[50 50 50]
[50 50 50]
[50 50 50]
[50 50 50]
[50 50 50]
[50 50 50]
[50 50 50]
[50 50 50]
[50 50 50]
[50 50 50]
[50 50 50]
[50 50 50]
[50 50 50]
[50 50 50]
[50 50 50]
ソースコード
def main():
width = 416
height = 416
num = int(1)
os.chdir("d:\\study_data\\get_image\\photo\\thermal_face-PascalVOC-export\\JPEGImages")
while True:
filename = 'img%20('+format(num)+').jpg'
if os.path.exists(filename):
image = cv2.imread(filename)
else:
print("\nEnd")
break
for y in range(0,height):
for x in range(0,width):
image[y,x,0] = image[y,x,0] + 50
if image[y,x,0] > 255:
image[y,x,0] = 255
image[y,x,1] = image[y,x,1] + 50
if image[y,x,1] > 255:
image[y,x,1] = 255
image[y,x,2] = image[y,x,2] + 50
if image[y,x,2] > 255:
image[y,x,2] = 255
#print(image[x,y])
cv2.imwrite('d:/study_data/get_image/photo/thermal_face-PascalVOC-export/Copy/img%20('+format(num)+').jpg', image)
num += 1
print("\rNo. %d" %num, end='')
if __name__ == '__main__':
main()