数据类型错误 [英] Data type error

查看:122
本文介绍了数据类型错误的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

运行此代码时,我得到输出:

When I run this code I get the output:

TypeError: an integer is required

我不知道为什么会这样,因为我将这两种数据类型分别设置为uint8和uint64.显然我不太了解数据类型.

I have no idea why this is happening because I set both of the data types to uint8 and uint64 respectively. Evidently I don't understand data types very well.

from PIL import Image

from numpy import random

N = 100

##open an image
im=Image.open('/Users/grahamwarner/Desktop/Experiment/gwarner/labeled_photos/masks/003030.png')

##create a random image
rand_matrix = random.randint(0, 255, (500, 500, 3)).astype('uint8')

rand_image = Image.fromarray(rand_matrix)

##select N random pixels
rand_pix = random.randint(0,499, (N,2)).astype('uint64')

##replace the random values at these pixels with the original values
for ii in range(N):

  rand_image.putpixel(tuple(rand_pix[ii,:]), im.getpixel(tuple(rand_pix[ii,:])))

推荐答案

PIL中的getpixel方法似乎对其输入非常挑剔,并且特别希望int s的元组(与Numpy的uint64类型).以下对我有用:

The getpixel method in PIL seems to be very picky about its input, and specifically wants a tuple of ints (which are not the same as Numpy's uint64 type). The following works for me:

for ii in range(N):
    coordinate = tuple(map(int, rand_pix[ii,:]))
    rand_image.putpixel(coordinate, im.getpixel(coordinate))

这篇关于数据类型错误的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆