0

file.bmp などの画像ファイルをインポートし、画像内の各ピクセルの RGB 値を読み取り、各行の RGB 値が最も高いピクセル (最も明るいピクセル)画面に出力しようとしています。Python を使用してそれを行う方法に関する提案はありますか?

4

2 に答える 2

1

scipy.misc.imreadさて、次のようにイメージを読み取って操作するために使用できます。

import scipy.misc
file_array = scipy.misc.imread("file.bmp")

def get_brightness(pixel_tuple):
   return sum([component*component for component in pixel_tuple])**.5 # distance from (0, 0, 0)

row_maxima = {}
height, width = len(file_array), len(file_array[0])
for y in range(height):
  for x in range(width):
    pixel = tuple(file_array[y][x]) # casting it to a tuple so it can be stored in the dict
    if y in row_maxima and get_brightness(pixel) > row_maxima[y]:
      row_maxima[y] = pixel
    if y not in row_maxima:
      row_maxima[y] = pixel
print row_maxima
于 2013-03-28T00:59:29.660 に答える
1

ここで numpy のパワーを大いに活用できます。以下のコードは、範囲 [0, 255] の「明るさ」を出力することに注意してください。

#!/bin/env python

import numpy as np
from scipy.misc import imread

#Read in the image
img = imread('/users/solbrig/smooth_test5.png')

#Sum the colors to get brightness
brightness = img.sum(axis=2) / img.shape[2]

#Find the maximum brightness in each row
row_max = np.amax(brightness, axis=1)
print row_max

画像にアルファ レイヤーがあると思われる場合は、次のようにします。

#!/bin/env python

import numpy as np
from scipy.misc import imread

#Read in the image
img = imread('/users/solbrig/smooth_test5.png')

#Pull off alpha layer
if img.shape[2] == 4:
    alph = img[:,:,3]/255.0
    img = img[:,:,0:3]
else:
    alph = np.ones(img.shape[0:1])

#Sum the colors to get brightness
brightness = img.sum(axis=2) / img.shape[2]
brightness *= alph

#Find the maximum brightness in each row
row_max = np.amax(brightness, axis=1)
print row_max
于 2013-03-28T02:02:59.633 に答える