39 lines
1.2 KiB
Python
39 lines
1.2 KiB
Python
import os
|
|
from PIL import Image
|
|
|
|
src_path = "/home/maxi/Projekt/wtc.waiswuaschd.de/app/static/img/chapters/source_chapter_1.jpg"
|
|
out_dir = "/home/maxi/Projekt/wtc.waiswuaschd.de/app/static/img/chapters"
|
|
im = Image.open(src_path).convert("RGBA")
|
|
|
|
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
|
|
|
|
final_1 = make_transparent(im)
|
|
out_path = os.path.join(out_dir, "chapter_1.png")
|
|
final_1.save(out_path, "PNG")
|
|
print(f"Saved Chapter 1 -> chapter_1.png ({final_1.width}x{final_1.height})")
|