python - Why an int is recognized as a float?
问题描述
I have to create an resize algorithm in python using pillow. Some var are in french. 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. a[x] is a tuple.
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
. 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)
推荐阅读
- android - 如何使用android默认文件选择器从SD卡中的下载文件夹中获取文件的文件路径
- c++ - 创建模板对象的指针时出错,“`num` 的值在常量 exp 中不可用”
- kotlin - 如何在 Kotlin 中正确处理/关闭自定义协程调度程序?
- python - 尝试使用 df.columns 获取 Pandas Dataframe 中的标题列表
- html - 如何在我的管理网站上获取我的配置 API 以在我的登录页面中使用?
- r - 根据日期时间和事件创建序列列以获取订单
- web-component - 扩展的 HTML 元素:“是”,包裹一个 HTML 元素,或其他任何东西 - 当前状态是什么?
- google-bigquery - 使用 OVER PARTITION BigQuery 在给定时间之前获取最新记录
- onedrive - 将文件从本地共享复制到共享一个驱动器文件夹
- javascript - 在命名空间存储中调度操作 (Vuex/Nuxt)