digital image read as an array of integers

杀马特。学长 韩版系。学妹 提交于 2021-01-28 19:52:15

问题


Using Python's PIL module, we can read an digital image into an array of integers,

from PIL import Image
from numpy import array

img = Image.open('x.jpg')
im = array(img)  # im is the array representation of x.jpg

I wonder how does PIL interpret an image as an array? First I tried this

od -tu1 x.jpg

and it indeed gave a sequence of numbers, but how does PIL interpret a color image into a 3D array?

In short, my question is that I want to know how can I get a color image's array representation without using any module like PIL, how could do the job using Python?


回答1:


Well, it depends on the image format I would say.

For a .jpg, there is a complete description of the format that permits to read the image . You can read it here

What PIL does is exactly what you did at first. But then it reads the bytes following the specifications, which allow it to transform this into a human readable format (in this case an array).

It may seem complex for JPEG, but if you take png (the version without compression) everything can seem way more simple. For example this image

png image without compression

If you open it, you will see something like that : Raw dump of previous png image

You can see several information on top that corresponds to the header.

Then you see all those zeroes, that are the numerical representation of black pixels of the image (the top left corner).

If you open the image with PIL, you will get an array that is mostly filled with 0.

If you want to know more about the first bytes, look at the png specifications chapter 11.2.2.

You will see that some of the bytes corresponds to the width and height of the image. This is how PIL is able to create the array :).

Hope this helps !




回答2:


Depends on the color mode. In PIL an image is stored as a list of integers with all channels interleaved on a per-pixel basis.

To illustrate this:

  • Grayscale image: [pixel1, pixel2, pixel3, ...]
  • RGB: [pixel1_R, pixel1_G, pixel1_B, pixel2_R, pixel_2_G, ...]
  • Same goes for RBGA and so on.


来源:https://stackoverflow.com/questions/14319397/digital-image-read-as-an-array-of-integers

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!