Python to Convert Programming Code into Beautiful Image Snippets with Source Code

A simple python utility tool to convert programming code into beautiful image snippets.


  • certifi==2020.6.20
  • chardet==3.0.4
  • colorama==0.4.3
  • configparser==5.0.0
  • crayons==0.3.1
  • idna==2.10
  • pkg-resources==0.0.0
  • requests==2.24.0
  • selenium==3.141.0
  • urllib3==1.25.9
  • webdriver-manager==3.2.1


  1. create virtual environment
virtualenv -p python3 venv

2. Activate an environment

source venv/bin/activate

3. change working directory

cd py-carbon-clips

4. install requirements

pip install -r requirements.txt
Code language: CSS (css)

Source Code:

import os import glob from urllib import parse from selenium import webdriver from webdriver_manager.firefox import GeckoDriverManager from selenium.webdriver.firefox.options import Options def get_driver(): options = Options() options.headless = True driver = webdriver.Firefox( executable_path=GeckoDriverManager().install(), options=options) return driver def read_file(file): with open(file, 'r') as _f: text = return text def get_files(path): files = glob.glob('{}/*'.format(path)) for file in files: if os.path.isfile: yield file def encode_text(file): clip_text = read_file(file) uri_encoded_clip_text = parse.quote_plus(clip_text) return uri_encoded_clip_text def take_snapshot(web, file): elem = web.find_element_by_xpath("//div[@id='export-container']") out_file = os.path.basename(file).split('.')[0] if not os.path.isdir(OUTPUT_DIR): os.mkdir(OUTPUT_DIR) file_path = '{}/{}.png'.format(OUTPUT_DIR, out_file) elem.screenshot(file_path) def main(): with (get_driver())as web: for file in get_files(INPUT_DIR): print("{}Processing-{}{}".format('*'*10, file, '*'*10)) encoded_text = encode_text(file) url = "" + encoded_text web.get(url) take_snapshot(web, file) if __name__ == '__main__': INPUT_DIR = 'input' OUTPUT_DIR = 'clips' if os.path.isdir(INPUT_DIR): main() else: os.mkdir(INPUT_DIR) msg = '{} is created please put files in {}' print(msg.format(INPUT_DIR, os.path.abspath(INPUT_DIR)))
Code language: JavaScript (javascript)


C++ Program
Java Program
Python Program
R Program

Leave a Comment