尝试在python3.6上从python2.7打开一个项目。我在那里遇到了ord()函数的问题。在

使用python2.7可以很好地使用ord(),并且我得到了具有相同图片的输出图像文件。我的意思是,我可以在图片上写下字节的信息。在

但是对于Python3.6,我尝试不调用ord(),我得到的是一行记录信息,而不是我的真实图片,就像Python2.7一样。在from PIL import Image

import sys
textfile = open(sys.argv[1],'rb')
textstring = textfile.read()
textfile.close()
xval = 500
yval = int(len(textstring)/(xval*4) + 1)
im = Image.new('RGBA', (xval,yval), (0,0,0,0))
width = xval
height = yval
count = 0
for y in range (0,height):
for x in range (0,width):
if count == len(textstring) - 4:
im.putpixel((x,y),(ord(textstring[count]),ord(textstring[count + 1]),ord(textstring[count + 2]),ord(textstring[count + 3])))
break
if count == len(textstring) - 3:
im.putpixel((x,y),(ord(textstring[count]),ord(textstring[count + 1]),ord(textstring[count + 2]),0))
break
if count == len(textstring) - 2:
im.putpixel((x,y),(ord(textstring[count]),ord(textstring[count + 1]),0,0))
break
if count == len(textstring) - 1:
im.putpixel((x,y),(ord(textstring[count]),0,0,0))
break
im.putpixel((x,y),(ord(textstring[count]),ord(textstring[count + 1]),ord(textstring[count + 2]),ord(textstring[count + 3])))
count += 4
im.save(sys.argv[2])
在python3中,我使用ord()得到一个错误:TypeError: ord() expected string of length 1, but int found
im.putpixel((x,y),(ord(textstring[count]),ord(textstring[count + 1]),ord(textstring[count + 2]),ord(textstring[count + 3])))

我试图添加b而不是ord(),但没有成功。在

要运行程序,需要添加参数:python encryption.py inputfile image.png

你能给我一些建议吗?我该怎么修理它?谢谢。在