q6 commited on
Commit
bdaff3a
·
1 Parent(s): ee673f5

Scale served webp images to 20 percent

Browse files
Files changed (1) hide show
  1. backend/app.py +8 -6
backend/app.py CHANGED
@@ -54,6 +54,8 @@ SEARCH_PAGE_SIZE = 5
54
  THUMB_MAX_AGE = 1800
55
  THUMB_DIR = Path(tempfile.gettempdir()) / "pixif2-thumbs"
56
  PAGE_URL_CACHE_MAX_AGE = 1800
 
 
57
 
58
  app = FastAPI()
59
  ACTIVE_TASKS = {}
@@ -584,7 +586,8 @@ async def fetch_pixiv_bytes(url, phpsessid):
584
 
585
  async def create_webp(post_id, image_url, phpsessid, page=0, kind="t"):
586
  cleanup_thumbs()
587
- out = THUMB_DIR / f"{post_id}_p{page}_{kind}.webp"
 
588
  if out.exists():
589
  os.utime(out, None)
590
  return out
@@ -592,13 +595,12 @@ async def create_webp(post_id, image_url, phpsessid, page=0, kind="t"):
592
  if not data:
593
  raise HTTPException(status_code=404, detail="image not found")
594
  image = Image.open(io.BytesIO(data))
595
- if kind == "v":
596
- image = image.resize((max(image.width // 2, 1), max(image.height // 2, 1)))
597
- else:
598
- image = image.resize((max(image.width // 3, 1), max(image.height // 3, 1)))
599
  if image.mode not in ("RGB", "RGBA"):
600
  image = image.convert("RGB")
601
- image.save(out, "WEBP", quality=82 if kind == "v" else 72)
602
  return out
603
 
604
 
 
54
  THUMB_MAX_AGE = 1800
55
  THUMB_DIR = Path(tempfile.gettempdir()) / "pixif2-thumbs"
56
  PAGE_URL_CACHE_MAX_AGE = 1800
57
+ WEBP_SCALE = 0.2
58
+ WEBP_QUALITY = 82
59
 
60
  app = FastAPI()
61
  ACTIVE_TASKS = {}
 
586
 
587
  async def create_webp(post_id, image_url, phpsessid, page=0, kind="t"):
588
  cleanup_thumbs()
589
+ scale_tag = int(WEBP_SCALE * 100)
590
+ out = THUMB_DIR / f"{post_id}_p{page}_{kind}{scale_tag}.webp"
591
  if out.exists():
592
  os.utime(out, None)
593
  return out
 
595
  if not data:
596
  raise HTTPException(status_code=404, detail="image not found")
597
  image = Image.open(io.BytesIO(data))
598
+ image = image.resize(
599
+ (max(int(image.width * WEBP_SCALE), 1), max(int(image.height * WEBP_SCALE), 1))
600
+ )
 
601
  if image.mode not in ("RGB", "RGBA"):
602
  image = image.convert("RGB")
603
+ image.save(out, "WEBP", quality=WEBP_QUALITY)
604
  return out
605
 
606