Example notebook for random text generation

This commit is contained in:
Emilio Martínez 2023-12-26 16:58:04 -03:00
parent 31bb28676c
commit eaafa5e291
1 changed files with 195 additions and 0 deletions

View File

@ -0,0 +1,195 @@
{
"cells": [
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"from PIL import Image, ImageDraw, ImageFont\n",
"import random\n",
"import string\n",
"from matplotlib import font_manager\n",
"\n",
"def generar_imagen_con_palabras_rndfont(texto, img_shape, tamano_letra, color_texto, fondo_color, ruta_guardado):\n",
" # Create white plain image\n",
" imagen = Image.new(\"RGB\", img_shape, fondo_color)\n",
" dibujo = ImageDraw.Draw(imagen)\n",
"\n",
" # Compute amount of lines depending on image shape and number of characters\n",
" N_total = len(texto)\n",
" N_lines = N_total//img_shape[1]\n",
" N_horizontal = img_shape[0]//(tamano_letra)\n",
"\n",
" # Get system font types\n",
" system_fonts = font_manager.get_fontconfig_fonts()\n",
" # Filter out some non-readable fonts\n",
" ttf_fonts = [font for font in system_fonts if ((\".ttf\" in font) and (\"lohit\" not in font) and (\"kacst\" not in font)) and (\"Navilu\" not in font) and (\"telu\" not in font) and (\"lyx\" not in font) and (\"malayalam\" not in font) and (\"tlwg\" not in font) and (\"samyak\" not in font) and (\"droid\" not in font) and (\"kalapi\" not in font) and (\"openoffice\" not in font) and (\"orya\" not in font)]\n",
"\n",
" # Write over image one font per line\n",
" for iter in range(N_lines):\n",
" rnd_font_index = random.randint(0,len(ttf_fonts)-1)\n",
" random_font = ttf_fonts[rnd_font_index]\n",
" # print(f\"Font N {iter}: {random_font}\")\n",
"\n",
" # Load text font and set size\n",
" fuente = ImageFont.truetype(font=random_font, size=tamano_letra)\n",
" # Get line text\n",
" texto_linea = texto[iter * N_horizontal : (iter+1) * N_horizontal]\n",
"\n",
" # Adjust text position\n",
" posicion_texto = ((imagen.width - fuente.getsize(texto_linea)[0]) // 2, \n",
" int(1.5* iter * tamano_letra))\n",
"\n",
" # Write text\n",
" dibujo.text(posicion_texto, texto_linea, font=fuente, fill=color_texto)\n",
"\n",
" # Save image\n",
" imagen.save(ruta_guardado)\n",
"\n"
]
},
{
"cell_type": "code",
"execution_count": 210,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"147\n"
]
},
{
"name": "stderr",
"output_type": "stream",
"text": [
"/tmp/ipykernel_28967/2165524556.py:1: MatplotlibDeprecationWarning: \n",
"The get_fontconfig_fonts function was deprecated in Matplotlib 3.5 and will be removed two minor releases later.\n",
" system_fonts = font_manager.get_fontconfig_fonts()#findSystemFonts(fontpaths=None, fontext='ttf')\n"
]
}
],
"source": [
"system_fonts = font_manager.get_fontconfig_fonts()#findSystemFonts(fontpaths=None, fontext='ttf')\n",
"ttf_fonts = [font for font in system_fonts if ((\".ttf\" in font) and (\"lohit\" not in font) and (\"kacst\" not in font)) and (\"Navilu\" not in font) and (\"telu\" not in font) and (\"lyx\" not in font) and (\"malayalam\" not in font) and (\"tlwg\" not in font) and (\"samyak\" not in font) and (\"droid\" not in font) and (\"kalapi\" not in font) and (\"openoffice\" not in font) and (\"orya\" not in font)]\n",
"print(len(ttf_fonts))"
]
},
{
"cell_type": "code",
"execution_count": 209,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"33\n",
"7\n",
"Font N 0: /usr/share/fonts/truetype/liberation2/LiberationSans-BoldItalic.ttf\n",
"Font N 1: /usr/share/fonts/truetype/liberation/LiberationSans-Italic.ttf\n",
"Font N 2: /usr/share/fonts/truetype/ubuntu/Ubuntu-C.ttf\n",
"Font N 3: /usr/share/fonts/truetype/ttf-bitstream-vera/VeraBI.ttf\n",
"Font N 4: /usr/share/fonts/truetype/ubuntu/UbuntuMono-R.ttf\n",
"Font N 5: /usr/share/fonts/truetype/lato/Lato-LightItalic.ttf\n",
"Font N 6: /usr/share/fonts/truetype/liberation2/LiberationSans-BoldItalic.ttf\n",
"Font N 7: /usr/share/fonts/truetype/liberation2/LiberationSans-Bold.ttf\n",
"Font N 8: /usr/share/fonts/truetype/ubuntu/Ubuntu-R.ttf\n",
"Font N 9: /usr/share/fonts/truetype/fonts-gujr-extra/Rekha.ttf\n",
"Font N 10: /usr/share/fonts/truetype/fonts-beng-extra/MuktiNarrowBold.ttf\n",
"Font N 11: /usr/share/fonts/truetype/tibetan-machine/TibetanMachineUni.ttf\n",
"Font N 12: /usr/share/fonts/truetype/Sahadeva/sahadeva.ttf\n",
"Font N 13: /usr/share/fonts/truetype/lato/Lato-Heavy.ttf\n",
"Font N 14: /usr/share/fonts/truetype/padauk/PadaukBook-Bold.ttf\n",
"Font N 15: /usr/share/fonts/truetype/padauk/Padauk-Bold.ttf\n",
"Font N 16: /usr/share/fonts/truetype/lato/Lato-Regular.ttf\n",
"Font N 17: /usr/share/fonts/truetype/pagul/Pagul.ttf\n",
"Font N 18: /usr/share/fonts/truetype/fonts-yrsa-rasa/Yrsa-SemiBold.ttf\n",
"Font N 19: /usr/share/fonts/truetype/freefont/FreeMonoOblique.ttf\n",
"Font N 20: /usr/share/fonts/truetype/liberation/LiberationMono-Regular.ttf\n",
"Font N 21: /usr/share/fonts/truetype/fonts-beng-extra/JamrulNormal.ttf\n",
"Font N 22: /usr/share/fonts/truetype/liberation/LiberationMono-Regular.ttf\n",
"Font N 23: /usr/share/fonts/truetype/lato/Lato-Italic.ttf\n",
"Font N 24: /usr/share/fonts/truetype/liberation2/LiberationSerif-Regular.ttf\n",
"Font N 25: /usr/share/fonts/truetype/ubuntu/Ubuntu-L.ttf\n",
"Font N 26: /usr/share/fonts/truetype/ttf-bitstream-vera/Vera.ttf\n",
"Font N 27: /usr/share/fonts/truetype/dejavu/DejaVuSerifCondensed-Bold.ttf\n",
"Font N 28: /usr/share/fonts/truetype/dejavu/DejaVuSerifCondensed-Bold.ttf\n",
"Font N 29: /usr/share/fonts/truetype/liberation/LiberationMono-Regular.ttf\n",
"Font N 30: /usr/share/fonts/truetype/fonts-yrsa-rasa/Yrsa-Medium.ttf\n",
"Font N 31: /usr/share/fonts/truetype/freefont/FreeSans.ttf\n",
"Font N 32: /usr/share/fonts/truetype/fonts-gujr-extra/padmaa-Bold.1.1.ttf\n"
]
},
{
"name": "stderr",
"output_type": "stream",
"text": [
"/tmp/ipykernel_28967/2273520072.py:44: MatplotlibDeprecationWarning: \n",
"The get_fontconfig_fonts function was deprecated in Matplotlib 3.5 and will be removed two minor releases later.\n",
" system_fonts = font_manager.get_fontconfig_fonts()\n",
"/tmp/ipykernel_28967/2273520072.py:59: DeprecationWarning: getsize is deprecated and will be removed in Pillow 10 (2023-07-01). Use getbbox or getlength instead.\n",
" posicion_texto = ((imagen.width - fuente.getsize(texto_linea)[0]) // 2,\n"
]
}
],
"source": [
"# initializing size of string\n",
"N_total = 30000\n",
"img_shape = (1600,900)\n",
"N_lines = N_total//img_shape[1]\n",
"tamano_letra = 22\n",
"N_horizontal = img_shape[0]//(10*tamano_letra)\n",
"\n",
"print(N_lines)\n",
"print(N_horizontal)\n",
"\n",
"# Generating random strings\n",
"texto = ''.join(random.choices(string.ascii_letters +\n",
" string.digits, k=N_total))\n",
"\n",
"color_texto = \"black\"\n",
"fondo_color = \"white\"\n",
"ruta_guardado = \"./imagen_con_palabras.png\"\n",
"\n",
"# Random font por cada línea\n",
"generar_imagen_con_palabras_rndfont(texto, img_shape, tamano_letra, color_texto, fondo_color, ruta_guardado)\n",
"\n",
"# Texto especificando un ttf\n",
"# tipo_letra = \"Playfair_Display/static/PlayfairDisplay-Black.ttf\" # Reemplaza con la ruta de tu tipo de letra\n",
"# generar_imagen_con_palabras(texto, img_shape, tipo_letra, tamano_letra, color_texto, fondo_color, ruta_guardado)"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": []
}
],
"metadata": {
"kernelspec": {
"display_name": "base",
"language": "python",
"name": "python3"
},
"language_info": {
"codemirror_mode": {
"name": "ipython",
"version": 3
},
"file_extension": ".py",
"mimetype": "text/x-python",
"name": "python",
"nbconvert_exporter": "python",
"pygments_lexer": "ipython3",
"version": "3.9.13"
},
"orig_nbformat": 4
},
"nbformat": 4,
"nbformat_minor": 2
}