Open In App

Generating Beautiful Code Snippets using Python

When creating technical documentation or tutorials, it can be helpful to include images of code snippets to illustrate specific examples or concepts. However, taking a screenshot of a code file can look unprofessional and hard to read. This article will explore how to convert programming code into beautiful image snippets in Python using the library pygments.

Pygments library

Pygments is a syntax highlighting library that supports over 500 different programming languages. It allows you to format a code snippet into a visually appealing image. This can be useful for creating technical documentation, tutorials, or even social media posts.



The library offers various customization options, such as selecting from various color schemes and adjusting the font size. It also supports several file formats for outputting the final image, including PNG, JPEG, and SVG.

To install the library in your Python distribution, run the following command in the command processor of your Operating System:



py -m pip install pygment

Procedure

  1. Import the library in your Python script using the statement: from pygments import highlight
  2. Define the code snippet you want to convert into an image using a string variable.
  3. Define the code snippet’s language using the highlight function’s lexer argument.
  4. Define the format of the output image using the format argument of the highlight function. The options include png, jpg, svg, and others.
  5. Define the style of the output image using the style argument of the highlight function. You can select from a list of pre-defined styles or create your custom style.
  6. Call the highlight function and pass in the code snippet, lexer, formatter, and style arguments. The function will return the image data of the code snippet.
  7. Save the image data to a file using the appropriate file-handling functions in Python.

Example 1: 

Creating a PNG image of a Python code snippet using the “friendly” style




from pygments import highlight
from pygments.lexers import PythonLexer
from pygments.formatters import ImageFormatter
  
code = """
def add(a, b):
    return a + b
"""
  
img = highlight(code, PythonLexer(), 
                ImageFormatter(style='friendly'), 
                outfile="code.png")

Output:

output image

Firstly the relevant modules are imported. The code that is to be converted into a snippet is defined in a string variable (multiline). Then the code, along with the function for the language of the code (in this case PythonLexer), the formatting style (friendly in this case) and the output filename are passed as an argument to the highlight function. The highlight function produces the output image of the specified language and 

Example 2:

Creating a JPEG image of a JavaScript code snippet using the “xcode” style




from pygments import highlight
from pygments.lexers import JavascriptLexer
from pygments.formatters import ImageFormatter
  
code = """
function add(a, b) {
    return a + b;
}
"""
  
img = highlight(code, JavascriptLexer(), 
                ImageFormatter(style='xcode'),
                outfile="code.jpeg")

Output:

Output image2

The above code is the same as the previous example. This time the code for javascript language, hence using the JavascriptLexer function, the formatting style as xcode, and the output filename as code.jpg being passed as an argument to the highlight function.

Example 3:

Creating an SVG image of a C++ code snippet using the “monokai” style




from pygments import highlight
from pygments.lexers import CppLexer
from pygments.formatters import ImageFormatter
  
code = """
int add(int a, int b) {
    return a + b;
}
"""
  
img = highlight(code, CppLexer(),
                ImageFormatter(style='monokai'),
                outfile="code.svg")

Output:

output image3

The code is the same as the one used in the first example. This time having a C++ code, CppLexer function, monokai formatting style, and output filename code.svg being passed as an argument to the highlight function. 


Article Tags :