¿Necesitas generar un tiff desde texto?, con Python y PIL es sencillo.

En el siguiente ejemplo crearemos la función txt2img que genera un tiff a partir de un archivo ascii.

El código se encarga de abrir el txt y leer todas las líneas para posteriormente escribir/dibujar linea a linea en una imagen.

#!/usr/bin/env python3
# -*- coding: utf-8 -*-
"""
Created on Thu Sep 11 06:23:46 2019
@author: altaruru
"""
from PIL import Image, ImageSequence, ImageFont, ImageDraw, ImageOps
import os

def txt2img(txtfile, tiffile):
	with open(txtfile, encoding='windows-1255') as stxt:
		slines = tuple(line.rstrip() for line in stxt.readlines())

	font = ImageFont.load_default()	
	teststr = 'abcefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ'	
	pt2px = lambda pt: int(round(pt * 96.0 / 72))
	
	max_width_line = max(slines, key=lambda s: font.getsize(s)[0])
	max_width_font = pt2px(font.getsize(max_width_line)[0])
	max_height_font = pt2px(font.getsize(teststr)[1])	
	height = max_height_font * len(slines)
	width = int(round(max_width_font + 10))
	linespacing = int(round(max_height_font * 1.0))
	x = 10
	y = 10	
	image = Image.new('L', (width, height), color=255)
	draw = ImageDraw.Draw(image)	
	
	for sline in slines:
		draw.text((x, y), sline, font=font)
		y += max_height_font
		
	image.save(tiffile)
	image.show()	
	return


txt2img('txt2tif.py', 'test1.tiff')

Para la prueba crea un archivo de texto de nombre txt2tif.py con tu editor de texto favorito y pega el código anterior.

Ejecuta desde Terminal con:

python3 txt2tif.py

El programa crea un archivo tiff con el texto origen.

Obtendrás una salida similar a esta:

Si quieres profundizar en el uso de Pillow te recomiendo eches un vistazo a https://pillow.readthedocs.io/en/3.0.x/index.html

Dudas y comentarios aquí mismo.

Saludos!

Deja tu comentario