How to load PNG images with Pil

Source: Internet
Author: User

A png 16-bit grayscale image needs to be processed at work, because the processing program has been written in Python and Pil many days ago, but some parameters are hard to remember because it takes too long. When parameters are added together with the above parameters, you can see that errors are always reported when you read and copy PNG files during the running process. After searching, I quickly found out that the original file format was not suitable. I used a 16-bit grayscale image and quickly went to the Pil document to check it, the file formats and types supported by PIL in this document are not 16 bits, and there is no suitable conversion format:

From "Python imaging library overview, pil1.1.3"

When I was in a hurry, I suddenly remembered that Pil is also open-source. Hurry to the installation directory and check it out. I soon found the parsing code (pngimageplugin. py) of the PNG file in it. The definition of the file format is much richer than that in the document:
_ Modes = {
# Supported bits/color combinations, and corresponding modes/rawmodes
(1, 0): ("1", "1 "),
(2, 0): ("L", "l; 2 "),
(4, 0): ("L", "l; 4 "),
(8, 0): ("L", "L "),
(16, 0): ("I", "I; 16B "),
(8, 2): ("RGB", "RGB "),
(16, 2): ("RGB", "RGB; 16B "),
(1, 3): ("P", "P; 1 "),
(2, 3): ("P", "P; 2 "),
(4, 3): ("P", "P; 4 "),
(8, 3): ("P", "P "),
(8, 4): ("La", "La "),
(16, 4): ("rgba", "La; 16B"), # La; 16B-> La not yet available
(8, 6): ("rgba", "rgba "),
(16, 6): ("rgba", "rgba; 16B "),
}
It not only supports 16-bit grayscale images, but also supports rgb16-bit images. It is estimated that HDR is enough for Continuous Cropping. Haha. It is also very simple to use. Reading a file can be automatically identified. When creating a file, you only need to declare its mode as "I; 16B.

Note that in the 16-bit grayscale image processed by Pil, the image data must be saved as the background of the image rather than other layers, otherwise, Pil reads and processes the graph in rgb8 format.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.