Python 如何将文件夹下的所有字节文件转换为图像

Python 如何将文件夹下的所有字节文件转换为图像,python,image,python-2.7,numpy,image-processing,Python,Image,Python 2.7,Numpy,Image Processing,上面的代码一次只能生成一张图片,现在我需要将路径下的所有二进制文件转换为图像,我该怎么做?如果您在一个体面的(即Unix/Linux/macOS)平台上,您可以将所有二进制文件并行转换为PNG图像,而无需编写任何Python,如果您使用安装在大多数Linux发行版上的GNU Parallel和ImageMagick,并且可通过自制为macOS提供 因此,将以.bin结尾的所有文件并行转换为PNG图像的命令如下: import numpy from PIL import Image import

上面的代码一次只能生成一张图片,现在我需要将路径下的所有二进制文件转换为图像,我该怎么做?

如果您在一个体面的(即Unix/Linux/macOS)平台上,您可以将所有二进制文件并行转换为PNG图像,而无需编写任何Python,如果您使用安装在大多数Linux发行版上的GNU ParallelImageMagick,并且可通过自制为macOS提供

因此,将以
.bin
结尾的所有文件并行转换为
PNG
图像的命令如下:

import numpy
from PIL import Image
import binascii

def getMatrixfrom_bin(filename,width):
    with open(filename, 'rb') as f:
        content = f.read()
    ...
    return fh

filename = "path\bin_filename(1)"
im = Image.fromarray(getMatrixfrom_bin(filename,512))
//getMatrixfrom_bin () is a function that generates a matrix from the binary bytes
im.save("path\bin_filename(1).png")
什么是“一些东西”?请注意,
{}
是我们当前正在处理的文件的简写,因此它是这样做的:

parallel 'some stuff' ::: *.bin
parallel 'some stuff' ::: *.bin
s=$(wc -c < {})         # s=total bytes in current file, i.e. s=filesize
w=512                   # w=image width
((h=s/w))               # h=s/w, i.e. h=height in pixels of current file
convert ...
parallel 'convert -depth 8 -size 500x400 gray:{} {.}.png' ::: *bin