-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathimg_generate.py
49 lines (39 loc) · 1.61 KB
/
img_generate.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
#Program to convert bitmap images into bitmap image blocks for use in the Pico ePaper 7.5" display
from PIL import Image
import os
def batch_process_images(input_folder, output_folder):
# Ensure the output folder exists
if not os.path.exists(input_folder):
os.makedirs(input_folder)
# Get a list of files in the input folder
input_files = os.listdir(input_folder)
# Iterate over the files in the input folder
for filename in input_files:
# Open the image
image = Image.open(os.path.join(input_folder, filename))
# Get the width and height of the image
width, height = image.size
# Calculate the number of 160x160 images that can be created
rows = height // 160
cols = width // 160
# Create the 160x160 images
for i in range(rows):
for j in range(cols):
# Calculate the left, upper, right, and lower coordinates of the image
left = j * 160
upper = i * 160
right = left + 160
lower = upper + 160
# Crop the image
img = image.crop((left, upper, right, lower))
# Save the image
try:
os.mkdir(os.path.join(output_folder+"/"+filename[:-4]))
except:
pass
img.save(os.path.join(output_folder+"/"+filename[:-4], f'image_{i}_{j}.bmp'))
# Specify the input and output folders
input_folder = "output"
output_folder = "bmp"
# Call the batch_process_images function
batch_process_images(input_folder, output_folder)