如何读取文件并将其转换为 Python 中的二进制图像
How to read the file and convert it to a binary image in Python
我是Python的新手,我想读一张像jpg,png这样的图片。并将其转换为二进制图像。这是我的作品:
from PIL import Image
import numpy
def main( ):
name= 'b.jpg'
img= Image.open (name);
for pixel in iter(img.getdata()):
print(pixel)
img.convert("1").show();
del image;
if __name__=='__main__':
main()
这可能是您的解决方案:
# Read Image
img= Image.open(file_path)
# Convert Image to Numpy as array
img = np.array(img)
# Put threshold to make it binary
binarr = np.where(img>128, 255, 0)
# Covert numpy array back to image
binimg = Image.fromarray(binarr)
我是Python的新手,我想读一张像jpg,png这样的图片。并将其转换为二进制图像。这是我的作品:
from PIL import Image
import numpy
def main( ):
name= 'b.jpg'
img= Image.open (name);
for pixel in iter(img.getdata()):
print(pixel)
img.convert("1").show();
del image;
if __name__=='__main__':
main()
这可能是您的解决方案:
# Read Image
img= Image.open(file_path)
# Convert Image to Numpy as array
img = np.array(img)
# Put threshold to make it binary
binarr = np.where(img>128, 255, 0)
# Covert numpy array back to image
binimg = Image.fromarray(binarr)