Last active
October 31, 2017 17:48
-
-
Save mpilosov/18ae1b8ea3b7103b26e310f4f2b2a04b to your computer and use it in GitHub Desktop.
Images with Python
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
from matplotlib import pyplot as plt # the standard python plotting library | |
# installation instructions here: https://github.com/matplotlib/matplotlib | |
import numpy as np # math library for data-handling | |
# DEMO on random matrix to show how filtering based on pixel intensity works. How to make an image. | |
n = 20 | |
I = np.random.random((n, n)) | |
I[I>0.5] = 1 # highpass filter in two lines of code. | |
I[I<0.5] = 0 | |
colormap = plt.cm.gray # any of these: https://matplotlib.org/users/colormaps.html | |
plt.imshow(I, cmap=colormap) # plot in Black and White | |
# this line below tells matplotlib "I'm done constructing my image (titles, labels, etc.), now show me it." sometimes unecessary. depends on how you're interacting with python | |
plt.show() | |
# HOW TO LOAD A FILE | |
filename = 'myfile.png' | |
I = plt.imread(filename) # this is how to load an image | |
plt.imshow(I) | |
plt.title('Original Image'); plt.ylabel('px'); plt.xlabel('px'); | |
plt.show() | |
# CONVERT image to grayscale | |
def rgb2gray(rgb): | |
r, g, b = rgb[:,:,0], rgb[:,:,1], rgb[:,:,2] | |
gray = 0.2989 * r + 0.5870 * g + 0.1140 * b | |
return gray | |
G = rgb2gray(I) | |
# OPTIONAL - CROPPING | |
# G = G[375:1175,330:950] # overwrite image with cropped version | |
plt.imshow(G) | |
plt.show() | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment