資源簡介
圖片的旋轉方法算法以及雙線性插值,最鄰近插值法
圖片的旋轉方法算法以及雙線性插值,最鄰近插值法
代碼片段和文件信息
from?PIL?import?Image
import?numpy?as?np?
def?shear(imgshxshy):
shx=0.5
shy=0.5
desImg?=?Image.new(img.mode(int(img.width?+?shx*img.height)int(img.width?*?shy?+?img.height)))
print(img.size)
convertMatrix?=?[[1shx0][shy10][001]]
for?x?in?range(img.width):
for?y?in?range(img.height):
color?=?img.getpixel((xy))
pos?=?[xy1]
pos_convert?=?np.dot(convertMatrixpos)
#print(pos_convert)
desImg.putpixel((int(pos_convert[0])int(pos_convert[1]))color)
return?desImg
def?Bilinear_interpolation(imgSrcdesImgposJudgeoriginPos):
if?(imgSrc.width?>??originPos[0]?+?1?and?imgSrc.height?>??originPos[1]?+?1)?and?(originPos>=0).all():
x_low?=?np.floor(originPos[0])
x_up?=?np.ceil(originPos[0])
y_low?=?np.floor(originPos[1])
y_up?=?np.ceil(originPos[1])
s?=?originPos[0]?-?x_low
t?=?originPos[1]?-?y_low
try:
p1?=?np.array(imgSrc.getpixel((x_lowy_low)))
p2?=?np.array(imgSrc.getpixel((x_upy_low)))
p3?=?np.array(imgSrc.getpixel((x_lowy_up)))
p4?=?np.array(imgSrc.getpixel((x_upy_up)))
colorReal?=?np.array((1-s)*(1-t)*p1+(1-s)*t*p3+(1-t)*s*p2+s*t*p4dtype=“int“)
desImg.putpixel((posJudge[0]posJudge[1])tuple(colorReal))
except:
print(x_low“??“x_up?“??“y_low“??“y_up“?“originPos)
def?nearest(imgSrcdesImgposJudgeoriginPos):
if?(imgSrc.width?>??originPos[0]?+?1?and?imgSrc.height?>??originPos[1]?+?1)?and?(originPos>=0).all():
x_low?=?np.floor(originPos[0])
x_up?=?np.ceil(originPos[0])
y_low?=?np.floor(originPos[1])
y_up?=?np.ceil(originPos[1])
#需要獲取的像素的位置
x_get?=?0
y_get?=?0
if?originPos[0]?-?x_low>=0.5:
x_get?=?x_up
else:
x_get?=?x_low
if?originPos[1]?-?y_low>=0.5:
y_get=y_up
else:
y_get=y_low
try:
colorReal?=?np.array(imgSrc.getpixel((x_gety_get)))
desImg.putpixel((posJudge[0]posJudge[1])tuple(colorReal))
except:
print(x_low“??“x_up?“??“y_low“??“y_up“?“originPos)
def?rotate(imgangle):
#maxEdge?=?int(np.sqrt(img.width?**?2?+?img.height?**?2))
beta?=?angle/180?*?np.pi
newWidth?=?int(img.width?*?np.cos(beta)?+?img.height?*?np.sin(beta))
newHeight?=?int(img.width?*?np.sin(beta)?+?img.height?*?np.cos(beta))
desImg?=?Image.new(img.mode(newWidthnewHeight))
#print(desImg.size)
convertMatrix?=?[[np.cos(beta)-np.sin(beta)0][np.sin(beta)np.cos(beta)0][001]]
#m其實是為了乘上pos使得以圖片中心點為原點的,但本身前面還要乘?convertMatrix,所以在前面先乘好
m?=?[[10-img.width/2]?[01-img.height/2]?
評論
共有 條評論