Skip to content

Instantly share code, notes, and snippets.

@zarzen
Created August 23, 2017 15:19
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save zarzen/c9b67ec161072637fef2d0dc196b5eee to your computer and use it in GitHub Desktop.
Save zarzen/c9b67ec161072637fef2d0dc196b5eee to your computer and use it in GitHub Desktop.
change_pixel
import scipy.misc
def change_pixel(img, new_name):
"""
Args: img, numpy array
Returns:
new_img
"""
new_img = img.copy()
nums = [1, 3, 5, 7]
n = 8
w = new_img.shape[0]
new_img = new_img.astype(int)
for i in range(3):
for j in nums:
m = (int)(w * j / n) - 1
l = m - 1
r = m + 1
# column
c_m = new_img[:, m, i]
c_l = new_img[:, l, i]
c_r = new_img[:, r, i]
avg_c = (c_m + c_l + c_r) / 3
avg_c = avg_c.astype(int)
new_img[:, m, i] = avg_c
new_img[:, l, i] = avg_c
new_img[:, r, i] = avg_c
# row
r_m = new_img[m, :, i]
r_l = new_img[l, :, i]
r_r = new_img[r, :, i]
avg_r = (r_m + r_l + r_r) / 3
avg_r = avg_r.astype(int)
new_img[m, :, i] = avg_r
new_img[l, :, i] = avg_r
new_img[r, :, i] = avg_r
# save new image
scipy.misc.imsave(new_name, new_img)
if __name__ == "__main__":
img1 = scipy.misc.imread('1.png')
img2 = scipy.misc.imread('2.png')
change_pixel(img1, 'new_img1.png')
change_pixel(img2, 'new_img2.png')
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment