博客網(wǎng)站鏈接:http://haoxinl.club/2018/02/18/pic-deal/
github地址:https://github.com/haoxinl/pic_deal
首先美圖獻(xiàn)上^^
image
前言
最近搭建了博客,現(xiàn)在把以前做過的小東西做個記錄放上來-。
這是一個圖片處理小程序,我個人還是用的比較順手的,畢竟學(xué)生黨么,經(jīng)常要交各種不同類型,尺寸的照片。申請一些東西也經(jīng)常會用到。
正文
依賴庫
- PIL
- matplotlib
- tkinter(可選)
實(shí)現(xiàn)功能
- 單張圖片按輸入大小縮放,按給定角度旋轉(zhuǎn),改變格式
- 批量圖片按比例縮放,按輸入大小縮放,改變格式
- 實(shí)現(xiàn)圖形用戶界面(使用tkinter)
程序?qū)崿F(xiàn)
首先引入依賴庫,這些應(yīng)該是我們常用的,應(yīng)該不會陌生。
from PIL import Image
import matplotlib.pyplot as plt
from os import listdir
from os.path import isfile, join
之后編寫我們的single_test函數(shù)
def single_test():
img=input('原始圖片:')
img = Image.open(img)
print('原始照片尺寸為: ',img.size)
print('格式為: ',img.format)
print('如果你想改變圖片大小請輸入1,想旋轉(zhuǎn)圖片請輸入2')
num = input('輸出你的決定:')
while(num):
if int(num)==1:
w=input('你想轉(zhuǎn)變的寬度:')
h=input('你想轉(zhuǎn)變的高度:')
name=input('新圖片的名稱')
new_img= img.resize((int(w), int(h)),Image.ANTIALIAS)
new_img.save(name)
plt.figure('resize')
plt.subplot(121)
plt.title('before resize')
plt.imshow(img)
plt.subplot(122)
plt.title('after resize')
plt.imshow(new_img)
plt.show()
break
elif int(num)==2:
jiaodu=input('旋轉(zhuǎn)的角度:')
new_img = img.rotate(int(jiaodu))
plt.figure('rotate')
name = input('新圖片的名稱')
new_img.save(name)
plt.subplot(121)
plt.title('before rotate ')
plt.imshow(img)
plt.subplot(122)
plt.title('after rotate')
plt.imshow(new_img)
plt.show()
break
else:
print('輸錯了,請重新輸入\n')
num = input('輸出你的決定:')
然后的批量處理函數(shù)大同小異
def batch_test():
path=input('輸入圖片的地址:')
name = [f for f in listdir(path) if isfile(join(path, f))]
fname=[join(path, f) for f in name]
print('如果你想按比例改變圖片大小請輸入1,按固定大小請輸入2,想改變圖片的格式請輸入3')
num=input('你的決定:')
while(num):
if int(num)==1:
k1 = input('輸入寬的縮放比例')
k2= input('輸入高的縮放比例')
for pic in fname:
img = Image.open(pic)
(x, y) = img.size
new_img = img.resize((int(x*float(k1)), int(y*float(k1))),Image.ANTIALIAS)
pic_name=join(path,'new1_'+name[fname.index(pic)])
new_img.save(pic_name)
break
elif int(num)==2:
w = input('輸入寬')
h= input('輸入高')
for pic in fname:
img = Image.open(pic)
(x, y) = img.size
new_img = img.resize((int(w), int(h)),Image.ANTIALIAS)
pic_name=join(path,'new2_'+name[fname.index(pic)])
new_img.save(pic_name)
break
elif int(num) == 3:
geshi=input('請輸入你想要改變到的格式')
for pic in fname:
img = Image.open(pic)
pic_name=join(path,'new3_'+name[fname.index(pic)].split('.')[0]+'.'+str(geshi))
img.save(pic_name)
break
else:
print('輸錯了,請重新輸入\n')
num = input('你的決定:')
最后便是主函數(shù)編寫
if __name__=="__main__":
yournum=int(input('___________1.single test__________\n___________2.batch test___________\n請選擇'))
while (yournum):
if int(yournum)==1:
single_test()
print('任務(wù)完成了,快去查看吧')
break
elif int(yournum)==2:
batch_test()
print('任務(wù)完成了,快去查看吧')
break
else:
print('重新輸入\n')
yournum = int(input('___________1.single test__________\n___________2.batch test___________\n請選擇'))
以單張圖片測試結(jié)果如下

tup.png
如上圖,成功改變了圖片的大小

gm.png
如上圖,同時旋轉(zhuǎn)了圖片90度,也改變了圖片格式
寫在后面
由于文中所用的庫都比較常用,所以不做介紹,至于前文所說的圖形界面程序主要在于tkinter的使用,其他思想方面基本一致。
所以詳情請見https://github.com/haoxinl/pic_deal
