pythonresizeimage
Ⅰ 如何利用python實現圖片轉字元畫詳解
# -*- coding: utf-8 -*-from PIL import Image
codeLib = '''@B%8&WM#*/\|()1{}[]?-_+~<>i!lI;:,"^`'. '''#生成字元畫所需的字元集count = len(codeLib)def transform1(image_file):
image_file = image_file.convert("L")#轉換為黑白圖片,參數"L"表示黑白模式
codePic = ''
for h in range(0,image_file.size[1]): #size屬性表示圖片的解析度,'0'為橫向大小,'1'為縱向
for w in range(0,image_file.size[0]):
gray = image_file.getpixel((w,h)) #返回指定位置的像素,如果所打開的圖像是多層次的圖片,那這個方法就返回一個元組
codePic = codePic + codeLib[int(((count-1)*gray)/256)]#建立灰度與字元集的映射
codePic = codePic+'\r\n'
return codePicdef transform2(image_file):
codePic = ''
for h in range(0,image_file.size[1]): for w in range(0,image_file.size[0]):
g,r,b = image_file.getpixel((w,h))
gray = int(r* 0.299+g* 0.587+b* 0.114)
codePic = codePic + codeLib[int(((count-1)*gray)/256)]
codePic = codePic+'\r\n'
return codePic
fp = open(u'暴走.jpg','rb')
image_file = Image.open(fp)
image_file=image_file.resize((int(image_file.size[0]*0.75), int(image_file.size[1]*0.5)))#調整圖片大小print u'Info:',image_file.size[0],' ',image_file.size[1],' ',count
tmp = open('tmp.txt','w')
tmp.write(transform1(image_file))
tmp.close()
Ⅱ Python 讀取文件夾將裡面的圖片處理成想要的大小並保存在個指定位置
fromPILimportImage
importos.path
importglob
defconvertjpg(jpgfile,outdir,width=1280,height=720):
img=Image.open(jpgfile)
new_img=img.resize((width,height),Image.BILINEAR)
new_img.save(os.path.join(outdir,os.path.basename(jpgfile)))
forjpgfileinglob.glob("D:/python/*.jpg"):
convertjpg(jpgfile,"D:/newfile")
convertjpg調用時可以有四個參數,如convertjpg(jpgfile,"D:/newfile",800,600)
Image open了jpg用完後要不要close?
Ⅲ python PIL如何才能把圖片修改成正方形或者任意尺寸而不產生擠壓
改變圖像尺寸有兩類方法:
一是縮放(resize),即重采樣。這時,如果圖像縱橫比發生變化就會導致「擠壓」。
二是裁剪(crop)。當然圖只能越裁越小;不過可以配合縮放,先放大再裁剪。
既然題主要求不能「擠壓」,那就只能裁剪了。函數名我給出來了,具體用法題主自己研究。
Ⅳ 請問可以用python實現將大圖片變成小圖片處理嗎,這邊要做一個圖像識別,太大的解析度運行慢
python有一個圖像處理庫——PIL,可以處理圖像文件。PIL提供了功能豐富的方法,比如格式轉換、旋轉、裁剪、改變尺寸、像素處理、圖片合並等等等等,非常強大。
舉個簡單的例子,調整圖片的大小:
12345678910111213141516171819
import Image infile = 'D:\\original_img.jpg'outfile = 'D:\\adjust_img.jpg'im = Image.open(infile)(x,y) = im.size #read image sizex_s = 250 #define standard widthy_s = y * x_s / x #calc height based on standard widthout = im.resize((x_s,y_s),Image.ANTIALIAS) #resize image with high-qualityout.save(outfile) print 'original size: ',x,yprint 'adjust size: ',x_s,y_s '''OUTPUT:original size: 500 358adjust size: 250 179'''
Ⅳ python用graphics中的image圖片保存與調用
from PIL import Image
import os.path
import glob
def convertjpg(jpgfile,outdir,width=1280,height=720):
img=Image.open(jpgfile)
new_img=img.resize((width,height),Image.BILINEAR)
new_img.save(os.path.join(outdir,os.path.basename(jpgfile)))
for jpgfile in glob.glob("D:/python/*.jpg"):
convertjpg(jpgfile,"D:/newfile")
convertjpg調用時可以有四個參數,如convertjpg(jpgfile,"D:/newfile",800,600)
Image open了jpg用完後要不要close?
Ⅵ opencv python 調用resize函數時一直報錯
你可以重新看一下opencv 的文檔,重新理解一下resize函數。resize函數提供了兩種方法來修改圖像的大小,一種是提供一個目標圖像大小(dsize)這和目標大小包含兩個維度:width和heigth。換句話說就是我要告訴resize函數我要將圖片變為dsize這么大/小。另一種方式是通過兩個參數fx,fy,這兩個參數是縮放比例,分別表示對目標圖像的長寬進行縮放的比例。
Ⅶ python可以用來處理圖像嗎
可以的,
PythonWare公司提供了免費的Python圖像處理工具包PIL(Python Image Library),該軟體包提供了基本的圖像處理功能,如:
改變圖像大小,旋轉圖像,圖像格式轉換,色場空間轉換,圖像增強,直方圖處理,插值和濾波等等。雖然在這個軟體包上要實現類似MATLAB中的復雜的圖像處理演算法並不太適合,但是Python的快速開發能力以及面向對象等等諸多特點使得它非常適合用來進行原型開發。
在PIL中,任何一副圖像都是用一個Image對象表示,而這個類由和它同名的模塊導出,因此,最簡單的形式是這樣的:
import Image img = Image.open(「dip.jpg」)
注意:第一行的Image是模塊名;第二行的img是一個Image對象;
Image類是在Image模塊中定義的。關於Image模塊和Image類,切記不要混淆了。現在,我們就可以對img進行各種操作了,所有對img的
操作最終都會反映到到dip.img圖像上。
PIL提供了豐富的功能模塊:Image,ImageDraw,ImageEnhance,ImageFile等等。最常用到的模塊是
Image,ImageDraw,ImageEnhance這三個模塊。下面我對此分別做一介紹。關於其它模塊的使用請參見說明文檔.有關PIL軟體包和
相關的說明文檔可在PythonWare的站點www.Pythonware.com上獲得。
Image模塊:
Image模塊是PIL最基本的模塊,其中導出了Image類,一個Image類實例對象就對應了一副圖像。同時,Image模塊還提供了很多有用的函數。
(1)打開一文件:
import Image img = Image.open(「dip.jpg」)
這將返回一個Image類實例對象,後面的所有的操作都是在img上完成的。
(2)調整文件大小:
import Image img = Image.open("img.jpg") new_img = img.resize
((128,128),Image.BILINEAR) new_img.save("new_img.jpg")
原來的圖像大小是256x256,現在,保存的new_img.jpg的大小是128x128。
就是這么簡單,需要說明的是Image.BILINEAR指定採用雙線性法對像素點插值。
在批處理或者簡單的Python圖像處理任務中,採用Python和PIL(Python Image Library)的組合來完成圖像處理任務是一個很不錯的選擇。設想有一個需要對某個文件夾下的所有圖像將對比度提高2倍的任務。用Python來做將是十分簡單的。當然,我也不得不承認Python在圖像處理方面的功能還比較弱,顯然還不適合用來進行濾波、特徵提取等等一些更為復雜的應用。我個人的觀點是,當你要實現這些「高級」的演算法的時候,好吧,把它交給MATLAB去完成。但是,如果你面對的只是一個通常的不要求很復雜演算法的圖像處理任務,那麼,Python圖像處理應該才是你的最佳搭檔。
Ⅷ python 創建固定大小的圖片
不知道你對圖片的格式有沒有要求,如果是bmp的話是沒有壓縮的。也就是說大小和顏色深度決定了圖片的大小。這樣也比較容易控制。當然也就不能不改size隨便調節文件的大小。
如果是jpg的話,可以通過改變圖片的質量來調節文件的大小。
比如
im = Image.open("aa.JPG")
print im.format, im.size, im.mode
print im.size[0]
im.resize((720,540), Image.ANTIALIAS).save('a.jpg', quality = 95)
你也可以做一個循環,對生成的文件大小與目標大小做比較,直到滿足條件為止。
只是給個思路,也許幫不上忙。
Ⅸ python中PLE調整圖片大小,等比例壓縮文件,怎麼寫代碼
How do I read image data from a URL in Python?
importosimportImagefileName='c:/py/jb51.jpg'fp=open(fileName,'rb')im=Image.open(fp)fp.close()x,y=im.sizeifx <300or y <300:os.remove(fileName)from PIL import Imageimport requestsimport numpy as npfrom StringIO import StringIOresponse = requests.get(url)img = np.array(Image.open(StringIO(response.content)))
from PIL import Imageimport urllib2
im = Image.open(urllib2.urlopen(url))
or if you userequests:
from PIL import Imageimport requests
im = Image.open(requests.get(url, stream=True).raw)
[python] view plain
#coding:utf-8
'''
python圖片處理
'''
importImageasimage
#等比例壓縮圖片
defresizeImg(**args):
args_key={'ori_img':'','dst_img':'','dst_w':'','dst_h':'','save_q':75}
arg={}
forkeyinargs_key:
ifkeyinargs:
arg[key]=args[key]
im=image.open(arg['ori_img'])
ori_w,ori_h=im.size
widthRatio=heightRatio=None
ratio=1
if(ori_wandori_w>arg['dst_w'])or(ori_handori_h>arg['dst_h']):
ifarg['dst_w']andori_w>arg['dst_w']:
widthRatio=float(arg['dst_w'])/ori_w#正確獲取小數的方式
ifarg['dst_h']andori_h>arg['dst_h']:
heightRatio=float(arg['dst_h'])/ori_h
ifwidthRatioandheightRatio:
ifwidthRatio<heightRatio:
ratio=widthRatio
else:
ratio=heightRatio
ifwidthRatioandnotheightRatio:
ratio=widthRatio
ifheightRatioandnotwidthRatio:
ratio=heightRatio
newWidth=int(ori_w*ratio)
newHeight=int(ori_h*ratio)
else:
newWidth=ori_w
newHeight=ori_h
im.resize((newWidth,newHeight),image.ANTIALIAS).save(arg['dst_img'],quality=arg['save_q'])
'''
image.ANTIALIAS還有如下值:
NEAREST:usenearestneighbour
BILINEAR:
BICUBIC:
ANTIALIAS:bestdown-sizingfilter
'''
#裁剪壓縮圖片
defclipResizeImg(**args):
args_key={'ori_img':'','dst_img':'','dst_w':'','dst_h':'','save_q':75}
arg={}
forkeyinargs_key:
ifkeyinargs:
arg[key]=args[key]
im=image.open(arg['ori_img'])
ori_w,ori_h=im.size
dst_scale=float(arg['dst_h'])/arg['dst_w']#目標高寬比
ori_scale=float(ori_h)/ori_w#原高寬比
ifori_scale>=dst_scale:
#過高
width=ori_w
height=int(width*dst_scale)
x=0
y=(ori_h-height)/3
else:
#過寬
height=ori_h
width=int(height*dst_scale)
x=(ori_w-width)/2
y=0
#裁剪
box=(x,y,width+x,height+y)
#這里的參數可以這么認為:從某圖的(x,y)坐標開始截,截到(width+x,height+y)坐標
#所包圍的圖像,crop方法與php中的image方法大為不一樣
newIm=im.crop(box)
im=None
#壓縮
ratio=float(arg['dst_w'])/width
newWidth=int(width*ratio)
newHeight=int(height*ratio)
newIm.resize((newWidth,newHeight),image.ANTIALIAS).save(arg['dst_img'],quality=arg['save_q'])
#水印(這里僅為圖片水印)
defwaterMark(**args):
args_key={'ori_img':'','dst_img':'','mark_img':'','water_opt':''}
arg={}
forkeyinargs_key:
ifkeyinargs:
arg[key]=args[key]
im=image.open(arg['ori_img'])
ori_w,ori_h=im.size
mark_im=image.open(arg['mark_img'])
mark_w,mark_h=mark_im.size
option={'leftup':(0,0),'rightup':(ori_w-mark_w,0),'leftlow':(0,ori_h-mark_h),
'rightlow':(ori_w-mark_w,ori_h-mark_h)
}
im.paste(mark_im,option[arg['water_opt']],mark_im.convert('RGBA'))
im.save(arg['dst_img'])
#Demon
#源圖片
ori_img='D:/tt.jpg'
#水印標
mark_img='D:/mark.png'
#水印位置(右下)
water_opt='rightlow'
#目標圖片
dst_img='D:/python_2.jpg'
#目標圖片大小
dst_w=94
dst_h=94
#保存的圖片質量
save_q=35
#裁剪壓縮
clipResizeImg(ori_img=ori_img,dst_img=dst_img,dst_w=dst_w,dst_h=dst_h,save_q=save_q)
#等比例壓縮
#resizeImg(ori_img=ori_img,dst_img=dst_img,dst_w=dst_w,dst_h=dst_h,save_q=save_q)
#水印
#waterMark(ori_img=ori_img,dst_img=dst_img,mark_img=mark_img,water_opt=water_opt)
[html] view plain