我正在从事一个艺术项目,并正在将文本转换成二进制文件 到目前为止,我认为我所做的很难,几乎就在那里...... 除了我不太确定如何解决问题的最后一点:
我有一系列的图像一个接一个地显示但是我需要将图像包起来,使它们被格式化为横向定位在A4(打印纸)的页面上。 我该怎么做呢?
我有将文本转换为二进制文件到图像的基本脚本。
我看到的另一个问题是,当我在整个段落上运行脚本时,会打开太多文件并收到错误(如下所示):
Traceback (most recent call last):
File "ascii_to_binary.py", line 73, in <module>
imgs = [Image.open(i) for i in list_im]
File "/home/odroid/.virtualenvs/cv/local/lib/python2.7/site-packages/PIL/Image.py", line 2410, in open
fp = builtins.open(filename, "rb")
IOError: [Errno 24] Too many open files: 'open_circle.png'
以下是整个脚本供参考:
import numpy as np
import cv2
import imutils
import sys
from PIL import Image
open_circle = 'open_circle.png'
closed_circle = 'closed_circle.png'
diamond = 'diamond.png'
text1 = open("filename.text", "r")
letters = text1.read()
a = len(letters)
binary_text = [None]*a #pre-allocating a list
encoded_bin = ' '.join([bin(ord(letter))[2:].zfill(8) for letter in letters])
b = len(encoded_bin[0:18])
list_im = [0]*b
for val in range(b):
if encoded_bin[val] == '0':
list_im[val] = closed_circle
if encoded_bin[val] == '1':
list_im[val] = open_circle
if encoded_bin[val] == ' ':
list_im[val] = diamond
imgs = [Image.open(i) for i in list_im]
min_shape = sorted( [(np.sum(i.size), i.size) for i in imgs] )[0][1]
imgs_comb = np.hstack( (np.asarray(i.resize(min_shape)) for i in imgs) )
imgs_comb = Image.fromarray(imgs_comb)
imgs_comb.show()