python-thumbnails.html 8.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122
  1. <!DOCTYPE html>
  2. <html xmlns="http://www.w3.org/1999/xhtml" lang="" xml:lang="">
  3. <head>
  4. <meta charset="utf-8" />
  5. <meta name="generator" content="pandoc" />
  6. <meta name="viewport" content="width=device-width, initial-scale=1.0, user-scalable=yes" />
  7. <title>Python Thumbnails</title>
  8. <style>
  9. code{white-space: pre-wrap;}
  10. span.smallcaps{font-variant: small-caps;}
  11. span.underline{text-decoration: underline;}
  12. div.column{display: inline-block; vertical-align: top; width: 50%;}
  13. div.hanging-indent{margin-left: 1.5em; text-indent: -1.5em;}
  14. ul.task-list{list-style: none;}
  15. pre > code.sourceCode { white-space: pre; position: relative; }
  16. pre > code.sourceCode > span { display: inline-block; line-height: 1.25; }
  17. pre > code.sourceCode > span:empty { height: 1.2em; }
  18. .sourceCode { overflow: visible; }
  19. code.sourceCode > span { color: inherit; text-decoration: inherit; }
  20. div.sourceCode { margin: 1em 0; }
  21. pre.sourceCode { margin: 0; }
  22. @media screen {
  23. div.sourceCode { overflow: auto; }
  24. }
  25. @media print {
  26. pre > code.sourceCode { white-space: pre-wrap; }
  27. pre > code.sourceCode > span { text-indent: -5em; padding-left: 5em; }
  28. }
  29. pre.numberSource code
  30. { counter-reset: source-line 0; }
  31. pre.numberSource code > span
  32. { position: relative; left: -4em; counter-increment: source-line; }
  33. pre.numberSource code > span > a:first-child::before
  34. { content: counter(source-line);
  35. position: relative; left: -1em; text-align: right; vertical-align: baseline;
  36. border: none; display: inline-block;
  37. -webkit-touch-callout: none; -webkit-user-select: none;
  38. -khtml-user-select: none; -moz-user-select: none;
  39. -ms-user-select: none; user-select: none;
  40. padding: 0 4px; width: 4em;
  41. color: #aaaaaa;
  42. }
  43. pre.numberSource { margin-left: 3em; border-left: 1px solid #aaaaaa; padding-left: 4px; }
  44. div.sourceCode
  45. { background-color: #f8f8f8; }
  46. @media screen {
  47. pre > code.sourceCode > span > a:first-child::before { text-decoration: underline; }
  48. }
  49. code span.al { color: #ef2929; } /* Alert */
  50. code span.an { color: #8f5902; font-weight: bold; font-style: italic; } /* Annotation */
  51. code span.at { color: #c4a000; } /* Attribute */
  52. code span.bn { color: #0000cf; } /* BaseN */
  53. code span.cf { color: #204a87; font-weight: bold; } /* ControlFlow */
  54. code span.ch { color: #4e9a06; } /* Char */
  55. code span.cn { color: #000000; } /* Constant */
  56. code span.co { color: #8f5902; font-style: italic; } /* Comment */
  57. code span.cv { color: #8f5902; font-weight: bold; font-style: italic; } /* CommentVar */
  58. code span.do { color: #8f5902; font-weight: bold; font-style: italic; } /* Documentation */
  59. code span.dt { color: #204a87; } /* DataType */
  60. code span.dv { color: #0000cf; } /* DecVal */
  61. code span.er { color: #a40000; font-weight: bold; } /* Error */
  62. code span.ex { } /* Extension */
  63. code span.fl { color: #0000cf; } /* Float */
  64. code span.fu { color: #000000; } /* Function */
  65. code span.im { } /* Import */
  66. code span.in { color: #8f5902; font-weight: bold; font-style: italic; } /* Information */
  67. code span.kw { color: #204a87; font-weight: bold; } /* Keyword */
  68. code span.op { color: #ce5c00; font-weight: bold; } /* Operator */
  69. code span.ot { color: #8f5902; } /* Other */
  70. code span.pp { color: #8f5902; font-style: italic; } /* Preprocessor */
  71. code span.sc { color: #000000; } /* SpecialChar */
  72. code span.ss { color: #4e9a06; } /* SpecialString */
  73. code span.st { color: #4e9a06; } /* String */
  74. code span.va { color: #000000; } /* Variable */
  75. code span.vs { color: #4e9a06; } /* VerbatimString */
  76. code span.wa { color: #8f5902; font-weight: bold; font-style: italic; } /* Warning */
  77. .display.math{display: block; text-align: center; margin: 0.5rem auto;}
  78. </style>
  79. <link rel="stylesheet" href="C:\SGZ_Pro\Hobbys\Writing\Org\pages\blog\styles.css" />
  80. <!--[if lt IE 9]>
  81. <script src="//cdnjs.cloudflare.com/ajax/libs/html5shiv/3.7.3/html5shiv-printshiv.min.js"></script>
  82. <![endif]-->
  83. <link rel='stylesheet' type='text/css' href='styles.css' />
  84. </head>
  85. <body>
  86. <header id="title-block-header">
  87. <h1 class="title">Python Thumbnails</h1>
  88. </header>
  89. <h1 id="what-is-this">What is this?</h1>
  90. <p>This is a script for me to generate video thumbnails for none video publications. I am way to lazy to create thumbnails so this just does it automaticly.</p>
  91. <div class="sourceCode" id="cb1"><pre class="sourceCode python"><code class="sourceCode python"><span id="cb1-1"><a href="#cb1-1" aria-hidden="true" tabindex="-1"></a><span class="co"># Python image processing imports. Handling text and other things.</span></span>
  92. <span id="cb1-2"><a href="#cb1-2" aria-hidden="true" tabindex="-1"></a><span class="im">from</span> PIL <span class="im">import</span> Image, ImageDraw, ImageFont</span>
  93. <span id="cb1-3"><a href="#cb1-3" aria-hidden="true" tabindex="-1"></a><span class="co"># This if for text wrapping. Using \n isn&#39;t supported in PIL text</span></span>
  94. <span id="cb1-4"><a href="#cb1-4" aria-hidden="true" tabindex="-1"></a><span class="co"># stuff.</span></span>
  95. <span id="cb1-5"><a href="#cb1-5" aria-hidden="true" tabindex="-1"></a><span class="im">import</span> textwrap</span></code></pre></div>
  96. <h1 id="open-the-image">Open the image</h1>
  97. <p>In a directory I store <code class="verbatim">background.png</code> here I just open it so I can manipulate it.</p>
  98. <div class="sourceCode" id="cb2"><pre class="sourceCode python"><code class="sourceCode python"><span id="cb2-1"><a href="#cb2-1" aria-hidden="true" tabindex="-1"></a><span class="co"># Image open and draw</span></span>
  99. <span id="cb2-2"><a href="#cb2-2" aria-hidden="true" tabindex="-1"></a>image <span class="op">=</span> Image.<span class="bu">open</span>(<span class="st">&quot;background.png&quot;</span>)</span>
  100. <span id="cb2-3"><a href="#cb2-3" aria-hidden="true" tabindex="-1"></a>draw <span class="op">=</span> ImageDraw.Draw(image)</span></code></pre></div>
  101. <h1 id="add-the-text">Add the text</h1>
  102. <p>Ask for input, run it through text wrap, specify a font I have in the directory and finally put it on the image.</p>
  103. <div class="sourceCode" id="cb3"><pre class="sourceCode python"><code class="sourceCode python"><span id="cb3-1"><a href="#cb3-1" aria-hidden="true" tabindex="-1"></a><span class="co"># Text</span></span>
  104. <span id="cb3-2"><a href="#cb3-2" aria-hidden="true" tabindex="-1"></a>text <span class="op">=</span> <span class="bu">input</span>(<span class="st">&#39;Text: &#39;</span>)</span>
  105. <span id="cb3-3"><a href="#cb3-3" aria-hidden="true" tabindex="-1"></a>textwrapped <span class="op">=</span> textwrap.wrap(text, width<span class="op">=</span><span class="dv">30</span>)</span>
  106. <span id="cb3-4"><a href="#cb3-4" aria-hidden="true" tabindex="-1"></a>dubai <span class="op">=</span> ImageFont.truetype(<span class="st">&quot;ReemKufi-Regular.ttf&quot;</span>, <span class="dv">100</span>)</span>
  107. <span id="cb3-5"><a href="#cb3-5" aria-hidden="true" tabindex="-1"></a>draw.text((<span class="dv">400</span>,<span class="dv">400</span>), <span class="st">&#39;</span><span class="ch">\n</span><span class="st">&#39;</span>.join(textwrapped), font<span class="op">=</span>dubai, fill<span class="op">=</span><span class="st">&quot;#EE7A2F&quot;</span>)</span></code></pre></div>
  108. <h1 id="saving-it">Saving it</h1>
  109. <p>I do some manipulation on the text variable so I get a nicer file name.</p>
  110. <div class="sourceCode" id="cb4"><pre class="sourceCode python"><code class="sourceCode python"><span id="cb4-1"><a href="#cb4-1" aria-hidden="true" tabindex="-1"></a>filename <span class="op">=</span> text.replace(<span class="st">&#39; &#39;</span>, <span class="st">&#39;-&#39;</span>)<span class="op">+</span><span class="st">&quot;.png&quot;</span></span>
  111. <span id="cb4-2"><a href="#cb4-2" aria-hidden="true" tabindex="-1"></a>image.save(filename, <span class="st">&quot;PNG&quot;</span>)</span>
  112. <span id="cb4-3"><a href="#cb4-3" aria-hidden="true" tabindex="-1"></a>image.show()</span></code></pre></div>
  113. <h1 id="codeberg-link">Codeberg link</h1>
  114. <p><a href="https://codeberg.org/zortazert/Python-Projects/src/branch/main/thumbnail">https://codeberg.org/zortazert/Python-Projects/src/branch/main/thumbnail</a></p>
  115. <hr>
  116. <footer>
  117. <a rel='license' href='http://creativecommons.org/licenses/by-sa/4.0/'><img alt='Creative Commons License' style='border-width:0' width='88' height='31' src='../images/cc-by-sa.png' /></a><br>
  118. Unless otherwise noted, all content on this website is Copyright Zortazert 2021-2022 and is licensed under <a rel='license' href='http://creativecommons.org/licenses/by-sa/4.0/'>CC BY-SA 4.0</a>.
  119. </footer>
  120. </body>
  121. </html>