本文實例講述了Ubuntu下使用Python實現游戲制作中的切分圖片功能。分享給大家供大家參考,具體如下:
why

拿到一個人物行走的素材,要用TexturePacker打包。TexturePacker打包后,助于游戲加載圖片效率,且比較好管理。
目前得到一張整圖,無法直接導入到TexturePacker。
what
切片:使用切片將源圖像分成許多的功能區域。
how
1 ubuntu下圖片處理軟件 GIMP: 畫好參考線后, 點擊 濾鏡->WEB ->切片
2 python + PIL (pip install pillow 安裝)
第一種手動太麻煩,不好精細自動化操作。
采用第二種
# coding=utf-8from PIL import Imageimport osdef mkdir(path): # 去除首位空格 path=path.strip() # 去除尾部 / 符號 path=path.rstrip("//") # 判斷路徑是否存在 # 存在 True # 不存在 False isExists=os.path.exists(path) # 判斷結果 if not isExists: # 如果不存在則創建目錄 print path+' 創建成功' # 創建目錄操作函數 os.makedirs(path) return True else: # 如果目錄存在則不創建,并提示目錄已存在 print path+' 目錄已存在' return Falsecnt = 0imageName = 'mageStand.png'pathName = 'mageStand'img = Image.open(imageName)ori_w,ori_h = img.sizerow = 4col = 4for j in range(0, col): Y = j*ori_h/col Y_end = Y + ori_h/col for i in range(0, row): X = i*ori_w/row X_end = X + ori_w/row print X, X_end if 8 == cnt: pathName+="adv" cnt = 0 mkdir(pathName) fileName = '%s/a_%d.png' %(pathName, cnt) img.crop((X, Y, X_end, Y_end)).save( fileName ) cnt+=1
希望本文所述對大家Python程序設計有所幫助。
新聞熱點
疑難解答