mirror of
				https://github.com/paperless-ngx/paperless-ngx.git
				synced 2025-10-30 03:56:23 -05:00 
			
		
		
		
	fixes #7 and some test cases.
This commit is contained in:
		| @@ -1,6 +1,7 @@ | |||||||
| import os | import os | ||||||
| import subprocess | import subprocess | ||||||
|  |  | ||||||
|  | from PIL import ImageDraw, ImageFont, Image | ||||||
| from django.conf import settings | from django.conf import settings | ||||||
|  |  | ||||||
| from documents.parsers import DocumentParser, ParseError | from documents.parsers import DocumentParser, ParseError | ||||||
| @@ -12,63 +13,22 @@ class TextDocumentParser(DocumentParser): | |||||||
|     """ |     """ | ||||||
|  |  | ||||||
|     def get_thumbnail(self, document_path, mime_type): |     def get_thumbnail(self, document_path, mime_type): | ||||||
|         """ |  | ||||||
|         The thumbnail of a text file is just a 500px wide image of the text |  | ||||||
|         rendered onto a letter-sized page. |  | ||||||
|         """ |  | ||||||
|         # The below is heavily cribbed from https://askubuntu.com/a/590951 |  | ||||||
|  |  | ||||||
|         bg_color = "white"  # bg color |  | ||||||
|         text_color = "black"  # text color |  | ||||||
|         psize = [500, 647]  # icon size |  | ||||||
|         n_lines = 50  # number of lines to show |  | ||||||
|         out_path = os.path.join(self.tempdir, "convert.png") |  | ||||||
|  |  | ||||||
|         temp_bg = os.path.join(self.tempdir, "bg.png") |  | ||||||
|         temp_txlayer = os.path.join(self.tempdir, "tx.png") |  | ||||||
|         picsize = "x".join([str(n) for n in psize]) |  | ||||||
|         txsize = "x".join([str(n - 8) for n in psize]) |  | ||||||
|  |  | ||||||
|         def create_bg(): |  | ||||||
|             work_size = ",".join([str(n - 1) for n in psize]) |  | ||||||
|             r = str(round(psize[0] / 10)) |  | ||||||
|             rounded = ",".join([r, r]) |  | ||||||
|             run_command( |  | ||||||
|                 settings.CONVERT_BINARY, |  | ||||||
|                 "-size ", picsize, |  | ||||||
|                 ' xc:none -draw ', |  | ||||||
|                 '"fill ', bg_color, ' roundrectangle 0,0,', work_size, ",", rounded, '" ',  # NOQA: E501 |  | ||||||
|                 temp_bg |  | ||||||
|             ) |  | ||||||
|  |  | ||||||
|         def read_text(): |         def read_text(): | ||||||
|             with open(document_path, 'r') as src: |             with open(document_path, 'r') as src: | ||||||
|                 lines = [line.strip() for line in src.readlines()] |                 lines = [line.strip() for line in src.readlines()] | ||||||
|                 text = "\n".join([line for line in lines[:n_lines]]) |                 text = "\n".join([line for line in lines[:50]]) | ||||||
|                 return text.replace('"', "'") |                 return text.replace('"', "'") | ||||||
|  |  | ||||||
|         def create_txlayer(): |         img = Image.new("RGB", (500, 700), color="white") | ||||||
|             run_command( |         draw = ImageDraw.Draw(img) | ||||||
|                 settings.CONVERT_BINARY, |         font = ImageFont.truetype( | ||||||
|                 "-background none", |             "/usr/share/fonts/liberation/LiberationSerif-Regular.ttf", 20, | ||||||
|                 "-fill", |             layout_engine=ImageFont.LAYOUT_BASIC) | ||||||
|                 text_color, |         draw.text((5, 5), read_text(), font=font, fill="black") | ||||||
|                 "-pointsize", "12", |  | ||||||
|                 "-border 4 -bordercolor none", |  | ||||||
|                 "-size ", txsize, |  | ||||||
|                 ' caption:"', read_text(), '" ', |  | ||||||
|                 temp_txlayer |  | ||||||
|             ) |  | ||||||
|  |  | ||||||
|         create_txlayer() |         out_path = os.path.join(self.tempdir, "thumb.png") | ||||||
|         create_bg() |         img.save(out_path) | ||||||
|         run_command( |  | ||||||
|             settings.CONVERT_BINARY, |  | ||||||
|             temp_bg, |  | ||||||
|             temp_txlayer, |  | ||||||
|             "-background None -layers merge ", |  | ||||||
|             out_path |  | ||||||
|         ) |  | ||||||
|  |  | ||||||
|         return out_path |         return out_path | ||||||
|  |  | ||||||
|   | |||||||
		Reference in New Issue
	
	Block a user
	 jonaswinkler
					jonaswinkler