第七色在线视频,2021少妇久久久久久久久久,亚洲欧洲精品成人久久av18,亚洲国产精品特色大片观看完整版,孙宇晨将参加特朗普的晚宴

為了賬號安全,請及時綁定郵箱和手機立即綁定
已解決430363個問題,去搜搜看,總會有你想問的

使用Python水平組合多個圖像

使用Python水平組合多個圖像

SMILET 2019-09-20 15:31:50
我試圖在Python中水平組合一些JPEG圖像。問題我有3個圖像 - 每個是148 x 95 - 見附件。我只是制作了3張相同的圖像 - 這就是為什么它們是相同的。我的嘗試我正在嘗試使用以下代碼水平加入它們:import sysfrom PIL import Imagelist_im = ['Test1.jpg','Test2.jpg','Test3.jpg']new_im = Image.new('RGB', (444,95)) #creates a new empty image, RGB mode, and size 444 by 95for elem in list_im:    for i in xrange(0,444,95):        im=Image.open(elem)        new_im.paste(im, (i,0))new_im.save('test.jpg')但是,這會產(chǎn)生附加的輸出test.jpg。題有沒有辦法水平連接這些圖像,使test.jpg中的子圖像沒有顯示額外的部分圖像?附加信息我正在尋找一種水平連接n個圖像的方法。我想一般使用這個代碼所以我更愿意:如果可能的話,不要硬編碼圖像尺寸在一行中指定尺寸,以便可以輕松更改它們
查看完整描述

3 回答

?
繁星點點滴滴

TA貢獻1803條經(jīng)驗 獲得超3個贊

我會試試這個:


import numpy as np

import PIL


list_im = ['Test1.jpg', 'Test2.jpg', 'Test3.jpg']

imgs    = [ PIL.Image.open(i) for i in list_im ]

# pick the image which is the smallest, and resize the others to match it (can be arbitrary image shape here)

min_shape = sorted( [(np.sum(i.size), i.size ) for i in imgs])[0][1]

imgs_comb = np.hstack( (np.asarray( i.resize(min_shape) ) for i in imgs ) )


# save that beautiful picture

imgs_comb = PIL.Image.fromarray( imgs_comb)

imgs_comb.save( 'Trifecta.jpg' )    


# for a vertical stacking it is simple: use vstack

imgs_comb = np.vstack( (np.asarray( i.resize(min_shape) ) for i in imgs ) )

imgs_comb = PIL.Image.fromarray( imgs_comb)

imgs_comb.save( 'Trifecta_vertical.jpg' )

只要所有圖像具有相同的種類(所有RGB,所有RGBA或所有灰度),它都應該工作。確定這是多行代碼的情況應該不難。這是我的示例圖像,結(jié)果如下:


Test1.jpg

http://img1.sycdn.imooc.com//5d8481170001651c04990743.jpg

Test2.jpg

http://img1.sycdn.imooc.com//5d8481180001602501880268.jpg

Test3.jpg

http://img1.sycdn.imooc.com//5d84811b00014f0002140318.jpg

Trifecta.jpg:

http://img1.sycdn.imooc.com//5d84811d0001639605640268.jpg

Trifecta_vertical.jpg

http://img1.sycdn.imooc.com//5d8481200001052c01880804.jpg

查看完整回答
反對 回復 2019-09-20
  • 3 回答
  • 0 關注
  • 750 瀏覽
慕課專欄
更多

添加回答

舉報

0/150
提交
取消
微信客服

購課補貼
聯(lián)系客服咨詢優(yōu)惠詳情

幫助反饋 APP下載

慕課網(wǎng)APP
您的移動學習伙伴

公眾號

掃描二維碼
關注慕課網(wǎng)微信公眾號