{"@attributes":{"version":"2.0"},"channel":{"title":"Documentation \u2013 Preparing content for recognition","link":"https:\/\/docs.aspose.com\/ocr\/python-net\/ocrinput\/","description":"Recent content in Preparing content for recognition on Documentation","generator":"Hugo -- gohugo.io","lastBuildDate":"Fri, 25 Aug 2023 00:00:00 +0000","item":{"title":"Python: Providing an image as pixel array","link":"https:\/\/docs.aspose.com\/ocr\/python-net\/ocrinput\/image-by-pixel\/","pubDate":"Sun, 27 Aug 2023 00:00:00 +0000","guid":"https:\/\/docs.aspose.com\/ocr\/python-net\/ocrinput\/image-by-pixel\/","description":"\n        \n        \n        <p>Aspose.OCR for Python via .NET allows you to <a href=\"https:\/\/docs.aspose.com\/ocr\/ocr\/python-net\/ocrinput\/#adding-source-content\">provide<\/a> an image for recognition as an array of pixels. This can be useful when working with <a href=\"https:\/\/docs.aspose.com\/ocr\/ocr\/python-net\/supported-file-formats\/\">unsupported<\/a> file formats or with raw bitmap data.<\/p>\n<p>Pixels are listed from left to right (by line), and each line is added to the array from top to bottom.<\/p>\n<p><img src=\"pixel-order.png\" alt=\"Color ordering\"><\/p>\n<h2 id=\"adding-color-image\">Adding color image<\/h2>\n<p>The color image is added as a flat byte array representing the amount of each color per pixel. The amount ranges from 0 to 255, where 0 means no that color and 255 is the maximum amount of that color. For example, 2x2 pixels color bitmap is represented as the following byte array in RGB color model:<\/p>\n<p><img src=\"pixel-order-rgb.png\" alt=\"RGB color array\"><\/p>\n<p>Because the pixel array does not contain an image header, you must directly specify the image&rsquo;s width and height, as well as its color model.<\/p>\n<table>\n<thead>\n<tr>\n<th>Color model<\/th>\n<th style=\"text-align:center\">Bytes per pixel<\/th>\n<th>Pixel color order<\/th>\n<\/tr>\n<\/thead>\n<tbody>\n<tr>\n<td><code>PixelType.RGB<\/code><\/td>\n<td style=\"text-align:center\">3<\/td>\n<td><ol><li>Red<\/li><li>Green<\/li><li>Blue<\/li><\/ol><\/td>\n<\/tr>\n<tr>\n<td><code>PixelType.BGR<\/code><\/td>\n<td style=\"text-align:center\">3<\/td>\n<td><ol><li>Blue<\/li><li>Green<\/li><li>Red<\/li><\/ol><\/td>\n<\/tr>\n<tr>\n<td><code>PixelType.RGBA<\/code><\/td>\n<td style=\"text-align:center\">4<\/td>\n<td><ol><li>Red<\/li><li>Green<\/li><li>Blue<\/li><li>Opacity<\/li><\/ol><\/td>\n<\/tr>\n<\/tbody>\n<\/table>\n<h3 id=\"example\">Example<\/h3>\n<div class=\"highlight\"><pre tabindex=\"0\" class=\"chroma\"><code class=\"language-python\" data-lang=\"python\"><span class=\"line\"><span class=\"cl\"><span class=\"nb\">input<\/span> <span class=\"o\">=<\/span> <span class=\"n\">OcrInput<\/span><span class=\"p\">(<\/span><span class=\"n\">InputType<\/span><span class=\"o\">.<\/span><span class=\"n\">SINGLE_IMAGE<\/span><span class=\"p\">)<\/span>\n<\/span><\/span><span class=\"line\"><span class=\"cl\"><span class=\"n\">pixels<\/span> <span class=\"o\">=<\/span> <span class=\"p\">[<\/span><span class=\"n\">pixel_1_red<\/span><span class=\"p\">,<\/span> <span class=\"n\">pixel_1_green<\/span><span class=\"p\">,<\/span> <span class=\"n\">pixel_1_blue<\/span><span class=\"p\">,<\/span> <span class=\"o\">...<\/span><span class=\"p\">,<\/span> <span class=\"n\">pixel_n_red<\/span><span class=\"p\">,<\/span> <span class=\"n\">pixel_n_green<\/span><span class=\"p\">,<\/span> <span class=\"n\">pixel_n_blue<\/span><span class=\"p\">]<\/span>\n<\/span><\/span><span class=\"line\"><span class=\"cl\"><span class=\"nb\">input<\/span><span class=\"o\">.<\/span><span class=\"n\">add<\/span><span class=\"p\">(<\/span><span class=\"nb\">bytearray<\/span><span class=\"p\">(<\/span><span class=\"n\">pixels<\/span><span class=\"p\">),<\/span> <span class=\"n\">image_width<\/span><span class=\"p\">,<\/span> <span class=\"n\">image_height<\/span><span class=\"p\">,<\/span> <span class=\"n\">PixelType<\/span><span class=\"o\">.<\/span><span class=\"n\">RGB<\/span><span class=\"p\">)<\/span>\n<\/span><\/span><\/code><\/pre><\/div><h2 id=\"adding-grayscale-image\">Adding grayscale image<\/h2>\n<p>The image is provided as a flat byte array, where each byte represents the degree of pixel brightness (one byte per pixel). The number ranges from 0 to 255, where 0 means black and 255 means white.<\/p>\n<p><img src=\"pixel-order-grayscale.png\" alt=\"Grayscale color array\"><\/p>\n<p>Because the pixel array does not contain an image header, you must directly specify the image&rsquo;s width and height, as well as the grayscale (<code>PixelType.BYTE<\/code>) color model.<\/p>\n<h3 id=\"example-1\">Example<\/h3>\n<div class=\"highlight\"><pre tabindex=\"0\" class=\"chroma\"><code class=\"language-python\" data-lang=\"python\"><span class=\"line\"><span class=\"cl\"><span class=\"nb\">input<\/span> <span class=\"o\">=<\/span> <span class=\"n\">OcrInput<\/span><span class=\"p\">(<\/span><span class=\"n\">InputType<\/span><span class=\"o\">.<\/span><span class=\"n\">SINGLE_IMAGE<\/span><span class=\"p\">)<\/span>\n<\/span><\/span><span class=\"line\"><span class=\"cl\"><span class=\"n\">pixels<\/span> <span class=\"o\">=<\/span> <span class=\"p\">[<\/span><span class=\"n\">pixel_1<\/span><span class=\"p\">,<\/span> <span class=\"n\">pixel_2<\/span><span class=\"p\">,<\/span> <span class=\"o\">...<\/span><span class=\"p\">,<\/span> <span class=\"n\">pixel_n<\/span><span class=\"p\">]<\/span>\n<\/span><\/span><span class=\"line\"><span class=\"cl\"><span class=\"nb\">input<\/span><span class=\"o\">.<\/span><span class=\"n\">add<\/span><span class=\"p\">(<\/span><span class=\"nb\">bytearray<\/span><span class=\"p\">(<\/span><span class=\"n\">pixels<\/span><span class=\"p\">),<\/span> <span class=\"n\">image_width<\/span><span class=\"p\">,<\/span> <span class=\"n\">image_height<\/span><span class=\"p\">,<\/span> <span class=\"n\">PixelType<\/span><span class=\"o\">.<\/span><span class=\"n\">BYTE<\/span><span class=\"p\">)<\/span>\n<\/span><\/span><\/code><\/pre><\/div>\n      "}}}