为什么 int 会被识别为 float?

我必须使用pillow在python中创建一个调整大小算法。有些变量是法语的。在这里,我在一段时间的末尾添加到 i (因此 i 始终是一个 int ),并且 x 始终是一个 int ,因为它超出了范围。a[x] 是一个元组。


from PIL import Image

photo=Image.open("meh.jpg")

from random import randint


taille=photo.size

largeur=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+1


newImage.show()

但是,我收到此错误:


#here are the values

a[x]=(0.0, 0.0, -1.0)

i=0

x=6

line 71, in resizing

    newImage.putpixel((i,x),a[x])

 

TypeError: integer argument expected, got float

对于特定值,不会发生这种情况。完全是随机的


有人有答案吗?因为这看起来很棘手或者太简单了,我明天要问我的老师,但我不知道我是否能够解决这个问题。感谢您


慕仙森
浏览 121回答 1
1回答

紫衣仙女

a[x]不是ints函数所要求的 的元组,而是 的元组floats。因此,您必须将此元组转换为整数。您可以将元组映射到整数:for x in range (hauteur):     a2 = tuple(map(int, a[x]))     newImage.putpixel((i,x),a2)
打开App,查看更多内容
随时随地看视频慕课网APP

相关分类

Python