0

As there is a way of getting array information as a PIL Image by Image.fromarray() from array data.

I was wondering if there is a way of getting nx data into an image.

I looked into the documentation for PIL Image but I could not figure out what could be useful in this case. I wanted to do with nx.draw() but returns a NoneType.

Edit: To be specific, I am looking for a way without saving it.

Santosh
  • 9
  • 1
  • 3

1 Answers1

1

Use nx.draw to create a matplotlib figure to create an image from using Pillow. You can use io.BytesIO as an intermediate similar to this:

import matplotlib.pyplot as plt
import networkx as nx
import io
from PIL import Image

im_io = io.BytesIO()
fig, ax = plt.subplots()
...
nx.draw(G, ax=ax, ...)
fig.savefig(im_io, format='png')
im = Image.open(im_io)
busybear
  • 10,194
  • 1
  • 25
  • 42
  • Is there a way to achieve this without having to save the figure? Like we get directly from an array using `Image.fromarray()`. – Santosh Apr 12 '20 at 01:31
  • Technically this doesn't create a file. What array would you be creating the image from? – busybear Apr 12 '20 at 03:04
  • I do not have an array. I have an nx graph that I would like to be formed into an image so that I can feed it to a CNN. – Santosh Apr 12 '20 at 20:15