CS256
Chris Pollett
Oct 9, 2017
import numpyor
import numpy as np
a = np.array([2, -1, 4, -8], float) type(a) #outputs <type 'numpy.ndarray'>
a = np.array((1,2,3), float) a = np.array(range(1,5), float) # array([ 1., 2., 3., 4.]) a = np.arange(1,5, dtype=float) # this used a numpy method #but also gives array([ 1., 2., 3., 4.]) a = np.arange(1,5,2, dtype=float) # array([ 1., 3.]) (used a stride of 2)
np.zeros(5, dtype=float) # array([ 0., 0., 0., 0., 0.]) np.ones((3,2), dtype=int) # array([[1, 1], [1, 1], [1, 1]]) # can give a shape np.identity(3, dtype=float) # 3x3 identity matrix np.random.rand(5) # an array of size 5 with random values between 0 and 1 np.random.rand(3, 4) # 3 x 4 array of random values between 0 and 1
b = a #copy by reference b[1] = 4 a # array([1, 4, 3]) c = a.copy() # a new array with the same entries as a c[1] = 5 a # array([1, 4, 3])
a = np.array([[1, 2, 3], [4, 5, 6]], float) # a 2D array b = np.array([[[1,2], [2, 3]], [[4, 5], [6,7]]], float) # a 3d array
len(a) # 2 (the length of the first axis) a.shape # (2,3) a.reshape(6, 1) # array([[ 1.], [ 2.], [ 3.], [ 4.], [ 5.], [ 6.]]) c = b.flatten() # array([ 1., 2., 2., 3., 4., 5., 6., 7.])
b = np.array([ 3., 4., 1., 5., 6., 2., 7.]) #notice type inference b.sort(); # now b is array([ 1., 2., 2., 3., 4., 5., 6., 7.]) b.fill(4) # now b will contain array([ 4., 4., 4., 4., 4., 4., 4., 4.])
a[1][2] # outputs 6.0 on array a above b = np.array([2, -1, 4, -8], float) b[1] #outputs -1.0 b[2:] #outputs [4, -8.0]
b = np.array([2, -1, 4, -8], float) -8 in b #True for elt in b: print elt
a = np.array([[1, 2, 3], [4, 5, 6]], float) b = a[1,:] #now b is array([ 4., 5., 6.]) c = a[:,2] # now c is array([ 3., 6.])
a = np.array([1, 2, 3, 5], int) a.sum() # 11 a.prod() #30 a.mean() # 2.75 np.median(a) # 2.5 a.var() # 2.1875 a.min() # 1 a.max() # 5 a.argmin() # 0 a.argmax() # 3
a = np.array([1, 2, 3], int) b = np.array([4, 5, 6], int) a + b # array([5, 7, 9]) a * b # array([ 4, 10, 18]) # notice point-wise multiplication (Hadamard product) b / a # array([4, 2, 2]) b % a # array([0, 1, 0]) b ** a # array([ 4, 25, 216]) a = np.array([[1, 2], [4, 5]], int) b = np.array([[5, 4], [1, 2]], int) a + b # array([[6, 6], [5, 7]]) a * b # array([[ 5, 8], [ 4, 10]]) # notice Hadamard product
a = np.array([1, 2, 3], float) np.exp(a) # array([ 2.71828183, 7.3890561 , 20.08553692])
a = np.array([1, 2, 3], int) b = np.array([4, 5, 3], int) z = a == b # array([False, False, True], dtype=bool) np.logical_not(z) # array([ True, True, False], dtype=bool) #numpy also support logical_and(z, w) and logical_or(z, w)
a.transpose()
a = np.array([1, -1, 1], float) b = np.array([3, -2, 4], float) np.dot(a, b) # 9.0 np.cross(a,b) # array([-2., -1., 1.])
a = np.array([[2,3],[4,5]], int) b = np.array([[0,3],[0,5]], int) np.dot(a,b) # array([[ 0, 21], [ 0, 37]])
a = np.array([[2,3],[4,5]], int) np.linalg.det(a) # -2.0 np.linalg.inv(a) #array([[-2.5, 1.5], [ 2. , -1. ]]) eivals, eivecs = np.linalg.eig(a) eivals # array([-0.27491722, 7.27491722]) eivecs # array([[-0.79681209, -0.49436913], [ 0.60422718, -0.86925207]])
np.roots([1,2,-3]) # array([-3., 1.]) #roots of polynomial x^2 + 2x - 3 np.poly([1,2,3,4]) # array([ 1, -10, 35, -50, 24]) #coeff's of poly with roots 1,2,3,4
a = np.array([1, 2, 3], int) a.tolist() # [1, 2, 3] list(a) # [1, 2, 3] # same as above tuple(a) # (1, 2, 3) a.tofile("bob.txt") #writes to the file bob.txt c = a.tostring() # '\x01\x00\x00\x00\x00\x00\x00\x00\x02\x00\x00\x00 # \x00\x00\x00\x00\x03\x00\x00\x00\x00\x00\x00\x00'
b = np.fromfile("bob.txt",int) d = np.fromstring(c)
Which of the following is true?
pip install pillow
from PIL import Image
im = Image.open("myphoto.jpg") #Pillow supports many formats #png, ppm, gif, tiff, etc im.show() #this would display the image in a new window
im = Image.new('RGBA', (100, 100), (255,255,255,255)) #100x100 size image with white opaque background im.show();
from PIL import Image, ImageGrabber im = ImageGrab.grab() # capture whole screen im.show() im = ImageGrab.grab((10,10,250,250)) #captures from (10,10) to (250,250) of screen im.show()
out_im = im.resize((50,200)) #out_im is a rescaled image of size 50x 200 #can also specify as a second argument how to resample from: #Image.NEAREST , BILINEAR, BICUBIC, LANCZOS
from PIL import Image, ImageFilter im = Image.open("myphoto.jpg") out_im = im.rotate(30) out_im = im.transpose(Image.FLIP_LEFT_RIGHT) #other possibilities are # FLIP_TOP_BOTTOM, ROTATE_90, ROTATE_180, ROTATE_270 out_im = im.filter(ImageFilter.BLUR) #other possibilities are: # CONTOUR, DETAIL, EDGE_ENHANCE, EDGE_ENHANCE_MORE, EMBOSS, # FIND_EDGES, SMOOTH, SMOOTH_MORE, and SHARPEN # MinFilter(some_num), MaxFilter(some_num), MedianFilter(some_num), # ModeFilter(some_num), Kernel(size, kernel, scale=None, offset=0) # RankFilter(size, rank)
box = (20, 20, 80, 80) out_im = im.crop(box) #new image given by rectangle with top left (20,20) #bottom left (80, 80) box2 = (0, 0, 60, 60) im.paste(out_im, box2) # paste image back into old image at the given coords #rectangle in box2 has to be of same size as dimension of image
im1 = Image.open("myphoto1.jpg") im2 = Image.open("myphoto2.jpg") q = 0.5 out = Image.blend(im1, im2, q) #adds images to make output image #with weight 1-q to im1 and q to im2.
r, g, b, a = 0, 1, 2, 3 source = im1.split(); # creates 4 images one for each color component
blue_mask = source[b].point(lambda i: i > 50 and 255) # if i > 50 output 255; otherwise 0 mod_green = source[g].point(lambda i: i * 0.4) source[g].paste(mod_green, None, blue_mask) #mod_green is pasted back only where mask says #None component could have been a rectangle to restrict paste to
im1 = Image.merge(im1.mode, source)
from PIL import Image, ImageDraw, ImageFont im = Image.new('RGB', (100, 100), (0,0,0)) draw = ImageDraw.Draw(im) draw.point(((5, 5), (85, 5), (85, 6)), fill=(255, 0, 0)) #draw three red points draw.line((10,15,80,90), fill=(255,255,255)) #draw a white line draw.ellipse((30,30,40,50), fill=(0,0,255)) # draw a blue ellipse draw.text((20, 55), "hello world", font=ImageFont.load_default()) del draw im.show()
im = Image.new('RGB', (100, 100), (0,0,0)) draw = ImageDraw.Draw(im) draw.point(((5, 5), fill=(255, 0, 0)) del draw im.getpixel((5,5)) # (255, 0, 0)
list(im.getdata()) # Since im was in rgb, this will output a # sequence of triples [(0, 0, 0), (0, 0, 0), ... import numpy as np im_array = np.fromstring(im.tobytes(), dtype=np.uint8) #1-dimensional im_array.reshape((im.size[1], im.size[0], 3)) # make into 3d array where 3rd dimension color
im.save("out_image.png", "PNG") #so you can choose image file format when save