我正在尝试浏览我用复盆子pi相机拍摄的png的每个像素,并选择性地更改高于或低于某个r、g或b值的像素。我知道这是一个非常低效的算法,我只是想了解一下python脚本。我的代码是基于@Constantin在以下问题中的代码:
How can I read the RGB value of a given pixel in Python?
他的密码如下。
import png, array
point = (2, 10)
reader = png.Reader(filename='image.png')
w, h, pixels, metadata = reader.read_flat()
pixel_byte_width = 4 if metadata['alpha'] else 3
pixel_position = point[0] + point[1] * w
new_pixel_value = (255, 0, 0, 0) if metadata['alpha'] else (255, 0, 0)
pixels[
pixel_position * pixel_byte_width :
(pixel_position + 1) * pixel_byte_width] = array.array('B', new_pixel_value)
output = open('image-with-red-dot.png', 'wb')
writer = png.Writer(w, h, **metadata)
writer.write_array(output, pixels)
output.close()
我把它改为:
import png, array
reader = png.Reader(filename='test.png')
w, h, pixels, metadata = reader.read_flat()
pixel_byte_width = 4 if metadata['alpha'] else 3
for x in range(w):
for y in range(h):
point_index = x+(y-1)*w
r = pixels[point_index * pixel_byte_width + 0]
g = pixels[point_index * pixel_byte_width + 1]
b = pixels[point_index * pixel_byte_width + 2]
pixel = pixels[point_index * pixel_byte_width :
(point_index+1) * pixel_byte_width]
new_pixel = (0, 0, 0, 0) if metadata['alpha'] else (0, 0, 0)
pixel = array.array('B', new_pixel)
output = open('test_edited.png', 'wb')
writer = png.Writer(w, h, **metadata)
writer.write_array(output, pixels)
output.close()
发生的事情是pi思考一两分钟,然后我可以打开一个完全相同的新png。我的脚本缺少了什么,或者有比python更好的平台来对Raspbian Jessie进行逐像素处理吗?
非常感谢!