亚洲在线久爱草,狠狠天天香蕉网,天天搞日日干久草,伊人亚洲日本欧美

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

為什么 int 會被識別為 float?

為什么 int 會被識別為 float?

慕仙森 2023-08-08 17:15:06
我必須使用pillow在python中創建一個調整大小算法。有些變量是法語的。在這里,我在一段時間的末尾添加到 i (因此 i 始終是一個 int ),并且 x 始終是一個 int ,因為它超出了范圍。a[x] 是一個元組。from PIL import Imagephoto=Image.open("meh.jpg")from random import randinttaille=photo.sizelargeur=int(taille[0]*facteur)hauteur=int(taille[1]*facteur)diffhauteur=hauteur-taille[1]difflargeur=largeur-taille[0]newImage= Image.new('RGB', (largeur,hauteur))if diffhauteur>1:    i=0    while i !=taille[0]:        a=[]        for b in range(taille[1]):            liste.append(photo.getpixel((i,b))) #get all the data of the pixels in the row        for b in range(diffhauteur): #add some pixel  (gradient) to get the lenght of the new img            index=randint(0,len(a)-2)            pixel2=a[index+1]            pixel1=a[index]            ab=  degrade(pixel1,pixel2) #create a gradient of two pixels as a tuple            a.insert(index,ab)        for x in range (hauteur):#add the row to the new img            newImage.putpixel((i,x),a[x])        i=i+1newImage.show()但是,我收到此錯誤:#here are the valuesa[x]=(0.0, 0.0, -1.0)i=0x=6line 71, in resizing    newImage.putpixel((i,x),a[x]) TypeError: integer argument expected, got float對于特定值,不會發生這種情況。完全是隨機的有人有答案嗎?因為這看起來很棘手或者太簡單了,我明天要問我的老師,但我不知道我是否能夠解決這個問題。感謝您
查看完整描述

1 回答

?
紫衣仙女

TA貢獻1839條經驗 獲得超15個贊

a[x]不是ints函數所要求的 的元組,而是 的元組floats。因此,您必須將此元組轉換為整數。

您可以將元組映射到整數:

for?x?in?range?(hauteur):
????a2?=?tuple(map(int,?a[x]))
????newImage.putpixel((i,x),a2)


查看完整回答
反對 回復 2023-08-08
  • 1 回答
  • 0 關注
  • 147 瀏覽
慕課專欄
更多

添加回答

舉報

0/150
提交
取消
微信客服

購課補貼
聯系客服咨詢優惠詳情

幫助反饋 APP下載

慕課網APP
您的移動學習伙伴

公眾號

掃描二維碼
關注慕課網微信公眾號