[SOLVED] How to print pil image python (hardcopy)?

Issue

This Content is from Stack Overflow. Question asked by Muhammad Huzaifa

from PIL import Image, ImageDraw, ImageFont
import os
img = Image.new('RGB', (100, 30), color = (73, 109, 137))

#fnt = ImageFont.truetype('/Library/Fonts/Arial.ttf', 15)
d = ImageDraw.Draw(img)
d.text((10,10), "Hello world", fill=(255, 255, 0))
#img.save('pil_text_font.png')

I want to print this image on paper. How can I do that?
I tried,

os.startfile(img,'print')

Error:

TypeError: startfile: filepath should be string, bytes or os.PathLike, not Image



Solution

Save your image to a file then output to screen like so:

from PIL import Image, ImageDraw, ImageFont
img = Image.new('RGB', (100, 30), color = (73, 109, 137))

#fnt = ImageFont.truetype('/Library/Fonts/Arial.ttf', 15)
d = ImageDraw.Draw(img)
d.text((10,10), "Hello world", fill=(255, 255, 0))
img.save('pil_text_font.png')

# open method used to open different extension image file
im = Image.open(r"pil_text_font.png") 
  
# This method will show image in any image viewer 
im.show() 

#delete the file immediately after to save space
os.remove("pil_text_font.png")

Here is a link on where I got my information from for this answer: https://www.geeksforgeeks.org/python-pil-image-open-method/


This Question was asked in StackOverflow by Muhammad Huzaifa and Answered by PythonKiddieScripterX It is licensed under the terms of CC BY-SA 2.5. - CC BY-SA 3.0. - CC BY-SA 4.0.

people found this article helpful. What about you?