Skip to content

Instantly share code, notes, and snippets.

@k2589
Created December 24, 2019 18:14
Show Gist options
  • Select an option

  • Save k2589/9bf63a20a0c77cb6d5b2f45c66f956e6 to your computer and use it in GitHub Desktop.

Select an option

Save k2589/9bf63a20a0c77cb6d5b2f45c66f956e6 to your computer and use it in GitHub Desktop.
Пример вызова Python скрипта с библиотекой Pillow из 1с
// Здесь соотвественно вызывается данный скрипт с нужными параметрами из переменных, которые были ранее определены
ЗапуститьПриложение(СтрШаблон("python3.7 /app/python/src/pyTxt2Image.py -f %1 -o %2", """" + ИмяФайла + """", ПолучательИД), , Истина);
// и потом нужным образом обрабатывается выходной файл
# -*- coding: utf-8 -*-
import sys, getopt, argparse
from PIL import Image
from PIL import ImageFont
from PIL import ImageDraw
textFile = ""
oFileName = ""
# Парсером можно настроить аргументы которые дальше будешь передавать из 1с
parser = argparse.ArgumentParser(description='Process some integers.')
# У меня например после параметра -f пердается txt файл с нужной информацией для помещения на картинку
parser.add_argument('-f')
# А в аргумент -o пердается имя созраняемой картинки
parser.add_argument('-o')
args = parser.parse_args()
textFile = open(args.f, "r")
oFileName = args.o
def text_wrap(text, font, max_width):
lines = []
lines = []
# If the width of the text is smaller than image width
# we don't need to split it, just add it to the lines array
# and return
if font.getsize(text)[0] <= max_width:
lines.append(text)
else:
# split the line by spaces to get words
words = text.split(' ')
i = 0
# append every word to a line while its width is shorter than image width
while i < len(words):
line = ''
while i < len(words) and font.getsize(line + words[i])[0] <= max_width:
line = line + words[i] + " "
i += 1
if not line:
line = words[i]
i += 1
# when the line gets longer than the max width do not append the word,
# add the line to the lines array
lines.append(line)
return lines
def draw_text_many(textBig, outputFileName):
img = Image.new("RGB", (500,1000), color = (255, 255, 255))
image_size = img.size
font_file_path = '/app/python/src/fonts/Roboto_Mono/RobotoMono-Thin.ttf'
font = ImageFont.truetype(font_file_path, size=23, encoding="unic")
x = 10
y = 10
draw = ImageDraw.Draw(img)
line_height = font.getsize("Пр")[1]
lines = text_wrap(textBig, font, image_size[0])
joined_lines = "\n".join(lines)
draw.text((x,y), joined_lines, fill = (0,0,0), font=font)
img.save("/app/tempFiles/img/" + outputFileName + ".png")
text = textFile.read()
draw_text_many(text, oFileName)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment