從零開始的圖片處理程序python實(shí)現(xiàn)

博客網(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

?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時請結(jié)合常識與多方信息審慎甄別。
平臺聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點(diǎn),簡書系信息發(fā)布平臺,僅提供信息存儲服務(wù)。

相關(guān)閱讀更多精彩內(nèi)容

  • Android 自定義View的各種姿勢1 Activity的顯示之ViewRootImpl詳解 Activity...
    passiontim閱讀 179,036評論 25 709
  • 這周的三個作業(yè)都是有關(guān)服裝的問題,想來想去要如何動筆,最后我把三個題目放在了一塊整理。 曾經(jīng)看過一部電視劇里有這么...
    Lisa_su閱讀 682評論 0 0
  • 《了不起的狐貍爸爸》中有三個飼養(yǎng)場主,都是你遇到的最卑鄙齷蹉的人。他們對面小山的樹洞里住著狐貍先生、狐貍太太和...
    鄔文敬閱讀 792評論 0 2

友情鏈接更多精彩內(nèi)容