123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122 |
- <!DOCTYPE html>
- <html xmlns="http://www.w3.org/1999/xhtml" lang="" xml:lang="">
- <head>
- <meta charset="utf-8" />
- <meta name="generator" content="pandoc" />
- <meta name="viewport" content="width=device-width, initial-scale=1.0, user-scalable=yes" />
- <title>Python Thumbnails</title>
- <style>
- code{white-space: pre-wrap;}
- span.smallcaps{font-variant: small-caps;}
- span.underline{text-decoration: underline;}
- div.column{display: inline-block; vertical-align: top; width: 50%;}
- div.hanging-indent{margin-left: 1.5em; text-indent: -1.5em;}
- ul.task-list{list-style: none;}
- pre > code.sourceCode { white-space: pre; position: relative; }
- pre > code.sourceCode > span { display: inline-block; line-height: 1.25; }
- pre > code.sourceCode > span:empty { height: 1.2em; }
- .sourceCode { overflow: visible; }
- code.sourceCode > span { color: inherit; text-decoration: inherit; }
- div.sourceCode { margin: 1em 0; }
- pre.sourceCode { margin: 0; }
- @media screen {
- div.sourceCode { overflow: auto; }
- }
- @media print {
- pre > code.sourceCode { white-space: pre-wrap; }
- pre > code.sourceCode > span { text-indent: -5em; padding-left: 5em; }
- }
- pre.numberSource code
- { counter-reset: source-line 0; }
- pre.numberSource code > span
- { position: relative; left: -4em; counter-increment: source-line; }
- pre.numberSource code > span > a:first-child::before
- { content: counter(source-line);
- position: relative; left: -1em; text-align: right; vertical-align: baseline;
- border: none; display: inline-block;
- -webkit-touch-callout: none; -webkit-user-select: none;
- -khtml-user-select: none; -moz-user-select: none;
- -ms-user-select: none; user-select: none;
- padding: 0 4px; width: 4em;
- color: #aaaaaa;
- }
- pre.numberSource { margin-left: 3em; border-left: 1px solid #aaaaaa; padding-left: 4px; }
- div.sourceCode
- { background-color: #f8f8f8; }
- @media screen {
- pre > code.sourceCode > span > a:first-child::before { text-decoration: underline; }
- }
- code span.al { color: #ef2929; } /* Alert */
- code span.an { color: #8f5902; font-weight: bold; font-style: italic; } /* Annotation */
- code span.at { color: #c4a000; } /* Attribute */
- code span.bn { color: #0000cf; } /* BaseN */
- code span.cf { color: #204a87; font-weight: bold; } /* ControlFlow */
- code span.ch { color: #4e9a06; } /* Char */
- code span.cn { color: #000000; } /* Constant */
- code span.co { color: #8f5902; font-style: italic; } /* Comment */
- code span.cv { color: #8f5902; font-weight: bold; font-style: italic; } /* CommentVar */
- code span.do { color: #8f5902; font-weight: bold; font-style: italic; } /* Documentation */
- code span.dt { color: #204a87; } /* DataType */
- code span.dv { color: #0000cf; } /* DecVal */
- code span.er { color: #a40000; font-weight: bold; } /* Error */
- code span.ex { } /* Extension */
- code span.fl { color: #0000cf; } /* Float */
- code span.fu { color: #000000; } /* Function */
- code span.im { } /* Import */
- code span.in { color: #8f5902; font-weight: bold; font-style: italic; } /* Information */
- code span.kw { color: #204a87; font-weight: bold; } /* Keyword */
- code span.op { color: #ce5c00; font-weight: bold; } /* Operator */
- code span.ot { color: #8f5902; } /* Other */
- code span.pp { color: #8f5902; font-style: italic; } /* Preprocessor */
- code span.sc { color: #000000; } /* SpecialChar */
- code span.ss { color: #4e9a06; } /* SpecialString */
- code span.st { color: #4e9a06; } /* String */
- code span.va { color: #000000; } /* Variable */
- code span.vs { color: #4e9a06; } /* VerbatimString */
- code span.wa { color: #8f5902; font-weight: bold; font-style: italic; } /* Warning */
- .display.math{display: block; text-align: center; margin: 0.5rem auto;}
- </style>
- <link rel="stylesheet" href="C:\SGZ_Pro\Hobbys\Writing\Org\pages\blog\styles.css" />
- <!--[if lt IE 9]>
- <script src="//cdnjs.cloudflare.com/ajax/libs/html5shiv/3.7.3/html5shiv-printshiv.min.js"></script>
- <![endif]-->
- <link rel='stylesheet' type='text/css' href='styles.css' />
- </head>
- <body>
- <header id="title-block-header">
- <h1 class="title">Python Thumbnails</h1>
- </header>
- <h1 id="what-is-this">What is this?</h1>
- <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>
- <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>
- <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>
- <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't supported in PIL text</span></span>
- <span id="cb1-4"><a href="#cb1-4" aria-hidden="true" tabindex="-1"></a><span class="co"># stuff.</span></span>
- <span id="cb1-5"><a href="#cb1-5" aria-hidden="true" tabindex="-1"></a><span class="im">import</span> textwrap</span></code></pre></div>
- <h1 id="open-the-image">Open the image</h1>
- <p>In a directory I store <code class="verbatim">background.png</code> here I just open it so I can manipulate it.</p>
- <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>
- <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">"background.png"</span>)</span>
- <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>
- <h1 id="add-the-text">Add the text</h1>
- <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>
- <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>
- <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">'Text: '</span>)</span>
- <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>
- <span id="cb3-4"><a href="#cb3-4" aria-hidden="true" tabindex="-1"></a>dubai <span class="op">=</span> ImageFont.truetype(<span class="st">"ReemKufi-Regular.ttf"</span>, <span class="dv">100</span>)</span>
- <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">'</span><span class="ch">\n</span><span class="st">'</span>.join(textwrapped), font<span class="op">=</span>dubai, fill<span class="op">=</span><span class="st">"#EE7A2F"</span>)</span></code></pre></div>
- <h1 id="saving-it">Saving it</h1>
- <p>I do some manipulation on the text variable so I get a nicer file name.</p>
- <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">' '</span>, <span class="st">'-'</span>)<span class="op">+</span><span class="st">".png"</span></span>
- <span id="cb4-2"><a href="#cb4-2" aria-hidden="true" tabindex="-1"></a>image.save(filename, <span class="st">"PNG"</span>)</span>
- <span id="cb4-3"><a href="#cb4-3" aria-hidden="true" tabindex="-1"></a>image.show()</span></code></pre></div>
- <h1 id="codeberg-link">Codeberg link</h1>
- <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>
- <hr>
- <footer>
- <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>
- 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>.
- </footer>
- </body>
- </html>
|