當前位置:首頁 » 編程語言 » pythonurl圖片大小

pythonurl圖片大小

發布時間: 2022-05-19 04:56:41

1. python如何獲取圖片長寬等信息

使用PIL模塊,windows安裝包下載:

http://www.pythonware.com/procts/pil/

使用方法

#coding:utf8
#獲取指定圖片的長和寬
fromPILimportImage
img=Image.open("img.jpg")
printimg.size

運行結果:
(52,54)

2. 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?

3. 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)
你也可以做一個循環,對生成的文件大小與目標大小做比較,直到滿足條件為止。
只是給個思路,也許幫不上忙。

4. 有網路圖片的url怎麼獲取圖片大小和長寬

右鍵圖片,另存為,保存下來。再右鍵下載好的圖片,選擇屬性,就可以看到了

5. 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

  • [html] 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)



6. python opencv中imshow輸出圖像太大,如何調整輸出尺寸

通過resize重置圖片尺寸
參數:圖片,輸出圖片尺寸(一般我不用這個,我傳None),寬的比例,高的比例
例子:將圖片img,縮小一半顯示,那麼寬高為原尺寸的0.5倍
img = cv2.resize(img,None,fx=0.5,fy=0.5)

設置完尺寸後再顯示
cv2.imshow('img',img)
cv2.waitKey(0)
.......
希望我的回答對你有幫助!

7. 通過url地址改變圖片大小

很簡單
你就是想通過url來改變同一張圖片的大小
假如圖片:章001.png;初始像素10*10px;
章001.png;初始像素10*10px;
章002.png;初始像素20*20px;
章003.png;初始像素30*30px;
多創建幾個像素不同內容
相同的圖片
就可以來
地址/!/both/長x寬使用這種地址請求根據你使用什麼框架來決定的
spring
mvc就可以實現
地址/!/both/長x寬/(restful風格)

8. 如何用Python方法獲取圖片的准確尺寸

先安裝pillow庫
然後運行:
from PIL import Image
img = Image.open('圖片路徑')
print(img.size)
返回的是圖片的 高×寬

9. python如何固定圖片像素的大小

你是想固定圖片的大小的話,可以利用opencv讀取圖片後,resize到你想要的大小,再保存到本地。

10. python 如何將大量圖片的url保存到本地

你如果要保存圖片的url,直接把imgsrc寫入本地文件就可以了,urllib.request.urlretrieve(imgsrc)這個的意思是你要保存的不是圖片的url,而是要把圖片下載下來,這個是要批量爬取網站上的圖片,需要考慮網站的反爬蟲措施了。

熱點內容
我的世界哪五個伺服器被炸了 發布:2025-05-15 10:36:16 瀏覽:993
ehcache存儲對象 發布:2025-05-15 10:35:31 瀏覽:527
搭建虛擬電腦的伺服器 發布:2025-05-15 10:29:31 瀏覽:269
湖人雙核配置哪個最好 發布:2025-05-15 10:09:48 瀏覽:979
手機熱點密碼怎麼查看 發布:2025-05-15 09:54:47 瀏覽:108
生意發力雲存儲 發布:2025-05-15 09:54:45 瀏覽:616
編寫一個shell腳本添加用戶 發布:2025-05-15 09:54:43 瀏覽:505
資料庫查看錶命令 發布:2025-05-15 09:52:27 瀏覽:914
p30是不是自帶方舟編譯器 發布:2025-05-15 09:51:48 瀏覽:599
追擊世界房間密碼是多少 發布:2025-05-15 09:51:46 瀏覽:995