PEP 8 specifies four spaces per level of indentation. Since whitespace matters in Python, this is a pretty strong convention.
You should round()
the results to the nearest integer rather than truncating them towards 0.
Each of the three channels is treated identically and independently, so you shouldn't write the same code three times.
def square_root_filter(input_image_file_name, output_image_file_name):
image_data = skimage.io.imread(input_image_file_name)
for row_data in image_data:
for pixel in row_data:
for channel in range(3):
pixel[channel] = round(256 * math.sqrt(pixel[channel] / 256))
skimage.io.imsave(output_image_file_name, image_data)
If you take advantage of the fact that channels
is a NumPy array, you can vectorize the calculation.
import numpy as np
import skimage.io
def square_root_filter(input_image_file_name, output_image_file_name):
image_data = skimage.io.imread(input_image_file_name)
for row_data in image_data:
for pixel in row_data:
pixel[:] = np.rint(256 * (pixel / 256) ** 0.5)
skimage.io.imsave(output_image_file_name, image_data)