98 lines
3.0 KiB
Python
98 lines
3.0 KiB
Python
import os
|
|
from PIL import Image
|
|
|
|
src_path = "/home/maxi/Projekt/wtc.waiswuaschd.de/app/static/img/chapters/source_chapters.jpg"
|
|
out_dir = "/home/maxi/Projekt/wtc.waiswuaschd.de/app/static/img/chapters"
|
|
im = Image.open(src_path).convert("RGBA")
|
|
w, h = im.size
|
|
|
|
def make_transparent(sub_img):
|
|
sub_img = sub_img.convert("RGBA")
|
|
data = sub_img.getdata()
|
|
new_data = []
|
|
for item in data:
|
|
r, g, b, a = item
|
|
lum = 0.299 * r + 0.587 * g + 0.114 * b
|
|
if lum >= 248:
|
|
new_data.append((r, g, b, 0))
|
|
elif lum <= 200:
|
|
new_data.append((r, g, b, 255))
|
|
else:
|
|
alpha = int(255 * (248 - lum) / (248 - 200))
|
|
new_data.append((r, g, b, alpha))
|
|
|
|
res = Image.new("RGBA", sub_img.size)
|
|
res.putdata(new_data)
|
|
|
|
bbox = res.getbbox()
|
|
if bbox:
|
|
px1 = max(0, bbox[0] - 8)
|
|
py1 = max(0, bbox[1] - 8)
|
|
px2 = min(res.width, bbox[2] + 8)
|
|
py2 = min(res.height, bbox[3] + 8)
|
|
res = res.crop((px1, py1, px2, py2))
|
|
return res
|
|
|
|
chapters_data = []
|
|
|
|
# 2. Traditionell & Basics
|
|
sub2 = im.crop((25, 80, 308, 262))
|
|
for y in range(sub2.height):
|
|
for x in range(sub2.width):
|
|
actual_x = 25 + x
|
|
actual_y = 80 + y
|
|
if actual_x > 268 and actual_y > 165:
|
|
sub2.putpixel((x, y), (255, 255, 255, 255))
|
|
chapters_data.append((2, "traditionell_basics", sub2))
|
|
|
|
# 3. Pasta & Nudelgerichte
|
|
sub3 = im.crop((270, 20, 518, 270))
|
|
for y in range(sub3.height):
|
|
for x in range(sub3.width):
|
|
actual_x = 270 + x
|
|
actual_y = 20 + y
|
|
if actual_x < 308 and actual_y < 165:
|
|
sub3.putpixel((x, y), (255, 255, 255, 255))
|
|
chapters_data.append((3, "pasta_nudelgerichte", sub3))
|
|
|
|
# 4. Kartoffeln
|
|
sub4 = im.crop((515, 20, 745, 270))
|
|
chapters_data.append((4, "kartoffeln", sub4))
|
|
|
|
# 5. Reis & Getreide
|
|
sub5 = im.crop((40, 315, 270, 505))
|
|
chapters_data.append((5, "reis_getreide", sub5))
|
|
|
|
# 6. Teig & Gebackenes
|
|
sub6 = im.crop((270, 330, 520, 536))
|
|
chapters_data.append((6, "teig_gebackenes", sub6))
|
|
|
|
# 7. Asiatische Küche: bowl and chopsticks within [45, 580, 292, 725]
|
|
sub7 = im.crop((45, 580, 292, 725))
|
|
chapters_data.append((7, "asiatische_kueche", sub7))
|
|
|
|
# 8. Süßspeisen: starts at y=568 (below label 6)
|
|
sub8 = im.crop((460, 568, 750, 725))
|
|
chapters_data.append((8, "suessspeisen", sub8))
|
|
|
|
# 9. Desserts
|
|
sub9 = im.crop((30, 765, 258, 938))
|
|
chapters_data.append((9, "desserts", sub9))
|
|
|
|
# 10. Kuchen & Gebäck
|
|
sub10 = im.crop((265, 745, 510, 938))
|
|
chapters_data.append((10, "kuchen_gebaeck", sub10))
|
|
|
|
# 11. Plätzchen: starts at y=774 (below label 8 descender!)
|
|
sub11 = im.crop((515, 774, 745, 938))
|
|
chapters_data.append((11, "plaetzchen", sub11))
|
|
|
|
for num, name, img in chapters_data:
|
|
final_img = make_transparent(img)
|
|
filename = f"chapter_{num}.png"
|
|
filepath = os.path.join(out_dir, filename)
|
|
final_img.save(filepath, "PNG")
|
|
print(f"Saved Chapter {num:2d} ({name}) -> {filename} ({final_img.width}x{final_img.height})")
|
|
|
|
print("All 10 chapters extracted with 100% purity!")
|