{"id":2971,"date":"2026-06-01T17:55:49","date_gmt":"2026-06-01T09:55:49","guid":{"rendered":"http:\/\/www.transgeniclearning.com\/blog\/?p=2971"},"modified":"2026-06-01T17:55:49","modified_gmt":"2026-06-01T09:55:49","slug":"how-to-convert-a-numpy-array-to-an-image-with-pillow-464a-56d17b","status":"publish","type":"post","link":"http:\/\/www.transgeniclearning.com\/blog\/2026\/06\/01\/how-to-convert-a-numpy-array-to-an-image-with-pillow-464a-56d17b\/","title":{"rendered":"How to convert a numpy array to an image with Pillow?"},"content":{"rendered":"<p>Hey there! I&#8217;m a supplier of Pillow, the super &#8211; cool Python library for image processing. Today, I&#8217;m gonna walk you through how to convert a NumPy array to an image using Pillow. <a href=\"https:\/\/www.sidefuchina.com\/bed-linen\/pillow\/\">Pillow<\/a><\/p>\n<p><img decoding=\"async\" src=\"https:\/\/www.sidefuchina.com\/uploads\/201711544\/small\/wholesale-luxury-100-cotton-white-hotel-face52246098985.jpg\"><\/p>\n<p>First off, let&#8217;s understand what we&#8217;re dealing with. NumPy is an awesome Python library for numerical operations. It&#8217;s great for handling multi &#8211; dimensional arrays and matrices. On the other hand, Pillow is a powerful library for working with images in Python. It can open, manipulate, and save many different image file formats.<\/p>\n<p>So, why would you want to convert a NumPy array to an image? Well, there are tons of reasons. Maybe you&#8217;ve been doing some data analysis or machine learning, and you&#8217;ve generated a NumPy array that represents an image. Or perhaps you&#8217;re working on a computer vision project, and you need to visualize the results in an image format.<\/p>\n<h3>Prerequisites<\/h3>\n<p>Before we start, make sure you have both NumPy and Pillow installed. You can install them using <code>pip<\/code>. Just open your terminal and run:<\/p>\n<pre><code>pip install numpy pillow\n<\/code><\/pre>\n<h3>The Basics of NumPy Arrays and Pillow Images<\/h3>\n<p>A NumPy array is a grid of values, all of the same type. For an image, this grid usually represents the pixels. Each element in the array corresponds to a pixel, and the value of that element represents the color of the pixel.<\/p>\n<p>Pillow, on the other hand, uses the <code>Image<\/code> class to represent images. An <code>Image<\/code> object has properties like width, height, and mode (e.g., &#8216;RGB&#8217; for a color image).<\/p>\n<h3>Converting a NumPy Array to a Pillow Image<\/h3>\n<p>Let&#8217;s start with a simple example. Suppose you have a NumPy array that represents a grayscale image. Here&#8217;s how you can convert it to a Pillow image:<\/p>\n<pre><code class=\"language-python\">import numpy as np\nfrom PIL import Image\n\n# Create a simple NumPy array representing a grayscale image\n# Let's make a 100x100 array filled with random values between 0 and 255\nimage_array = np.random.randint(0, 256, (100, 100), dtype=np.uint8)\n\n# Convert the NumPy array to a Pillow image\nimage = Image.fromarray(image_array)\n\n# Save the image\nimage.save('random_grayscale_image.png')\n<\/code><\/pre>\n<p>In this code, we first import the necessary libraries: <code>numpy<\/code> and <code>PIL.Image<\/code>. Then we create a 100&#215;100 NumPy array filled with random integers between 0 and 255. The <code>dtype=np.uint8<\/code> is important because Pillow expects the pixel values to be in the range of 0 &#8211; 255, and <code>uint8<\/code> (unsigned 8 &#8211; bit integer) is the appropriate data type for this.<\/p>\n<p>Next, we use the <code>Image.fromarray()<\/code> method to convert the NumPy array to a Pillow <code>Image<\/code> object. Finally, we save the image as a PNG file.<\/p>\n<h3>Handling Color Images<\/h3>\n<p>If you&#8217;re working with color images, things are a bit more complicated. A color image in RGB format has three channels: red, green, and blue. So, your NumPy array should have three dimensions: height, width, and 3 (for the three color channels).<\/p>\n<p>Here&#8217;s an example:<\/p>\n<pre><code class=\"language-python\">import numpy as np\nfrom PIL import Image\n\n# Create a 100x100 color image array\nimage_array = np.random.randint(0, 256, (100, 100, 3), dtype=np.uint8)\n\n# Convert the NumPy array to a Pillow image\nimage = Image.fromarray(image_array)\n\n# Save the image\nimage.save('random_color_image.png')\n<\/code><\/pre>\n<p>In this case, we create a 100x100x3 NumPy array, where the third dimension represents the RGB channels. Then we convert it to a Pillow image and save it.<\/p>\n<h3>Dealing with Different Data Types<\/h3>\n<p>Sometimes, your NumPy array might have a different data type than <code>uint8<\/code>. For example, you might have floating &#8211; point values. In that case, you need to convert the data type to <code>uint8<\/code> before converting to a Pillow image.<\/p>\n<p>Here&#8217;s how you can do it:<\/p>\n<pre><code class=\"language-python\">import numpy as np\nfrom PIL import Image\n\n# Create a NumPy array with floating - point values\nimage_array = np.random.rand(100, 100)\n\n# Scale the values to the range 0 - 255 and convert to uint8\nscaled_array = (image_array * 255).astype(np.uint8)\n\n# Convert the NumPy array to a Pillow image\nimage = Image.fromarray(scaled_array)\n\n# Save the image\nimage.save('scaled_image.png')\n<\/code><\/pre>\n<p>In this code, we first create a NumPy array with floating &#8211; point values between 0 and 1. Then we scale these values to the range 0 &#8211; 255 and convert the data type to <code>uint8<\/code>. Finally, we convert the array to a Pillow image and save it.<\/p>\n<h3>Advanced Techniques<\/h3>\n<p>If you want to do more complex operations, like resizing the image or changing its color mode, Pillow has a lot of built &#8211; in methods.<\/p>\n<p>For example, let&#8217;s say you want to resize the image after converting it from a NumPy array:<\/p>\n<pre><code class=\"language-python\">import numpy as np\nfrom PIL import Image\n\n# Create a NumPy array\nimage_array = np.random.randint(0, 256, (100, 100), dtype=np.uint8)\n\n# Convert the NumPy array to a Pillow image\nimage = Image.fromarray(image_array)\n\n# Resize the image\nresized_image = image.resize((200, 200))\n\n# Save the resized image\nresized_image.save('resized_image.png')\n<\/code><\/pre>\n<p>In this code, we first convert the NumPy array to a Pillow image. Then we use the <code>resize()<\/code> method to change the size of the image to 200&#215;200 pixels. Finally, we save the resized image.<\/p>\n<h3>Conclusion<\/h3>\n<p><img decoding=\"async\" src=\"https:\/\/www.sidefuchina.com\/uploads\/202111544\/small\/hotel-kimono-collar-100-cotton-terry-bathrobe36317392684.jpg\"><\/p>\n<p>Converting a NumPy array to an image with Pillow is a pretty straightforward process. Whether you&#8217;re working with grayscale or color images, Pillow provides a simple and efficient way to do it.<\/p>\n<p><a href=\"https:\/\/www.sidefuchina.com\/table-linen\/table-cloth\/\">Table Cloth<\/a> As a Pillow supplier, I can offer you high &#8211; quality support and resources for all your image &#8211; processing needs. If you&#8217;re looking to integrate Pillow into your projects, whether it&#8217;s for data visualization, computer vision, or any other application, I&#8217;d love to have a chat with you. Reach out to me to discuss your requirements and let&#8217;s work together to make your projects a success!<\/p>\n<h3>References<\/h3>\n<ul>\n<li>NumPy Documentation<\/li>\n<li>Pillow Documentation<\/li>\n<\/ul>\n<hr>\n<p><a href=\"https:\/\/www.sidefuchina.com\/\">Jiangsu Sidefu Textile Co., Ltd.<\/a><br \/>Jiangsu Sidefu Textile Co., Ltd. is well-known as one of the professional manufacturers and suppliers of various linen products in China, is equipped with hundreds of professional staff and advanced equipment. We have served many five-star hotels and owned good reputation for the quality of our products. Now, welcome to buy or wholesale pillow with our factory.<br \/>Address: No.101 Yongxing Road, Chongchuan Zone, Nantong, Jiangsu, China<br \/>E-mail: info@sidefu-china.com<br \/>WebSite: <a href=\"https:\/\/www.sidefuchina.com\/\">https:\/\/www.sidefuchina.com\/<\/a><\/p>\n","protected":false},"excerpt":{"rendered":"<p>Hey there! I&#8217;m a supplier of Pillow, the super &#8211; cool Python library for image processing. &hellip; <a title=\"How to convert a numpy array to an image with Pillow?\" class=\"hm-read-more\" href=\"http:\/\/www.transgeniclearning.com\/blog\/2026\/06\/01\/how-to-convert-a-numpy-array-to-an-image-with-pillow-464a-56d17b\/\"><span class=\"screen-reader-text\">How to convert a numpy array to an image with Pillow?<\/span>Read more<\/a><\/p>\n","protected":false},"author":303,"featured_media":2971,"comment_status":"closed","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[1],"tags":[2934],"class_list":["post-2971","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-industry","tag-pillow-4138-573698"],"_links":{"self":[{"href":"http:\/\/www.transgeniclearning.com\/blog\/wp-json\/wp\/v2\/posts\/2971","targetHints":{"allow":["GET"]}}],"collection":[{"href":"http:\/\/www.transgeniclearning.com\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"http:\/\/www.transgeniclearning.com\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"http:\/\/www.transgeniclearning.com\/blog\/wp-json\/wp\/v2\/users\/303"}],"replies":[{"embeddable":true,"href":"http:\/\/www.transgeniclearning.com\/blog\/wp-json\/wp\/v2\/comments?post=2971"}],"version-history":[{"count":0,"href":"http:\/\/www.transgeniclearning.com\/blog\/wp-json\/wp\/v2\/posts\/2971\/revisions"}],"wp:featuredmedia":[{"embeddable":true,"href":"http:\/\/www.transgeniclearning.com\/blog\/wp-json\/wp\/v2\/posts\/2971"}],"wp:attachment":[{"href":"http:\/\/www.transgeniclearning.com\/blog\/wp-json\/wp\/v2\/media?parent=2971"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"http:\/\/www.transgeniclearning.com\/blog\/wp-json\/wp\/v2\/categories?post=2971"},{"taxonomy":"post_tag","embeddable":true,"href":"http:\/\/www.transgeniclearning.com\/blog\/wp-json\/wp\/v2\/tags?post=2971"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}