Skip to content

Commit

Permalink
Merge pull request #5 from geftactics/cut-control
Browse files Browse the repository at this point in the history
Adding cut control. Closes #4
  • Loading branch information
geftactics authored Nov 29, 2021
2 parents c18693f + f8e454e commit 170f89a
Show file tree
Hide file tree
Showing 5 changed files with 26 additions and 8 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
.idea/
*.pyc
*.bin
.pypirc
*.egg-info/
dist/
5 changes: 3 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ Print a file from disk:
```
import StarTSPImage
raster = StarTSPImage.imageFileToRaster('file.bmp')
raster = StarTSPImage.imageFileToRaster('file.bmp', cut=True))
printer = open('/dev/usb/lp0', 'wb')
printer.write(raster)
Expand All @@ -32,8 +32,9 @@ from PIL import Image, ImageDraw
image = Image.new('RGB', (500, 500), color='White')
draw = ImageDraw.Draw(image)
draw.ellipse((0, 0, 500, 500), fill='Black')
draw.ellipse((10, 10, 490, 490), fill='White')
raster = StarTSPImage.imageToRaster(image)
raster = StarTSPImage.imageToRaster(image, cut=True)
printer = open('/dev/usb/lp0', "wb")
printer.write(raster)
Expand Down
14 changes: 9 additions & 5 deletions StarTSPImage/StarTSPImage.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
from PIL import Image


def buildRaster(img):
def buildRaster(img, cut=True):

bytes_per_line = 72

Expand All @@ -21,6 +21,10 @@ def buildRaster(img):
buf.extend([0x1b, ord('*'), ord('r'), ord('A')]) # Enter raster mode
buf.extend([0x1b, ord('*'), ord('r'), ord('P'), ord('0'), 0x00]) # continuous mode

# Handle cuts
if not cut:
buf.extend([0x1b, ord('*'), ord('r'), ord('E'), ord('1'), 0x00]) # Raster EOT no-cut

# Loop over bytes array, adding a transfer data command for each line
# followed by the amount of bytes that make up a line
byte = 0
Expand All @@ -36,10 +40,10 @@ def buildRaster(img):
return bytearray(buf)


def imageToRaster(img):
return buildRaster(img)
def imageToRaster(img, cut=True):
return buildRaster(img, cut)


def imageFileToRaster(image_path):
def imageFileToRaster(image_path, cut=True):
img = Image.open(image_path)
return buildRaster(img)
return buildRaster(img, cut)
2 changes: 1 addition & 1 deletion setup.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
from setuptools import setup, find_packages

version = '0.2.5'
version = '0.2.6'

setup(
name='StarTSPImage',
Expand Down
12 changes: 12 additions & 0 deletions test.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
import StarTSPImage
from PIL import Image, ImageDraw

image = Image.new('RGB', (500, 500), color='White')
draw = ImageDraw.Draw(image)
draw.ellipse((0, 0, 500, 500), fill='Black')
draw.ellipse((10, 10, 490, 490), fill='White')

raster = StarTSPImage.imageToRaster(image, cut=True)

printer = open('/dev/usb/lp0', "wb")
printer.write(raster)

0 comments on commit 170f89a

Please sign in to comment.