简体   繁体   English

如何根据像素值在 Python 中绘制像素?

[英]How to paint pixels in Python according to their pixel value?

For example, i want to paint blue all pixels with the value of 90 in a grayscale image with python.例如,我想用 python 将灰度图像中值为 90 的所有像素都涂成蓝色。 How do i do that?我怎么做?

I have no idea on how to do It, i am pretty new to python我不知道该怎么做,我是 python 的新手

from PIL import Image
import requests
import numpy as np
import matplotlib.pyplot as plt

# load an image from a URL
image = Image.open(requests.get('https://upload.wikimedia.org/wikipedia/en/7/7d/Lenna_%28test_image%29.png', stream=True).raw)

# convert to grayscale
grayscale_image = image.convert('L')
grayscale_array = np.array(grayscale_image)

# get a matrix of with all pixels whith a value above 60
values_above = grayscale_array > 60

# Iterate over all the pixels in the image
for i in range(image.width):
    for j in range(image.height):
        # Check the corresponding value in the boolean matrix - if it is True, set the pixel to blue
        if values_above[i][j]:
            # Change the pixel value to the desired value
            image.putpixel((i, j), (0, 0, 255))

# show image
plt.imshow(image)
plt.show()

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

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