This repository has been archived by the owner on Jan 8, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 98
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
refactor + fixes add watermark on images #3
- Loading branch information
Showing
9 changed files
with
127 additions
and
56 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
File renamed without changes.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,4 @@ | ||
"""Watermarking""" | ||
|
||
from .image import watermark_image | ||
from .video import watermark_video |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,44 @@ | ||
import logging | ||
|
||
from PIL import Image | ||
|
||
from telewater import conf | ||
from telewater.utils import cleanup | ||
|
||
|
||
def to_png(file: str) -> str: | ||
im = Image.open(file) | ||
if not file.endswith(".png"): | ||
png_file = f"{file}.png" | ||
im.save(png_file) | ||
else: | ||
png_file = file | ||
|
||
return png_file | ||
|
||
|
||
def _open_image(file: str): | ||
im = Image.open(file) | ||
logging.info( | ||
f"""Opened Image {file} | ||
Image format {im.format} | ||
Mode {im.mode} | ||
Size {im.size} | ||
Height {im.height} | ||
Width {im.width} | ||
""" | ||
) | ||
im = im.convert("RGBA") | ||
|
||
return im | ||
|
||
|
||
def watermark_image(image_file: str, watermark_file: str = "image.png"): | ||
png = to_png(image_file) | ||
im = _open_image(png) | ||
wt = _open_image(watermark_file) | ||
im.alpha_composite(im=wt, dest=(conf.config.x_off, conf.config.y_off)) | ||
output_file = f"watered_{png}" | ||
im.save(output_file) | ||
cleanup(png) | ||
return output_file |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,22 @@ | ||
""" Watermark to videos is applied here.""" | ||
|
||
import logging | ||
import os | ||
|
||
from telewater import conf | ||
|
||
|
||
def watermark_video(video_file: str, watermark_file: str = "image.png"): | ||
"""Apply watermark to video or gifs""" | ||
output_file = f"watered_{video_file}" | ||
command = f'ffmpeg -i {video_file} \ | ||
-i {watermark_file} \ | ||
-an -dn -sn -r {conf.config.frame_rate} \ | ||
-preset {conf.config.preset} \ | ||
-tune zerolatency -tune fastdecode \ | ||
-filter_complex "overlay={conf.config.x_off}:{conf.config.y_off}" \ | ||
{output_file}' | ||
|
||
logging.info(f"Running command {command}") | ||
os.system(command) | ||
return output_file |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters