How to save an 2d grey image to png using PIL
To save a 2D grey image to png using PIL in Python, you can follow these steps:
- Import the necessary modules:
from PIL import Image
import numpy as np
- Load the image into a numpy array using the
imreadfunction from theimageiomodule:
img = np.array(imageio.imread('image.png'))
- Convert the numpy array to a PIL Image object:
im = Image.fromarray(img)
- Save the image as a png file using the
savemethod 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')
原文地址: https://www.cveoy.top/t/topic/btoy 著作权归作者所有。请勿转载和采集!