• Home
  • Features
  • Pricing
  • Docs
  • Announcements
  • Sign In

jpwsutton / instax_api / 3811978815

pending completion
3811978815

Pull #30

github

GitHub
Merge bc650e95a into 51db4a494
Pull Request #30: feat: general code improvements and adding github workflow

945 of 945 new or added lines in 15 files covered. (100.0%)

1901 of 2101 relevant lines covered (90.48%)

0.9 hits per line

Source File
Press 'n' to go to next uncovered line, 'b' for previous

0.0
/instax/print.py
1
#!/usr/bin/env python3
2
"""Instax SP Print Script.
×
3

4
Author: James Sutton 2017 - jsutton.co.uk
5

6
This can be used to print an image to a Fujifilm Instax SP-2 printer.
7
Parameters:
8
 - JSON Log File (Default ddmmyy-hhmmss.json)
9
 - Image to print
10
 - Port (Default 8080)
11
 - Host (Default 192.168.0.251)
12

13
"""
14
import argparse
×
15
import datetime
×
16
import sys
×
17

18
from loguru import logger
×
19

20
from instax.instaxImage import InstaxImage
×
21
from instax.sp2 import SP2
×
22
from instax.sp3 import SP3
×
23

24

25
def printPrinterInfo(info):
×
26
    """Log Printer information"""
27
    logger.info("Model: %s" % info["model"])
×
28
    logger.info("Firmware: %s" % info["version"]["firmware"])
×
29
    logger.info("Battery State: %s" % info["battery"])
×
30
    logger.info("Prints Remaining: %d" % info["printCount"])
×
31
    logger.info("Total Lifetime Prints: %d" % info["count"])
×
32
    logger.info("")
×
33

34

35
# https://gist.github.com/vladignatyev/06860ec2040cb497f0f3
36
def printProgress(count, total, status=""):
×
37
    # logger.info(status)
38
    bar_len = 60
×
39
    filled_len = int(round(bar_len * count / float(total)))
×
40
    percents = round(100.0 * count / float(total), 1)
×
41
    bar = "=" * filled_len + "-" * (bar_len - filled_len)
×
42
    sys.stdout.write("[{}] {}{} ...{}\r".format(bar, percents, "%", status))
×
43
    sys.stdout.flush()  # As suggested by Rom Ruben (see: http://stackoverflow.com/questions/3173320/text-progress-bar-in-the-console/27871113#comment50529068_27871113)
×
44

45

46
if __name__ == "__main__":
×
47
    parser = argparse.ArgumentParser()
×
48
    parser.add_argument("-d", "--debug", action="store_true", default=False, help=argparse.SUPPRESS)
×
49
    parser.add_argument("-l", "--log", action="store_true", default=False, help=argparse.SUPPRESS)
×
50
    parser.add_argument("-o", "--host", default="192.168.0.251", help=argparse.SUPPRESS)
×
51
    parser.add_argument("-p", "--port", type=int, default=8080, help=argparse.SUPPRESS)
×
52
    parser.add_argument("-i", "--pin", type=int, default=1111, help="The pin code to use, default: 1111.")
×
53
    parser.add_argument(
×
54
        "-e",
55
        "--preview",
56
        action="store_true",
57
        default=False,
58
        help="Show a preview of the image before it is printed, then exit.",
59
    )
60
    parser.add_argument("-t", "--timeout", type=int, default=10, help=argparse.SUPPRESS)
×
61
    parser.add_argument(
×
62
        "-v",
63
        "--version",
64
        type=int,
65
        default=2,
66
        choices=[1, 2, 3],
67
        help="The version of Instax Printer to use (1, 2 or 3). Default is 2 (SP-2).",
68
    )
69
    parser.add_argument("image", help="The location of the image to print.")
×
70
    args = parser.parse_args()
×
71

72
    if not args.debug:
×
73
        logger.remove()
×
74
        logger.add(sys.stderr, level="INFO")
×
75

76
    # If Not specified, set the log file to a datestamp.
77
    if args.log:
×
78
        logFilename = f"{datetime.datetime.now():%Y-%m-%d.%H-%M-%S.log}"
×
79
        logger.add(logFilename)
×
80

81
    logger.info("--- Instax Printer Python Client ---")
×
82
    logger.info("")
×
83
    myInstax = None
×
84

85
    if args.version == 1:
×
86
        logger.info("Attempting to print to an Instax SP-1 printer.")
×
87
        # TODO - Need to find an SP-2 to test with.
88
    elif args.version == 2:
×
89
        logger.info("Attempting to print to an Instax SP-2 printer.")
×
90
        myInstax = SP2(ip=args.host, port=args.port, pinCode=args.pin, timeout=args.timeout)
×
91
    elif args.version == 3:
×
92
        logger.info("Attempting to print to an Instax SP-3 printer.")
×
93
        # Warning, this does not work in production yet.
94
        myInstax = SP3(ip=args.host, port=args.port, pinCode=args.pin, timeout=args.timeout)
×
95
    else:
96
        logger.error("Invalid Instax printer version given")
×
97
        exit(1)
×
98

99
    if args.preview is True:
×
100
        # Going to preview the image as it will be printed
101
        logger.info(f"Previewing Image: {args.image}")
×
102
        instaxImage = InstaxImage(type=args.version)
×
103
        instaxImage.loadImage(args.image)
×
104
        instaxImage.convertImage()
×
105
        instaxImage.previewImage()
×
106
        logger.info("Preview complete, exiting.")
×
107
        exit(0)
×
108
    else:
109
        # Attempt print
110
        logger.info("Connecting to Printer.")
×
111
        info = myInstax.getPrinterInformation()
×
112
        printPrinterInfo(info)
×
113

114
        logger.info("Printing Image: %s" % args.image)
×
115
        # Initialize The Instax Image
116
        instaxImage = InstaxImage(type=args.version)
×
117
        instaxImage.loadImage(args.image)
×
118
        instaxImage.convertImage()
×
119
        # Save a copy of the converted bitmap
120
        # instaxImage.saveImage("test.bmp")
121
        # Preview the image that is about to print
122
        # instaxImage.previewImage()
123
        encodedImage = instaxImage.encodeImage()
×
124
        myInstax.printPhoto(encodedImage, printProgress)
×
125
        logger.info("Thank you for using instax-print!")
×
126
        logger.info(
×
127
            r"""
128
            \   /\
129
            )  ( ')
130
            (  /  )
131
            \(___)|"""
132
        )
STATUS · Troubleshooting · Open an Issue · Sales · Support · CAREERS · ENTERPRISE · START FREE · SCHEDULE DEMO
ANNOUNCEMENTS · TWITTER · TOS & SLA · Supported CI Services · What's a CI service? · Automated Testing

© 2025 Coveralls, Inc