from PIL import Image import numpy as np # Location of the image img = Image.open("koko.jpg") # size of the image print(img.size) # format of the image print(img.format) # mode of the image xpixel = img.size[0] ypixel = img.size[1] #turn into editable list of colors data = np.asarray(img, dtype=np.uint8) data_new = np.zeros([ypixel, xpixel, 3], dtype=np.uint8) #apply sharpen convolution as SIMD/Vector instruction for y in range(data.shape[1]-1): #process all colors and the column convolution as vector operation #factors needed for type conversion to np.float64 to prevent integer overflow new_value = -1.0*data[:, y+1, :] + 2.5*data[:, y, :] - 1.0*data[:, y-1, :] new_value[new_value > 255] = 255 new_value[new_value < 0] = 0 data_new[:, y, :] = new_value for x in range(data.shape[0]-1): new_value = 1.0*data_new[x, :, :] - 1.0*data[x+1,:, :] + 2.5*data[x,:, :] - 1.0*data[x-1,:, :] new_value[new_value > 255] = 255 new_value[new_value < 0] = 0 data_new[x, :, :] = new_value #convert our new data into an image img_new = Image.fromarray(data_new) #the data and new image is 8-Bit unsigned integer per color -> PILLOW mode "RGB" img_new.convert("RGB") img_new.show()