SystemError:新しいスタイルのgetargs形式ですが、引数はタプルではありません[putpixel(xy、value)に関連]



Systemerror New Style Getargs Format Argument Is Not Tuple Related Putpixel Xy



今日、私は中国の大学moocで勉強しています Python機械学習アプリケーション(Lixin、Songtian) 顔画像の特徴抽出過程で次元削減を行ったところ、小さな問題が発生しましたが、検索してみたところ、インターネット上でこの問題に対処する方法はほとんどありませんでした。これについて少しお話しします。
まず、使用する必要のある写真は次のとおりです。
画像

import numpy as np import PIL.Image as image from sklearn.cluster import KMeans def loadData(filePath): f = open(filePath,'rb') # Open the file in binary form data = [] img = image.open(f) # Return image pixel value in list form m,n = img.size for i in range(m): # Process each pixel RGB color to 0-1 for j in range(n): # Store in data in the range x,y,z = img.getpixel((i,j)) # Get the value of a certain pixel position data.append([x/256,y/256,z/256]) f.close() return np.mat(data),m,n #Return data in matrix form, and the size of the picture imgData,row,col = loadData('bull.jpg') label = KMeans(n_clusters=4).fit_predict(imgData) label = label.reshape([row,col]) pic_new = image.new('L', (row, col)) for i in range(row): for j in range(col): pic_new.putpixel((i,j),256 / (label[i][j] + 1)) pic_new.save('result-bull-4.jpg', 'JPEG')

上記のコードの実行に問題があります。



ファイル「…lib site-packages PIL Image.py」、1641行目、putpixel
self.imを返します。putpixel(xy, value)


SystemError: new style getargs format but argument is not a tuple

問題の説明は次のとおりです。



pic_new.putpixel((i、j)、256 /(label [i] [j] +1))

解決策は、256 /(label [i] [j] +1)をint型に変換することです。

pic_new.putpixel((i、j)、int(256 /(label [i] [j] +1)))



変更後、上記の正しいコードを実行して以下を取得します。
画像