Printing the Tapestry of Game of Thrones on a Fiscal Printer Using Python
Once, in one of the projects, a fiscal printer fell into my hands. Every day we come across these devices when we make payments in stores, but few people realize what they really are. I won’t go into the details of their work, I’ll just say that these are the things that print receipts with purchase data on special thermal paper (yes, almost all fiscal printers have no ink!).
I had to figure out how to get the state of functioning of the fiscal printer and its internal settings. The task has long been completed, and the fiscal printer was abandoned for a long time in the far corner ... Until the idea came to my mind to do some work: D
Detailed steps for printing a tapestry in python under the cat below.
In this article, I will briefly describe the procedure with detailed comments, assuming that the output will be a training article with several useful practical skills:
download the tapestry video from youtube
create a long monochrome tapestry image for printing on a fiscal printer
connect to the fiscal printer
print a tapestry on a fiscal printer
we mount the resulting video clip for publication in social networks
Download the tapestry video from youtube
It is done very simply using the pytube library, you just need to decide on the index of the video stream that you are going to download.
I selected thread with id 18 because it is a small resolution - we still print it on a check tape, and download faster))
Create a long monochrome tapestry image for printing on a fiscal printer
For video processing, we need the well-known OpenCV library and Pillow (a modern fork of PIL) (although here instead of OpenCV we could use the avconv utility from the libav tool, more about it in the last section of this article). Many thanks to the author for writing python or python-opencv , which is a python wheel, installed via PIP and does not require installing OpenCV itself ( hooray! ).
A fiscal printer can only print special images - monochrome bmp files of a fixed width of 528 pixels (but unlimited length, ho-ho-ho!). In addition, the tapestry image in the video clip is constantly moving, so we need to carefully cut the frames so that we get one long picture.
At the output, we get a long picture with the image of the whole tapestry, only a fragment is shown below, the original picture has a width of 55,000 pixels and does not pass according to the publication rules:
But such an image is obtained after monochrome transformations, only without rotation:
We print a tapestry on a fiscal printer
I have a specific fiscal printer of the Atol fprint-22 model at my disposal, but the general rules apply to other fiscal printer models. Moreover, my fiscal book is quite ancient and does not yet support the new-fangled requirements of FZ-54 (I recall that after the entry into force of this law, all fiscal officers were obliged to send data through the OFD to the tax office, which entailed pain and suffering - a flashing of each device).
A small digression about fiscal printers. They relate to POS devices - this is all kinds of peripherals for the needs of trade, which are connected to a PC and integrated into a single accounting and payment system. Of these well-known devices, you have definitely seen barcode scanners and credit card payment terminals. For all of these devices, a unified UnifiedPOS interaction protocol was invented.
In short, this is a separate topic and a very narrow circle of specialists involved in POS devices. The situation is complicated by the fact that most of these devices are designed for operation exclusively under Windows through COM objects - dll files with a very poor documentary description of functionality. Although I heard about the cash systems running under FreeBSD, but I haven’t seen anything like that while working with POS devices, it’s good that I didn’t need a detailed immersion in the world of Retail POS business processes ...
Therefore, the procedure for working with most of these devices is as follows:
driver is installed from the manufacturer
connection to the device through the manufacturer’s utility is configured
looking for the desired registry key with the desired device
looking for the necessary settings for connecting to it
(most work on the RS-232 serial software interface)
connects to the device through the COM object of the manufacturer’s driver
work with the device through the API COM object
COM object and device physical port are released
( important point )
Since I have an ancient fiscal book at my disposal, the driver for it is used exactly the 8th version. Now the manufacturer has added a driver of the 10th version, which greatly simplifies working with a fiscal printer through a separate python wrapper module, which is good news.
The following code demonstrates a function that connects to a fiscal printer using the above-described algorithm, produces a beep, and prints the previously received monochrome tapestry image. We will need to install pywin32 .
The code turned out to be quite long and boring, so I put it under the spoiler:
The output was such a manuscript, a performance on “A Song of Ice and Fire”:
The printer at the end completely howled, printed slowly and dimly, and then completely finished printing only the final scene - no fiscal printer has ever seen such a load: D
It remains to prepare the video and post on the social network. As an audio series, I found an amateur composition on the 8bit network - the title theme of the series . The idea was to superimpose one another without using a video editor at all, I will write about this later in the final part of the article.
We mount the resulting video clip for publication in social networks
cut from the video shot on the smartphone part at the beginning and part at the end
(to fit mp3 file with 8bit music for 3)
write 3 audio file losses to one file
overlay video file and audio file into a new video file
convert video file from mov format to mp4 format
(my smartphone shoots videos with the extension mov
For these purposes, I wrote a script to run on the command line, which can be executed both bash in linux and bat in win (the differences are indicated in the script comments):
PS: my first article on Habré, planned to write a short article for a start, cut as much as I could) I hope that the reading was pleasant, and the result of my work - interesting)