Skip to content

Instantly share code, notes, and snippets.

@Jswoon

Jswoon/Python Secret

Last active November 17, 2018 19:58
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save Jswoon/ba77acbca67d9dffba136670423b62b1 to your computer and use it in GitHub Desktop.
Save Jswoon/ba77acbca67d9dffba136670423b62b1 to your computer and use it in GitHub Desktop.
Python Coded Image Processor
from scipy import misc,ndimage #misc is used to read and save the image and ndimage is for the ready made filter like gaussian_filter and rotate
import matplotlib.pyplot as plt
import numpy as np
def show_an_image(filename):
image = misc.imread(filename) #read the image
plt.imshow(image)
plt.show () #show the image
return None
#Put Behind Bar - put some vertical bars with width and spacing equal to 50 pixels where color of black bars are (0,0,0)
def put_behind_bar(filename):
image = misc.imread(filename)
xlen,ylen = image.shape[0], image.shape[1]
for i in range (xlen): #to scan the row pixel by pixel
for j in range(ylen): #to scan the column pixel by pixel
if int(j/50) %2 == 0: #to check for n x 50 pixel
image[i][j] = 0 #to convert the RGB to black
plt.imshow(image)
plt.show()
misc.imsave('put_behind_bar_output.jpg', image) #save the image as 'put_behind_bar_output.jpg'
return None
#Put Behind Bar Transparent- put some vertical bars with width and spacing equal to 50 pixels where bars are half the values of the original pixel
def put_behind_bar_transparent(filename):
image = misc.imread(filename)
xlen,ylen = image.shape[0], image.shape[1]
for i in range (xlen):
for j in range(ylen):
if int(j/50) %2 == 0:
image[i][j] = image[i][j]/2 #half the original pixel
plt.imshow(image)
plt.show()
misc.imsave('put_behind_bar_transparent_output.jpg', image) #save the picture
return
#Mirror - flip the image horizontally and overlay the flipped and original pictures together
def mirror_image(filename):
image = misc.imread(filename)
#image2 = ndimage.gaussian_filter(image,sigma=(1,1,1))
x,y = image.shape[0], image.shape[1] #
for i in range (x):
for j in range(y):
w = y
image[i][j] = image[i][j]/2+image[i][w-j-1]/2 #the RGB pixel is divided by 2
#image= image*[255,255,255]
image[i][w-j-1] = image[i][j] #add the pixel from the left half to the right half of the picture
plt.imshow(image)
plt.show()
misc.imsave('mirror_image_output.jpg',image)
return None
#Circle - to make a circle in the center of the picture
## the function of circle_pic, blur_image and rotate_image is given from another module IT1007
def circle_pic(filename):
pic = misc.imread(filename)
xlen, ylen = pic.shape[0], pic.shape[1]
xcenter, ycenter = xlen/2, ylen/2
pic2 = np.array(pic)
for i in range(xlen):
for j in range(ylen):
if (i-xcenter)**2 + (j-ycenter)**2 > (xlen/3)**2: #if the pixel is out of the circle
pic2[i][j] = pic2[i][j]/2 #the RGB pixel is divided by 2
plt.imshow(pic2)
plt.show()
misc.imsave("circle_pic_output.jpg",pic2)
#Blur image - to blur the image using gaussian filter
def blur_image(filename):
pic = misc.imread(filename)
#blur_pic = ndimage.gaussian_filter(pic, sigma=(5,5,1)) #to blur the image
#gaussian filter is to return the same shape as input
blurredpic = ndimage.gaussian_filter(pic, sigma=(5,5,0), order=0)
plt.subplot(121)
plt.imshow(pic, interpolation = 'nearest')
plt.subplot(122)
plt.imshow(blurredpic)
misc.imsave("blurredpic.jpg",blurredpic)
plt.show()
return
#Rotate image - rotate the picture by 45 degrees
def rotate_image(filename):
pic = misc.imread(filename)
rotate_1 = ndimage.rotate(pic, 45) #rotate the picture by 45 degrees
rotate_1_noreshape = ndimage.rotate(pic, 45, reshape=False) #rotate the picture by 45 degrees without reshaping it
plt.subplot(121)
plt.imshow(rotate_1)
plt.axis('off')
plt.subplot(122)
plt.imshow(rotate_1_noreshape)
plt.axis('off')
plt.show()
misc.imsave("rotate_image_noreshape_output.jpg",rotate_1_noreshape)
#Dye hair - change the hair color from green to pinkish. If a pixel is green, replace the color by [R x 2, G x 0.2, B x 0.8]
def dye_hair(filename):
image = misc.imread(filename)
image1 = misc.imread(filename)
xlen,ylen = image.shape[0],image.shape[1]
for i in range(xlen):
for j in range(ylen):
if image[i][j][0]<image[i][j][1]:
if image[i][j][1]>image[i][j][2]: #check if the pixel is green
image[i][j] = image[i][j]*[2, 0.2, 0.8] #multiply the pixel by [2 ,0.2 ,0.8]
plt.subplot(121)
plt.imshow(image1)
plt.axis('off')
plt.subplot(122)
plt.imshow(image)
plt.axis('off')
plt.show()
misc.imsave('dye_hair_output.jpg', image)
#Photoshop - 1st picture is Avenger picture with a green background and 2nd picture is NUS background.
# - Replace the green background with the NUS one and it will look like Avengers are in NUS!
def photoshop(filename1, filename2):
image1 = misc.imread(filename1)
image2 = misc.imread(filename2)
image3 = misc.imread(filename1)
for i in range(image1.shape[0]):
for j in range(image1.shape[1]):
if image1[i][j][1]>image1[i][j][0]:
if image1[i][j][1] > image1[i][j][2]: #check if the pixel is green
if image1[i][j][1]>110:
image1[i][j] = image1[i][j][1]*0 #multiply all the green pixel by 0
image1[i][j] = image1[i][j][1]+image2[i][j] #add the pixel of NUS background to it
plt.subplot(221) #print pic at first row left position
plt.imshow(image3) #print the Avengers pic
plt.axis('off') #off the axis
plt.subplot(222) #print pic at first row right position
plt.imshow(image2) #print the NUS Background pic
plt.axis('off')
plt.subplot(212) #print pic at second row center
plt.imshow(image1) #print the merge image of Avengers at NUS
plt.axis('off')
plt.show()
misc.imsave('aveNUgerS.jpg',image1) #save the image as 'aveNUgerS'
def image_processor():
print ('Welcome to GET1033 Python Coded Image Processor!')
Filename = input('Please enter the file name:') #promt the user for an image input
print ('Filename =', Filename)
print ('Please select an operation you want to perform')
#function_dict - to put all the functions written above into a dictionary so that user can select a choice here
function_dict = { '1': show_an_image, '2' : mirror_image, '3' : put_behind_bar,
'4': put_behind_bar_transparent, '5': circle_pic, '6': blur_image,
'7': rotate_image, '8': dye_hair}
print ('1. Show the image')
print ('2. Mirror image')
print ('3. Put behind bar')
print ('4. Put behind transparent bar')
print ('5. Circle Picture')
print ('6. Blurring')
print ('7. Rotation')
print ('8. Change Hair Colour')
print ('9. Photoshop')
print ('Q. Quit')
print ('R. Reset')
while 1>0: #infinity loop
choice = input('Enter your choice (1-9,Q, R) :')
if choice == 'R': #if the user want to change another picture, input R and it will reset the whole process
return image_processor();
else:
if choice == 'Q': #if the user want to quit the loop, input a Q
return None
else:
if choice == '9': #if the user want the photoshop function, input 9
photoshop('avengers green triangle.jpg','background.jpg')
else:
if choice in function_dict: #if the user input 1-8, he will obtain the different filters listed in the dictionary
function_dict[choice](Filename)
else:
print('Invalid choice!')
image_processor()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment