|
# Path Setup
import os
os.system('cls') # for windows or Ctrl+L
os.chdir('path') # path setup
# Image loading
img = Image.open('Lenna.png')
img.show(img) #RGB
#img = Image.open('lenna.png').convert('L') #Gray-scale
img.save(os.path.join(os.getcwd(),'imgNew.tiff'),'tiff') # getcwd gets the current path
# Convolution filtering
import numpy as np
import scipy.ndimage
from scipy.misc.pilutil import Image
b = scipy.ndimage.filters.convolve(img,k)
k = np.ones((5,5))/25
b = scipy.misc.toimage(b)
b.show()
# Contrast stretching
import math, numpy
#import scipy.misc
#from scipy.misc.pilutil import Image
img1=scipy.misc.fromimage(img) # convert img (image class) to an ndarrary
# img1 = numpy.asarray(img)
b = img1.min()
a = img1.max()
# print a,b
# converting img1 to float
c = img1.astype(float) # uint8 to floating precision
# contrast stretching
img2 = 255*(c - a)/(b - a)
img3 = scipy.misc.toimage(img2) # converted back to image class
img3.show()
# Fourier transform
# import math, numpy
import scipy.fftpack as fftim
# from scipy.misc.pilutil import Image
img1 = numpy.asarray(a)
fft1 = abs(fftim.fft2(img1))
fft2 = fftim.fftshift(fft1)
import pylab as py
py.imshow(fft2)
Archiver|手机版|科学网 ( 京ICP备07017567号-12 )
GMT+8, 2025-1-17 02:32
Powered by ScienceNet.cn
Copyright © 2007- 中国科学报社