-
Notifications
You must be signed in to change notification settings - Fork 0
/
convert.py
40 lines (37 loc) · 1.26 KB
/
convert.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
from PIL import Image
import os
indir = 'images'
outdir = 'storage'
fnum = 0
directory = os.fsencode(indir)
for file in os.listdir(directory):
fname = os.fsdecode(file)
print(f'*** procesing file {fname}')
img = Image.open(f'{indir}/{fname}')
w, h = img.size
if (w>320) or (h>240):
print(f'*** scaling down to 320x240')
img.thumbnail((320,240), resample=Image.BICUBIC)
w, h = img.size
if (w<320) or (h<240):
print(f'*** adding margins')
back = Image.new('RGB', (320,240), (0, 0, 0))
back.paste(img,((320-w)//2,(240-h)//2))
img = back
if img.mode != 'P':
print(f'*** changing color depth')
img = img.convert("P", palette=Image.ADAPTIVE, colors=256)
pixels = bytearray(img.getdata())
palette = bytearray(img.getpalette())
cnum = 0
while len(pixels)>0:
with open(f'{outdir}/slide{fnum}.c{cnum}', 'wb') as out_file:
print(f'*** writing chunk {cnum} of {fname}')
out_file.write(pixels[0:0x8000])
pixels = pixels[0x8000:]
cnum += 1
with open(f'{outdir}/slide{fnum}.pal', 'wb') as out_file:
print(f'*** saving palette for {fname}')
out_file.write(palette)
fnum += 1
print(f'*** DONE\n')