Created
April 1, 2013 14:35
-
-
Save TacoSteemers/5285271 to your computer and use it in GitHub Desktop.
Small scipy/numpy/pyplot script to clean up a monochrome drawing and show the input and output side by side
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 numpy | |
from scipy import misc | |
import matplotlib.pyplot as plt | |
if __name__ == "__main__": | |
input = 'input.png' | |
output = 'output.png' | |
# Editing | |
imIn = misc.imread(input) | |
imOut = numpy.zeros( (len(imIn), len(imIn[0]), 4), dtype=int ) | |
i1 = -1 | |
i2 = -1 | |
for a in imIn: | |
i1 = i1 + 1 | |
i2 = -1 | |
for b in a: | |
i2 = i2 + 1 | |
if b[0] < 195: | |
#imOut[i1][i2][0] = b[0] | |
imOut[i1][i2][0] = 128 | |
imOut[i1][i2][1] = 0 | |
imOut[i1][i2][2] = 0 | |
imOut[i1][i2][3] = 255 | |
else: | |
imOut[i1][i2][0] = 255 | |
imOut[i1][i2][1] = 255 | |
imOut[i1][i2][2] = 255 | |
imOut[i1][i2][3] = 0 | |
misc.imsave(output, imOut) | |
# Plotting | |
plt.figure() | |
plt.subplot(121) | |
# Without reloading the plot | |
# will not show the new image. | |
plt.imshow(misc.imread(output)) | |
plt.axis('off') | |
rect = plt.figure(1).patch | |
rect.set_facecolor('white') | |
plt.subplot(122) | |
plt.imshow(imIn) | |
plt.axis('off') | |
plt.show() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment