Occlude MNIST dataset images

foreword

  The blogger wants to modify the MNIST image to see if the accuracy of the training model is still high. However, when the image is blocked, the use of the putpixel function keeps reporting an error. Now it has been resolved. Please record this problem.

During the previous operation, the error has been prompted:

 return self.im.putpixel(xy, value)
TypeError: function takes exactly 1 argument (3 given)

The translation means that 3 parameters are given but the function can only extract one.

However, since this code has already run through on the flower data set, the blogger is very puzzled why it doesn't work here. After reading a lot of learning materials, he finally found the answer.

The picture below is a picture overlaid under flower-daisy.

Suddenly I saw an article saying that the image mode needs to be converted, only to realize that the flower data set is color and the MNIST data set is black and white, so I always use red (255, 0, 0) to cover it, is it not feasible?

image.convert() function in PIL in Python image processing

As mentioned in this article, MNIST is a binary image, black is 0, and white is 255. Therefore, I modified the color of the previous coverage code. When changing red (255, 0, 0) to white 255, the code runs through instantly!

The problem that has plagued me for a long time turned out to be the problem of the color mode of the dataset.

Correct code:

for i in [0,1,2]:
    im1 = Image.open(os.path.join(path, x_names[i]))
    arr = np.array(im1)
    h = arr.shape[0]
    print(h)
    w = arr.shape[1]
    print(w)
    for j in range(0, 20):
        for k in range(0, 20):
            data = im1.getpixel((j, k))  # 得到像素值
            im1.putpixel([j, k],255) 
    count += 1
    print(count)
    im1.save(os.path.join(outdir, '{}.png'.format(count)))

The fixed position in the upper left corner is blocked.

 

Guess you like

Origin blog.csdn.net/qq_43604183/article/details/127320384