简体   繁体   English

尝试使用Python(和PIL)进行打印

[英]Trying to Print With Python (and PIL)

I'm trying to send an image to a printer to print using a Python script. 我正在尝试将图像发送到打印机以使用Python脚本进行打印。 I'm not overly experienced in the language and took a few tips from a few other people, and I'm currently having an issue in that I keep getting an error saying a file in PIL is missing. 我对这种语言没有过多的经验,并从其他人那里获得了一些提示,而我目前遇到的一个问题是,我不断收到错误消息,说缺少PIL文件。 Here's my code: 这是我的代码:

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')

The error I keep getting is: 我不断得到的错误是:

IOError: [Errno 22] invalid mode ('rb') or filename:'<PIL.JpegImagePlugin.JpegImageFile 
image mode=RGB size=4032x2268 at 0x30C8D50>'

Does anyone know what's going on, or if not, a different way of accessing the photo without using PIL? 有谁知道发生了什么事,或者是否知道不使用PIL来访问照片的另一种方式? Thanks. 谢谢。

i think the issue is that you are opening the image with PIL here: 我认为问题在于您正在此处使用PIL打开图像:
card_pic = Image.open("/home/nao/recordings/cameras/bcard.jpg")
than trying open the file here: 而不是尝试在此处打开文件:
f = open(str(card_pic), 'rb') #open in binary
but str(card_pic) is trying to turn the PIL image object into a string, it does not give you back the filename. 但是str(card_pic)试图将PIL图像对象转换为字符串,但它没有给您返回文件名。 try this line instead: 试试这行:
f = open("/home/nao/recordings/cameras/bcard.jpg", 'rb')

If you want to read the content of a file then you just pass the filename. 如果要读取文件的内容,则只需传递文件名。 Instead you're loading it into a PIL Image and then passing the image to the file open() function which doesn't make any kind of sense. 相反,您将其加载到PIL Image ,然后将该映像传递给没有任何意义的文件open()函数。

Try: 尝试:

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.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM