This repository was archived by the owner on May 14, 2021. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathprinter.py
More file actions
74 lines (54 loc) · 1.45 KB
/
printer.py
File metadata and controls
74 lines (54 loc) · 1.45 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
import platform
if 'edison' in platform.platform():
edison=True
print("I'm running on a edison")
else:
edison=False
print("I'm not running on a edison")
if edison==True:
import mraa
import urllib
from urlparse import urlparse
from os.path import splitext
import sys
sys.path.insert(0, './Python-Thermal-Printer-master')
from Adafruit_Thermal import *
from PIL import Image
from PIL import ImageFile
ImageFile.LOAD_TRUNCATED_IMAGES = False
#open the serial port on TX RX on the edison
if edison==True:
x=mraa.Uart(0)
#Instantiate the pronter
if edison==True:
printer = Adafruit_Thermal("/dev/ttyMFD1", 19200, timeout=5)
def printImageByUrl (url):
urlpath=urlparse(url)
root, ext = splitext(urlpath.path)
print root
print ext
filename='./gfx/printfile'+ext
urllib.urlretrieve(url, filename)
#here I downscale the Image
baseWidth = 384
# Open the image file.
try:
im = Image.open(filename)
except:
print "Unable to load image"
# Calculate the height using the same aspect ratio
widthPercent = (baseWidth / float(im.size[0]))
height = int((float(im.size[1]) * float(widthPercent)))
size = (baseWidth, height)
try:
im.load()
except IOError:
pass # You can always log it to logger
newName="./gfx/printfile_scaled"+ext
#here I really scale the image
im.thumbnail(size,Image.ANTIALIAS)
im.save(newName)
#print the image on the printer
printer.printImage(Image.open(newName))
#add some empty lines
printer.feed(7)