Convert JSON to PNG in Python Last Updated : 28 Apr, 2025 Summarize Comments Improve Suggest changes Share Like Article Like Report We are given JSON data and our task is to convert JSON to PNG in Python using different approaches. In this article, we will explore how to convert JSON data into PNG images using Python. Convert JSON to PNG in PythonBelow are some of the ways by which we can convert JSON to PNG in Python: Using pil LibraryUsing Matplotlib LibraryConvert JSON to PNG in Python Using PIL (Pillow) LibraryThe Pillow library, a fork of the original Python Imaging Library (PIL), is a powerful image-processing library in Python. To use this library for converting JSON to PNG, you can follow the code below: This example uses the Pillow library to create an image, draw the JSON text on it, and save the image as a PNG file. Python3 from PIL import Image, ImageDraw, ImageFont import json def json_to_png(json_data, output_file): # Convert JSON to string json_str = json.dumps(json_data, indent=4) # Create an image with white background image = Image.new('RGB', (800, 600), 'white') draw = ImageDraw.Draw(image) # Set the font and size font = ImageFont.load_default() # Draw the JSON text on the image draw.text((10, 10), json_str, font=font, fill='black') # Save the image as PNG image.save(output_file) # Example usage sample_json = {"name": "John Doe", "age": 30, "city": "Example City"} json_to_png(sample_json, "example1_output.png") print('successfully converted JSON to PNG') Output: successfully converted JSON to PNG Convert JSON to PNG Using Matplotlib LibraryMatplotlib is a popular plotting library in Python. Though primarily designed for creating charts and graphs, it can be repurposed to visualize JSON data as an image. Here's an example: This example utilizes Matplotlib to create a text plot, remove axis elements, and save the plot as a PNG file. Python3 import matplotlib.pyplot as plt import json def json_to_png(json_data, output_file): # Convert JSON to string json_str = json.dumps(json_data, indent=4) # Create a text plot plt.text(0.5, 0.5, json_str, ha='center', va='center', wrap=True) # Remove axis ticks and labels plt.axis('off') # Save the plot as PNG plt.savefig(output_file, bbox_inches='tight', pad_inches=0) # Example usage sample_json = {"name": "John Doe", "age": 30, "city": "Example City"} json_to_png(sample_json, "example2_output.png") print("successfully converted JSON to PNG") Output: successfully converted JSON to PNG ConclusionIn this article, we explored some different approaches to convert JSON data to PNG images using Python. Whether using image manipulation libraries like Pillow, repurposing plotting libraries like Matplotlib, these examples provide flexibility for various use cases. Depending on your specific requirements and preferences, you can choose the approach that best fits your needs when working with JSON data visualization in Python. Comment More infoAdvertise with us Next Article Convert JSON to PNG in Python K kasoti2002 Follow Improve Article Tags : Python Python Programs Python-pil Python-json Python json-programs +1 More Practice Tags : python Similar Reads Python Tutorial - Learn Python Programming Language Python is one of the most popular programming languages. Itâs simple to use, packed with features and supported by a wide range of libraries and frameworks. Its clean syntax makes it beginner-friendly. It'sA high-level language, used in web development, data science, automation, AI and more.Known fo 10 min read Python Interview Questions and Answers Python is the most used language in top companies such as Intel, IBM, NASA, Pixar, Netflix, Facebook, JP Morgan Chase, Spotify and many more because of its simplicity and powerful libraries. To crack their Online Assessment and Interview Rounds as a Python developer, we need to master important Pyth 15+ min read Non-linear Components In electrical circuits, Non-linear Components are electronic devices that need an external power source to operate actively. Non-Linear Components are those that are changed with respect to the voltage and current. Elements that do not follow ohm's law are called Non-linear Components. Non-linear Co 11 min read Python OOPs Concepts Object Oriented Programming is a fundamental concept in Python, empowering developers to build modular, maintainable, and scalable applications. By understanding the core OOP principles (classes, objects, inheritance, encapsulation, polymorphism, and abstraction), programmers can leverage the full p 11 min read Python Projects - Beginner to Advanced Python is one of the most popular programming languages due to its simplicity, versatility, and supportive community. Whether youâre a beginner eager to learn the basics or an experienced programmer looking to challenge your skills, there are countless Python projects to help you grow.Hereâs a list 10 min read Python Exercise with Practice Questions and Solutions Python Exercise for Beginner: Practice makes perfect in everything, and this is especially true when learning Python. If you're a beginner, regularly practicing Python exercises will build your confidence and sharpen your skills. To help you improve, try these Python exercises with solutions to test 9 min read Python Programs Practice with Python program examples is always a good choice to scale up your logical understanding and programming skills and this article will provide you with the best sets of Python code examples.The below Python section contains a wide collection of Python programming examples. These Python co 11 min read Spring Boot Tutorial Spring Boot is a Java framework that makes it easier to create and run Java applications. It simplifies the configuration and setup process, allowing developers to focus more on writing code for their applications. This Spring Boot Tutorial is a comprehensive guide that covers both basic and advance 10 min read Python Introduction Python was created by Guido van Rossum in 1991 and further developed by the Python Software Foundation. It was designed with focus on code readability and its syntax allows us to express concepts in fewer lines of code.Key Features of PythonPythonâs simple and readable syntax makes it beginner-frien 3 min read Python Data Types Python Data types are the classification or categorization of data items. It represents the kind of value that tells what operations can be performed on a particular data. Since everything is an object in Python programming, Python data types are classes and variables are instances (objects) of thes 9 min read Like