Created
August 23, 2017 15:19
-
-
Save zarzen/c9b67ec161072637fef2d0dc196b5eee to your computer and use it in GitHub Desktop.
change_pixel
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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