3

I'm doing a python program (using python3.6) that dynamically generates a .jpg picture as output (I used Pillow module). How can I send that picture directly to my printer automatically right after I generate it? I tried to look into the os module but I couldn't figure out how to do it.

Michael
  • 6,451
  • 5
  • 31
  • 53
Alberto
  • 35
  • 5

1 Answers1

2

The os module should be able to that on MacOS with

os.system("lpr -P YOUR_PRINTER file_name.jpg")
Nethale
  • 117
  • 4
  • how can I find the proper name of the printer? is there any way to have a list of all printers installed? about the name of the file, can I print any graphic file? (.jpg, .png, .pdf)? the name of the file is refered to the local path where my python program is, right? – Alberto Dec 14 '18 at 10:55
  • I am currently not in reach of a MacBook but the following link describes how you an get your printer name :) [link](https://mediawiki.middlebury.edu/LIS/Connect_To_Network_Printers_-_Mac_OS_X) – Nethale Dec 14 '18 at 10:57
  • thank you! I'm gonna try right now! does this method works for either usb and wifi printer? or just for usb ones? – Alberto Dec 14 '18 at 11:00
  • No problem. It should work for every printer that is connected to your system. So USB/WiFi does not matter. – Nethale Dec 14 '18 at 11:05
  • it worked just fine with my wifi printer, but unfortunately on my laser one (is an old hp laserjet 2420) appeared a message that says there'not enough memory, I don't understand why (the picture is just 560KB). oh and I noticed that it automatically fit the picture for an A4 size. I would prefer to mantain the 100% scale when I print. can I do that in python as well? – Alberto Dec 14 '18 at 11:18
  • I never had to work with a laserprinter before. Sorry. About the printing options see [link](http://www.it.uu.se/datordrift/maskinpark/skrivare/cups/). You would ahve to change them as parameters over the lpr command. Basically what you are looking for should be this: **lpr -o fit-to-page filename** or this **-o scaling=percent** – Nethale Dec 14 '18 at 12:03
  • if you need more control, you'll get far more control/options if you embed the image inside a PDF file (or similar, but generating PDFs from code is pretty easy) first and then print that – Sam Mason Dec 14 '18 at 12:38
  • If the image is being generated by Pillow anyway, you could paste it on top of a white background to change the effective size once printed. – radarhere Dec 14 '18 at 23:22