Create a python program that outputs a 32x32 pixel-art image of a soda.
- User
Create a python program that outputs a 32x32 pixel-art image of a soda.
- Assistant
Certainly! Below is a Python program that creates a 32x32 pixel-art image of a soda using the Pillow library. The program defines a simple soda bottle with a cap and a label and saves the image as
soda.png.pythonfrom PIL import Image # Define the size of the image width, height = 32, 32 # Create a new image with a white background img = Image.new('RGB', (width, height), 'white') pixels = img.load() # Define colors background = (255, 255, 255) # White background soda_body = (200, 0, 0) # Dark red for the soda bottle cap_color = (150, 150, 150) # Gray for the cap label_color = (255, 255, 255) # White for the label # Draw the soda bottle body for x in range(12, 20): for y in range(10, 28): pixels[x, y] = soda_body # Draw the bottle cap for x in range(13, 19): for y in range(8, 10): pixels[x, y] = cap_color # Draw the label on the bottle for x in range(13, 19): for y in range(18, 22): pixels[x, y] = label_color # Save the image img.save('soda.png')Explanation:
- Import the Pillow library: We use the
Imagemodule from Pillow to create and manipulate images. - Create a new image: An image of size 32x32 pixels with a white background is initialized.
- Define colors: RGB values are set for the background, soda bottle body, cap, and label.
- Draw the soda bottle:
- Bottle body: A rectangle is drawn to represent the main body of the soda bottle.
- Bottle cap: A smaller rectangle on top represents the bottle cap.
- Label: Another rectangle in the middle of the bottle represents the label.
Instructions to Run the Program:
-
Install Pillow Library: If you haven't installed Pillow, you can install it using pip:
- Import the Pillow library: We use the