[英]Trying to Print With Python (and PIL)
我正在嘗試將圖像發送到打印機以使用Python腳本進行打印。 我對這種語言沒有過多的經驗,並從其他人那里獲得了一些提示,而我目前遇到的一個問題是,我不斷收到錯誤消息,說缺少PIL文件。 這是我的代碼:
from PIL import Image
from PIL.ExifTags import TAGS
import socket
import sys
from threading import Thread
def print_bcard(HOST):
print 'Printing business card'
card_pic = Image.open("/home/nao/recordings/cameras/bcard.jpg")
HOST = '192.168.0.38'
PORT = 9100
s = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
s.connect((HOST, PORT))
f = open(str(card_pic), 'rb') #open in binary
l = f.read(1024)
while (l):
s.send(l)
l = f.read(1024)
f.close()
s.close()
print_bcard('192.168.0.38')
我不斷得到的錯誤是:
IOError: [Errno 22] invalid mode ('rb') or filename:'<PIL.JpegImagePlugin.JpegImageFile
image mode=RGB size=4032x2268 at 0x30C8D50>'
有誰知道發生了什么事,或者是否知道不使用PIL來訪問照片的另一種方式? 謝謝。
我認為問題在於您正在此處使用PIL打開圖像:
card_pic = Image.open("/home/nao/recordings/cameras/bcard.jpg")
而不是嘗試在此處打開文件:
f = open(str(card_pic), 'rb') #open in binary
但是str(card_pic)
試圖將PIL圖像對象轉換為字符串,但它沒有給您返回文件名。 試試這行:
f = open("/home/nao/recordings/cameras/bcard.jpg", 'rb')
如果要讀取文件的內容,則只需傳遞文件名。 相反,您將其加載到PIL Image
,然后將該映像傳遞給沒有任何意義的文件open()
函數。
嘗試:
with open("/home/nao/recordings/cameras/bcard.jpg", 'rb') as f:
l = f.read(1024)
while (l):
s.send(l)
l = f.read(1024)
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.