I need to write python code that has a json input containing a list of meetings (name…
- User
I need to write python code that has a json input containing a list of meetings (name and hour) and output a jpg image listing these meetings.
- Assistant
To achieve this, you'll need to use several Python libraries:
json,Pillowfor creating the image, andmatplotlibfor layout management. Here's a simple example of how you can create a JPG image with a list of meetings from a JSON input.First, make sure to install the required libraries:
bashpip install pillow matplotlibNow, create a Python script (e.g.,
meetings_to_image.py) with the following code:pythonimport json from datetime import datetime import matplotlib.pyplot as plt from matplotlib.ticker import FuncFormatter import pytz from PIL import Image, ImageDraw, ImageFont def format_time(x, pos): hour = int(x) return datetime.fromtimestamp(hour, tz=pytz.UTC).strftime('%H:%M') def create_meetings_image(meetings): # Set up the figure and axis fig, ax = plt.subplots(figsize=(10, 20)) # Set up the grid ax.grid(True, linestyle='-', linewidth=0.5, color='lightgray') # Set up the x and y limits ax.set_xlim(0, len(meetings)) ax.set_ylim(0, 12) # Set up the x and y ticks ax.xaxis.set_major_locator(plt.MaxNLocator(nbins=len(meetings))) ax.yaxis.set_major_locator(plt.MultipleLocator(1)) # Set up the x and y tick labels ax.xaxis.set_major_formatter(FuncFormatter(lambda x, pos: meetings[int(x)]['name'])) ax.yaxis.set_major_formatter(FuncFormatter(format_time)) # Create a bar chart for i, meeting in enumerate(meetings): ax.barh(meeting['hour'], 1, left=0, color='#4CAF50') ax.text(meeting['hour'] + 0.5, meeting['hour'] + 0.5, meeting['name'], ha='center', va='bottom', fontsize=8) # Save the image plt.savefig('meetings.jpg') if __name__ == "__main__": with open('meetings.json') as f: meetings = json.load(f) create_meetings_image(meetings)
Other models, same or similar prompt
Responses were produced in different, uncontrolled contexts (system prompts, settings, prior turns). This is not a benchmark.