To save a 2D grey image to png using PIL in Python, you can follow these steps:

  1. Import the necessary modules:
from PIL import Image
import numpy as np
  1. Load the image into a numpy array using the imread function from the imageio module:
img = np.array(imageio.imread('image.png'))
  1. Convert the numpy array to a PIL Image object:
im = Image.fromarray(img)
  1. Save the image as a png file using the save method of the PIL Image object:
im.save('output.png')

Here's the complete code:

from PIL import Image
import numpy as np
import imageio

# Load image into numpy array
img = np.array(imageio.imread('image.png'))

# Convert numpy array to PIL Image object
im = Image.fromarray(img)

# Save image as png
im.save('output.png')
How to save an 2d grey image to png using PIL

原文地址: https://www.cveoy.top/t/topic/btoy 著作权归作者所有。请勿转载和采集!

免费AI点我,无需注册和登录