Skip to content

Instantly share code, notes, and snippets.

@tomathosauce
Created October 25, 2020 15:39
Show Gist options
  • Save tomathosauce/602fa45a96bcab98b1934935fd4f5270 to your computer and use it in GitHub Desktop.
Save tomathosauce/602fa45a96bcab98b1934935fd4f5270 to your computer and use it in GitHub Desktop.
Create sticker packs for Sticker Maker (app for Whatsapp)
from PIL import Image
import os, argparse, time, glob, zipfile
from shutil import copyfile
parser = argparse.ArgumentParser()
import tkinter as tk
from tkinter import filedialog
import re
root = tk.Tk()
root.withdraw()
def resize_static_no_stretching(image):
image = image.convert('RGBA')
width, height = image.size
if img.height > img.width:
factor = 512 / img.height
else:
factor = 512 / img.width
new_size = (int(round(img.width * factor)), int(round(img.height * factor)))
image = image.resize(new_size, resample=Image.ANTIALIAS)
print("\nResizing to {}".format(new_size))
new_image = Image.new('RGBA', (512, 512), (0, 0, 0, 0))
y = (512 - image.size[1]) // 2
x = (512 - image.size[0]) // 2
new_image.paste(image, (x, y))
return new_image
STICKER_DIMENSIONS = (512,512)
path = os.path.dirname(os.path.realpath(__file__))
stamp = time.time()
var = 500
count = 1000
compressed = []
title = input("Titulo del pack: ")
author = input("Autor del pack: ")
input("Selecciona el icono del pack (presionar ENTER)")
thumbnail = filedialog.askopenfilename()
print(thumbnail)
if not thumbnail:
exit()
input("Selecciona las imagenes (presionar ENTER)")
files = filedialog.askopenfilenames(parent=root,title='Choose a file')
print(files)
if not files:
exit()
input("Selecciona la carpeta (presionar ENTER)")
sub_path = filedialog.askdirectory(parent=root,title='Choose a folder')
if not sub_path:
exit()
sub_path = os.path.join(sub_path, title)
print("Path: {}".format(path))
try:
os.mkdir(sub_path)
if os.path.isdir(sub_path):
to_be_deleted = glob.glob(sub_path)
try:
for fdeleted in to_be_deleted:
os.remove(fdeleted)
except PermissionError as PermError:
print(PermError)
zip = zipfile.ZipFile(os.path.join(sub_path, title + ".wastickers"), 'w')
t_name = str(int(stamp)+count) + ".png"
thumbnail_path = os.path.join(sub_path, t_name)
img = Image.open(thumbnail)
img = img.resize((96,96))
img.save(thumbnail_path , "png")
print(thumbnail, thumbnail_path)
compressed.append(thumbnail_path)
zip.write(thumbnail_path, arcname="\\"+t_name, compress_type = zipfile.ZIP_DEFLATED)
for file in files:
file_lowered = file.lower()
if file_lowered.endswith(('.png', '.jpg', '.jpeg', ".webp", ".gif")):
if file_lowered.endswith((".gif")):
continue
count = count + var
name = str(int(stamp)+count) + ".webp"
s_path = os.path.join(sub_path, name )
f_img = os.path.join(path,file)
img = Image.open(file)
if file_lowered.endswith((".gif")):
img.save(s_path ,"webp", duration=img.info["duration"], save_all=True)
elif file_lowered.endswith((".webp")):
copyfile(file, s_path)
else:
img = resize_static_no_stretching(img)
img.save(s_path , "webp")
print("Saving {} to {}".format( file, s_path))
compressed.append(s_path)
zip.write(s_path, arcname="\\"+name, compress_type = zipfile.ZIP_DEFLATED)
atp = os.path.join(sub_path, "author.txt")
at = open(atp, "w")
at.write(author)
at.close()
tit = os.path.join(sub_path, "title.txt")
ti = open(tit, "w")
ti.write(title)
ti.close()
zip.write(atp , "author.txt", compress_type = zipfile.ZIP_DEFLATED)
zip.write(tit , "title.txt", compress_type = zipfile.ZIP_DEFLATED)
except OSError as error:
print(error)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment