對圖片進行resize、裁剪、旋轉、翻轉
#首先我們的原始圖片是10張網路下載尺寸不一的圖片,如下:
操作1:resize 將圖片resize到相同尺寸(320,240)
from PIL import Image import torchvision.transforms as transforms #使用PIL库读入图片并进行resize def ResizeImage(): if not os.path.exists(rdir): os.makedirs(rdir) for i in range(10): im = Image.open(dir+str(i)+".jpg") im = im.resize((320,240),Image.BILINEAR) #第一个参数为想要的size,第二个参数为插值方法,双线性插值这里用的是 im.save('{}/{}.jpg'.format(rdir, i))
操作2:剪裁(包括圍繞中心剪裁和隨機剪裁)
#图像随机剪裁和中心剪裁 def crop(lib): for i in range(10): img = Image.open(lib+"/"+str(i)+".jpg") CenterCrop = transforms.CenterCrop((240,320)) #中心裁剪 cropped_image = CenterCrop(img) #PIL.Image.Image # im=np.array(cropped_image) #可以将PIL.Image.Image转成ndarry #cropped_image.show() #将图片显示 cropped_image.save('{}/cen_crop{}.jpg'.format(rdir, i)) RandomCrop = transforms.RandomCrop(size=(240, 320)) #随机剪裁 random_image = RandomCrop(img) random_image.save('{}/rand_crop{}.jpg'.format(rdir, i))
操作3:隨機旋轉
#随机旋转 def random_rotation(lib): for i in range(10): img = Image.open(lib+"/"+str(i)+".jpg") RR = transforms.RandomRotation(degrees=(10, 80)) #degrees为随机旋转的角度 rr_image = RR(img) rr_image.save('{}/rand_rotation{}.jpg'.format(rdir, i))
操作4:翻轉
#图片依概率翻转,p为翻转的概率 def horizontal_flip(lib): for i in range(10): img = Image.open(lib+"/"+str(i)+".jpg") HF = transforms.RandomHorizontalFlip(p=1.0) #p为概率,缺省时默认0.5 hf_image = HF(img) hf_image.save('{}/hori_flip{}.jpg'.format(rdir, i))
下面展示一下操作後的圖片:
#從上到下每行依序為resize、中心裁剪、翻轉、隨機裁剪和隨機旋轉的結果
單張圖像變換大小——img.resize()
這個是一段學過的簡單程序,可以改變圖像的大小,jpg,png都可以的:
#encoding=utf-8 #author: walker #date: 2014-05-15 #function: 更改图片尺寸大小 from PIL import Image ''' filein: 输入图片 fileout: 输出图片 width: 输出图片宽度 height:输出图片高度 type:输出图片类型(png, gif, jpeg...) ''' def ResizeImage(filein, fileout, width, height, type): img = Image.open(filein) out = img.resize((width, height),Image.ANTIALIAS) #resize image with high-quality out.save(fileout, type) if __name__ == "__main__": filein = r'0.jpg' fileout = r'testout.png' width = 6000 height = 6000 type = 'png' ResizeImage(filein, fileout, width, height, type)
這個函數img.resize ((width, height),Image.ANTIALIAS)
第二個參數:
Image.NEAREST
:低品質Image.BILINEAR
:雙線性##Image.BICUBIC
:三次樣條插值
Image.ANTIALIAS
:高品質
以上是Python怎麼對圖片進行resize、裁切、旋轉、翻轉的詳細內容。更多資訊請關注PHP中文網其他相關文章!

Python是解釋型語言,但也包含編譯過程。 1)Python代碼先編譯成字節碼。 2)字節碼由Python虛擬機解釋執行。 3)這種混合機制使Python既靈活又高效,但執行速度不如完全編譯型語言。

UseeAforloopWheniteratingOveraseQuenceOrforAspecificnumberoftimes; useAwhiLeLoopWhenconTinuingUntilAcIntiment.forloopsareIdealForkNownsences,而WhileLeleLeleLeleLeleLoopSituationSituationsItuationsItuationSuationSituationswithUndEtermentersitations。

pythonloopscanleadtoerrorslikeinfiniteloops,modifyingListsDuringteritation,逐個偏置,零indexingissues,andnestedloopineflinefficiencies

forloopsareadvantageousforknowniterations and sequests,供應模擬性和可讀性;而LileLoopSareIdealFordyNamicConcitionSandunknowniterations,提供ControloperRoverTermination.1)forloopsareperfectForeTectForeTerToratingOrtratingRiteratingOrtratingRitterlistlistslists,callings conspass,calplace,cal,ofstrings ofstrings,orstrings,orstrings,orstrings ofcces

pythonisehybridmodeLofCompilation和interpretation:1)thepythoninterpretercompilesourcecececodeintoplatform- interpententbybytecode.2)thepythonvirtualmachine(pvm)thenexecutecutestestestestestesthisbytecode,ballancingEaseofuseEfuseWithPerformance。

pythonisbothinterpretedAndCompiled.1)它的compiledTobyTecodeForportabilityAcrosplatforms.2)bytecodeisthenInterpreted,允許fordingfordforderynamictynamictymictymictymictyandrapiddefupment,儘管Ititmaybeslowerthananeflowerthanancompiledcompiledlanguages。

在您的知識之際,而foroopsareideal insinAdvance中,而WhileLoopSareBetterForsituations則youneedtoloopuntilaconditionismet

ForboopSareSusedwhenthentheneMberofiterationsiskNownInAdvance,而WhileLoopSareSareDestrationsDepportonAcondition.1)ForloopSareIdealForiteratingOverSequencesLikelistSorarrays.2)whileLeleLooleSuitableApeableableableableableableforscenarioscenarioswhereTheLeTheLeTheLeTeLoopContinusunuesuntilaspecificiccificcificCondond


熱AI工具

Undresser.AI Undress
人工智慧驅動的應用程序,用於創建逼真的裸體照片

AI Clothes Remover
用於從照片中去除衣服的線上人工智慧工具。

Undress AI Tool
免費脫衣圖片

Clothoff.io
AI脫衣器

Video Face Swap
使用我們完全免費的人工智慧換臉工具,輕鬆在任何影片中換臉!

熱門文章

熱工具

VSCode Windows 64位元 下載
微軟推出的免費、功能強大的一款IDE編輯器

WebStorm Mac版
好用的JavaScript開發工具

mPDF
mPDF是一個PHP庫,可以從UTF-8編碼的HTML產生PDF檔案。原作者Ian Back編寫mPDF以從他的網站上「即時」輸出PDF文件,並處理不同的語言。與原始腳本如HTML2FPDF相比,它的速度較慢,並且在使用Unicode字體時產生的檔案較大,但支援CSS樣式等,並進行了大量增強。支援幾乎所有語言,包括RTL(阿拉伯語和希伯來語)和CJK(中日韓)。支援嵌套的區塊級元素(如P、DIV),

SAP NetWeaver Server Adapter for Eclipse
將Eclipse與SAP NetWeaver應用伺服器整合。

記事本++7.3.1
好用且免費的程式碼編輯器