Code Yarns ‍👨‍💻
Tech BlogPersonal Blog

How to convert between NumPy array and PIL Image

📅 2014-Jan-16 ⬩ ✍️ Ashwin Nanjappa ⬩ 🏷️ numpy, pil ⬩ 📚 Archive

This example illustrates converting a 3-channel RGB PIL Image to 3D NumPy array and back:

import numpy
import PIL

# Convert PIL Image to NumPy array
img = PIL.Image.open("foo.jpg")
arr = numpy.array(img)

# Convert array to Image
img = PIL.Image.fromarray(arr)

Tried with: Python 2.7.3 and Ubuntu 12.04


© 2022 Ashwin Nanjappa • All writing under CC BY-SA license • 🐘 @codeyarns@hachyderm.io📧