{"id":3120,"date":"2020-12-19T16:55:44","date_gmt":"2020-12-19T15:55:44","guid":{"rendered":"https:\/\/www.picademie.nl\/?p=3120"},"modified":"2025-02-07T08:15:10","modified_gmt":"2025-02-07T07:15:10","slug":"tekstoutput-in-kleuren-python","status":"publish","type":"post","link":"https:\/\/www.picademie.nl\/index.php\/2020\/12\/19\/tekstoutput-in-kleuren-python\/","title":{"rendered":"ANSI tekstoutput in kleur (Python)"},"content":{"rendered":"\n<pre class=\"wp-block-preformatted\">ANSI is de afkorting van 'American National Standards Institute'. Het betreft een Amerikaanse non-profitorganisatie die een aantal Amerikaanse standaarden en normeringen beheert zoals de tekenreeks die computers gebruiken bij de invoer van het toetsenbord.  <\/pre>\n\n\n\n<p><\/p>\n\n\n\n<p>In Python kun je nauwelijks tekst opmaak doen. Gelukkig kunnen we wel tekst, of de achtergrond ervan in kleur printen. Hier zijn een aantal voorbeelden die een script kunnen opfleuren.<\/p>\n\n\n\n<p>De<mark style=\"background-color:rgba(0, 0, 0, 0)\" class=\"has-inline-color has-black-color\"> ANSI c<\/mark>ode om tekst in kleur af te beelden is<strong> <mark style=\"background-color:rgba(0, 0, 0, 0)\" class=\"has-inline-color has-vivid-cyan-blue-color\">ESC[1;<\/mark>kleurcode<mark style=\"background-color:rgba(0, 0, 0, 0)\" class=\"has-inline-color has-vivid-cyan-blue-color\">m<\/mark><\/strong><\/p>\n\n\n\n<p>-> In <mark style=\"background-color:rgba(0, 0, 0, 0)\" class=\"has-inline-color has-luminous-vivid-orange-color\">Python <\/mark>noteer je dit als: <mark style=\"background-color:rgba(0, 0, 0, 0)\" class=\"has-inline-color has-vivid-cyan-blue-color\"><strong>\\033<\/strong>[1;<\/mark><strong>kleurcode<\/strong><mark style=\"background-color:rgba(0, 0, 0, 0)\" class=\"has-inline-color has-vivid-cyan-blue-color\">m<\/mark><\/p>\n\n\n\n<p>De deel van de kleurcodes zie je in de tabel hieronder.<\/p>\n\n\n\n<p>Om de kleur te stoppen gebruik je i<mark style=\"background-color:rgba(0, 0, 0, 0)\" class=\"has-inline-color has-black-color\">n ANSI<\/mark>:<strong> <mark style=\"background-color:rgba(0, 0, 0, 0)\" class=\"has-inline-color has-vivid-cyan-blue-color\">ESC[1;m<\/mark><\/strong><\/p>\n\n\n\n<p>-> In <mark style=\"background-color:rgba(0, 0, 0, 0)\" class=\"has-inline-color has-luminous-vivid-orange-color\">Python <\/mark>noteer je dit als: <mark style=\"background-color:rgba(0, 0, 0, 0)\" class=\"has-inline-color has-vivid-cyan-blue-color\"><strong>\\033<\/strong>[1;m<\/mark><\/p>\n\n\n\n<p>Zie hieronder een aantal voorbeelden i.c.m. het Python print commando.<\/p>\n\n\n\n<p><mark style=\"background-color:rgba(0, 0, 0, 0)\" class=\"has-inline-color has-vivid-cyan-blue-color\"><strong>print<\/strong> &#8216;<strong>\\033<\/strong>[1;30m<\/mark><mark style=\"background-color:rgba(0, 0, 0, 0)\" class=\"has-inline-color has-black-color\">Gray like Ghost<\/mark><mark style=\"background-color:rgba(0, 0, 0, 0)\" class=\"has-inline-color has-vivid-cyan-blue-color\"><strong>\\033<\/strong>[1;m&#8217;<br><strong>print<\/strong> &#8216;<strong>\\033<\/strong>[1;31m<\/mark><mark style=\"background-color:rgba(0, 0, 0, 0)\" class=\"has-inline-color has-black-color\">Red like Radish<\/mark><mark style=\"background-color:rgba(0, 0, 0, 0)\" class=\"has-inline-color has-vivid-cyan-blue-color\"><strong>\\033<\/strong>[1;m&#8217;<br><strong>print<\/strong> &#8216;<strong>\\033<\/strong>[1;32m<\/mark><mark style=\"background-color:rgba(0, 0, 0, 0)\" class=\"has-inline-color has-black-color\">Green like Grass<\/mark><mark style=\"background-color:rgba(0, 0, 0, 0)\" class=\"has-inline-color has-vivid-cyan-blue-color\"><strong>\\033<\/strong>[1;m&#8217;<br><strong>print<\/strong> &#8216;<strong>\\033<\/strong>[1;33m<\/mark><mark style=\"background-color:rgba(0, 0, 0, 0)\" class=\"has-inline-color has-black-color\">Yellow like Yolk<\/mark><mark style=\"background-color:rgba(0, 0, 0, 0)\" class=\"has-inline-color has-vivid-cyan-blue-color\"><strong>\\033<\/strong>[1;m&#8217;<br><strong>print<\/strong> &#8216;<strong>\\033<\/strong>[1;34m<\/mark><mark style=\"background-color:rgba(0, 0, 0, 0)\" class=\"has-inline-color has-black-color\">Blue like Blood<\/mark><mark style=\"background-color:rgba(0, 0, 0, 0)\" class=\"has-inline-color has-vivid-cyan-blue-color\"><strong>\\033<\/strong>[1;m&#8217;<br><strong>print<\/strong> &#8216;<strong>\\033<\/strong>[1;35m<\/mark><mark style=\"background-color:rgba(0, 0, 0, 0)\" class=\"has-inline-color has-black-color\">Magenta like Mimosa<\/mark><mark style=\"background-color:rgba(0, 0, 0, 0)\" class=\"has-inline-color has-vivid-cyan-blue-color\"><strong>\\033<\/strong>[1;m&#8217;<br><strong>print<\/strong> &#8216;<strong>\\033<\/strong>[1;36m<\/mark><mark style=\"background-color:rgba(0, 0, 0, 0)\" class=\"has-inline-color has-black-color\">Cyan like Caribbean<\/mark><mark style=\"background-color:rgba(0, 0, 0, 0)\" class=\"has-inline-color has-vivid-cyan-blue-color\"><strong>\\033<\/strong>[1;m&#8217;<br><strong>print<\/strong> &#8216;<strong>\\033<\/strong>[1;37m<\/mark><mark style=\"background-color:rgba(0, 0, 0, 0)\" class=\"has-inline-color has-black-color\">White like Whipped Cream<\/mark><mark style=\"background-color:rgba(0, 0, 0, 0)\" class=\"has-inline-color has-vivid-cyan-blue-color\"><strong>\\033<\/strong>[1;m&#8217;<br><strong>print<\/strong> &#8216;<strong>\\033<\/strong>[1;38m<\/mark><mark style=\"background-color:rgba(0, 0, 0, 0)\" class=\"has-inline-color has-black-color\">Crimson like Chianti<\/mark><mark style=\"background-color:rgba(0, 0, 0, 0)\" class=\"has-inline-color has-vivid-cyan-blue-color\"><strong>\\033<\/strong>[1;m&#8217;<br><strong>print<\/strong> &#8216;<strong>\\033<\/strong>[1;41m<\/mark><mark style=\"background-color:rgba(0, 0, 0, 0)\" class=\"has-inline-color has-black-color\">Highlighted Red like Radish<\/mark><mark style=\"background-color:rgba(0, 0, 0, 0)\" class=\"has-inline-color has-vivid-cyan-blue-color\"><strong>\\033<\/strong>[1;m&#8217;<br><strong>print<\/strong> &#8216;<strong>\\033<\/strong>[1;42m<\/mark><mark style=\"background-color:rgba(0, 0, 0, 0)\" class=\"has-inline-color has-black-color\">Highlighted Green like Grass<\/mark><mark style=\"background-color:rgba(0, 0, 0, 0)\" class=\"has-inline-color has-vivid-cyan-blue-color\"><strong>\\033<\/strong>[1;m&#8217;<br><strong>print<\/strong> &#8216;<strong>\\033<\/strong>[1;43m<\/mark><mark style=\"background-color:rgba(0, 0, 0, 0)\" class=\"has-inline-color has-black-color\">Highlighted Brown like Bear<\/mark><mark style=\"background-color:rgba(0, 0, 0, 0)\" class=\"has-inline-color has-vivid-cyan-blue-color\"><strong>\\033<\/strong>[1;m&#8217;<br><strong>print<\/strong> &#8216;<strong>\\033<\/strong>[1;44m<\/mark><mark style=\"background-color:rgba(0, 0, 0, 0)\" class=\"has-inline-color has-black-color\">Highlighted Blue like Blood<\/mark><mark style=\"background-color:rgba(0, 0, 0, 0)\" class=\"has-inline-color has-vivid-cyan-blue-color\"><strong>\\033<\/strong>[1;m&#8217;<br><strong>print<\/strong> &#8216;<strong>\\033<\/strong>[1;45m<\/mark><mark style=\"background-color:rgba(0, 0, 0, 0)\" class=\"has-inline-color has-black-color\">Highlighted Magenta like Mimosa<\/mark><mark style=\"background-color:rgba(0, 0, 0, 0)\" class=\"has-inline-color has-vivid-cyan-blue-color\"><strong>\\033<\/strong>[1;m&#8217;<br><strong>print<\/strong> &#8216;<strong>\\033<\/strong>[1;46m<\/mark><mark style=\"background-color:rgba(0, 0, 0, 0)\" class=\"has-inline-color has-black-color\">Highlighted Cyan like Caribbean<\/mark><mark style=\"background-color:rgba(0, 0, 0, 0)\" class=\"has-inline-color has-vivid-cyan-blue-color\"><strong>\\033<\/strong>[1;m&#8217;<br><strong>print<\/strong> &#8216;<strong>\\033<\/strong>[1;47m<\/mark><mark style=\"background-color:rgba(0, 0, 0, 0)\" class=\"has-inline-color has-black-color\">Highlighted Gray like Ghost<\/mark><mark style=\"background-color:rgba(0, 0, 0, 0)\" class=\"has-inline-color has-vivid-cyan-blue-color\"><strong>\\033<\/strong>[1;m&#8217;<br><strong>print<\/strong> &#8216;<strong>\\033<\/strong>[1;48m<\/mark><mark style=\"background-color:rgba(0, 0, 0, 0)\" class=\"has-inline-color has-black-color\">Highlighted Crimson like Chianti<\/mark><mark style=\"background-color:rgba(0, 0, 0, 0)\" class=\"has-inline-color has-vivid-cyan-blue-color\"><strong>\\033<\/strong>[1;m&#8217;<\/mark><\/p>\n\n\n\n<p>Bovenstaande code geeft in Python respectievelijk de volgende kleuren:<\/p>\n\n\n\n<figure class=\"wp-block-image size-large\"><img loading=\"lazy\" decoding=\"async\" width=\"438\" height=\"511\" src=\"https:\/\/www.picademie.nl\/wp-content\/uploads\/2020\/12\/image-71.png\" alt=\"\" class=\"wp-image-3121\" srcset=\"https:\/\/www.picademie.nl\/wp-content\/uploads\/2020\/12\/image-71.png 438w, https:\/\/www.picademie.nl\/wp-content\/uploads\/2020\/12\/image-71-257x300.png 257w\" sizes=\"auto, (max-width: 438px) 100vw, 438px\" \/><\/figure>\n\n\n\n<p><strong>Voorbeeld code<\/strong><br>Onderstaand Pythonscript laat alle 256 variaties zien.<\/p>\n\n\n\n<p><\/p>\n\n\n\n<figure class=\"wp-block-image size-full\"><img loading=\"lazy\" decoding=\"async\" width=\"573\" height=\"109\" src=\"https:\/\/www.picademie.nl\/wp-content\/uploads\/2022\/10\/image-15.png\" alt=\"\" class=\"wp-image-10945\" srcset=\"https:\/\/www.picademie.nl\/wp-content\/uploads\/2022\/10\/image-15.png 573w, https:\/\/www.picademie.nl\/wp-content\/uploads\/2022\/10\/image-15-300x57.png 300w\" sizes=\"auto, (max-width: 573px) 100vw, 573px\" \/><\/figure>\n\n\n\n<div class=\"wp-block-file\"><a id=\"wp-block-file--media-a298e8ce-dc36-4a43-b5f7-d3ed9d9c945c\" href=\"https:\/\/www.picademie.nl\/wp-content\/uploads\/2022\/10\/ANSI256.zip\">Je kunt het gezipte bestand ANSI256.py ook hier downloaden<\/a><a href=\"https:\/\/www.picademie.nl\/wp-content\/uploads\/2022\/10\/ANSI256.zip\" class=\"wp-block-file__button\" download aria-describedby=\"wp-block-file--media-a298e8ce-dc36-4a43-b5f7-d3ed9d9c945c\">Downloaden<\/a><\/div>\n\n\n\n<p><\/p>\n\n\n\n<figure class=\"wp-block-image size-full\"><img loading=\"lazy\" decoding=\"async\" width=\"645\" height=\"261\" src=\"https:\/\/www.picademie.nl\/wp-content\/uploads\/2022\/10\/image-16.png\" alt=\"\" class=\"wp-image-10950\" srcset=\"https:\/\/www.picademie.nl\/wp-content\/uploads\/2022\/10\/image-16.png 645w, https:\/\/www.picademie.nl\/wp-content\/uploads\/2022\/10\/image-16-300x121.png 300w\" sizes=\"auto, (max-width: 645px) 100vw, 645px\" \/><figcaption>Escape code naar ANSI standaard<\/figcaption><\/figure>\n\n\n\n<p><strong>Scherm wissen<\/strong><br>Een ander handig commando is: <strong><mark style=\"background-color:rgba(0, 0, 0, 0)\" class=\"has-inline-color has-vivid-cyan-blue-color\">print (&#8220;\\033<\/mark><\/strong>[1;1;1m,<strong><mark style=\"background-color:rgba(0, 0, 0, 0)\" class=\"has-inline-color has-vivid-cyan-blue-color\">\\033c<\/mark>&#8220;<\/strong>)<\/p>\n\n\n\n<p><strong>Er is nog meer!<\/strong><br>De ANSI codering voor tekst kleur is erg uitgebreid. Hierboven vind je een basis waarmee je een eerste opmaak van een script kunt realiseren. Wil je meer functies zoals <mark style=\"background-color:rgba(0, 0, 0, 0)\" class=\"has-inline-color has-vivid-cyan-blue-color\">knipperende tekst<\/mark>? Kijk dan op het internet daar vind je nog veel meer informatie.<\/p>\n\n\n\n<p><em>Have A Nice Day!<\/em><\/p>\n","protected":false},"excerpt":{"rendered":"<div class=\"mh-excerpt\"><p>ANSI is de afkorting van &#8216;American National Standards Institute&#8217;. Het betreft een Amerikaanse non-profitorganisatie die een aantal Amerikaanse standaarden en normeringen beheert zoals de tekenreeks <a class=\"mh-excerpt-more\" href=\"https:\/\/www.picademie.nl\/index.php\/2020\/12\/19\/tekstoutput-in-kleuren-python\/\" title=\"ANSI tekstoutput in kleur (Python)\">[&#8230;]<\/a><\/p>\n<\/div>","protected":false},"author":1,"featured_media":3124,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"jetpack_post_was_ever_published":false,"_jetpack_newsletter_access":"","_jetpack_dont_email_post_to_subs":false,"_jetpack_newsletter_tier_id":0,"_jetpack_memberships_contains_paywalled_content":false,"_jetpack_memberships_contains_paid_content":false,"footnotes":""},"categories":[1],"tags":[],"class_list":["post-3120","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-uncategorized"],"jetpack_featured_media_url":"https:\/\/www.picademie.nl\/wp-content\/uploads\/2020\/12\/ANSI2.png","jetpack_sharing_enabled":true,"_links":{"self":[{"href":"https:\/\/www.picademie.nl\/index.php\/wp-json\/wp\/v2\/posts\/3120","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.picademie.nl\/index.php\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.picademie.nl\/index.php\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.picademie.nl\/index.php\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/www.picademie.nl\/index.php\/wp-json\/wp\/v2\/comments?post=3120"}],"version-history":[{"count":0,"href":"https:\/\/www.picademie.nl\/index.php\/wp-json\/wp\/v2\/posts\/3120\/revisions"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/www.picademie.nl\/index.php\/wp-json\/wp\/v2\/media\/3124"}],"wp:attachment":[{"href":"https:\/\/www.picademie.nl\/index.php\/wp-json\/wp\/v2\/media?parent=3120"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.picademie.nl\/index.php\/wp-json\/wp\/v2\/categories?post=3120"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.picademie.nl\/index.php\/wp-json\/wp\/v2\/tags?post=3120"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}