简体   繁体   English

为什么 int 被识别为浮点数?

[英]Why an int is recognized as a float?

I have to create an resize algorithm in python using pillow.我必须使用枕头在 python 中创建一个调整大小算法。 Some var are in french.一些 var 是法语。 Here, i add to i at the end of a while (so i is always an int) and x is always an int since it's apart of the range.在这里,我在一段时间的末尾加上 i(所以 i 总是一个 int)并且 x 总是一个 int 因为它是范围的一部分。 a[x] is a tuple. 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()

But, i get this error:但是,我收到此错误:

#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

This does'nt happen for a specific value.对于特定值,这不会发生。 It's totally random这完全是随机的

Someone have an answer?有人有答案吗? Because it seems tricky or just too easy I'm gonna ask to my teacher tomorrow but i don't know if i will be able to solve this.因为这看起来很棘手或太容易了,我明天要问我的老师,但我不知道我是否能够解决这个问题。 Thanks you谢谢

a[x] is not a tuple of ints as requested by the function, but a tuple of floats . a[x]不是函数请求的ints元组,而是floats元组。 Therefore, you have to convert this tuple to integer.因此,您必须将此元组转换为整数。

see here这里

You can map the tuple to integer:您可以将元组映射到整数:

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM