相关文章推荐
没读研的足球  ·  SciPy ...·  1 月前    · 
粗眉毛的薯片  ·  像素操作 - Web API | MDN·  1 月前    · 
沉稳的脆皮肠  ·  linux: ...·  1 年前    · 

如何将一个图像转换成它的原始像素数据的数组?

1 人不认可

我想创建一个小的逐个像素的图像处理程序,所以我想问问有没有什么东西(最好是Python)可以把.png图像转换成RGB原始像素数据。

例如,一个3px*3px的图像,如 this will output:

  • larger image, each square represents one pixel
  • [(255, 0, 0), (0, 0, 0), (255, 0, 0), (0, 255, 0), (0, 255, 0), (0, 255, 0), (0, 0, 255), (255, 255, 255), (0, 0, 255)]
    

    像素阵列将从左到右,从上到下排列(常规的西方阅读方式)。

    额外的东西(如果你能做到,那也是很好的)。

    如果上面的数组增加了额外的复杂性,也可以用打印出HSV像素数据的列表来代替。

    如果有任何帮助,我们将不胜感激。

    4 个评论
    stackoverflow.com/a/25102495/17201436 ---这个回答是否有帮助?
    你能举出 adds additional complications 和一些数字的例子吗?我不太明白这一点
    这是否回答了你的问题? python - 图像的RGB矩阵
    任何像样的图像处理库都会以原始RGB格式从文件中加载图像到内存中。请阅读有关支持的文件格式和确切的内存存储的相关文件。选择一个支持RGB到HSV转换的库。
    python
    image-processing
    LordoCreations
    LordoCreations
    发布于 2021-11-29
    2 个回答
    f10w
    f10w
    发布于 2021-11-29
    0 人赞同

    你可以用 Pillow (PIL)图书馆。

    from PIL import Image
    import numpy as np
    path = 'image.png'
    image = Image.open(path)
    image = np.asarray(image)
    

    注意,返回的数组长度为4,因为".png "格式支持一个额外的Alpha通道(透明度),所以它是RGBA。 为了摆脱这个额外的通道,你需要先将图像转换为RGB格式。

    from PIL import Image
    import numpy as np
    path = 'image.png'
    image = Image.open(path).convert('RGB')
    image = np.asarray(image)
        
    问题是重复的,应该被标记为重复的。
    manaclan
    manaclan
    发布于 2021-11-29
    0 人赞同

    用opencv python读取你的图像。
    Few things to notice:

  • PIL a little bit more user friendly, but opencv will serve you really good if you do heavy image processing stuff
  • If you are familar with numpy, I would suggest using opencv because behind the scence, opencv image is actually a numpy array
  • pip install opencv-python import cv2 im = cv2.imread("test.png") # Use this to if you read png image