ebook-snipping-tool/utils/utils.py

70 lines
2.7 KiB
Python

import os
import sys
import subprocess
from PIL import Image
def resource_path(relative_path): # for Pyinstaller
if hasattr(sys, '_MEIPASS'):
return os.path.join(sys._MEIPASS, relative_path)
return os.path.join(os.path.abspath("."), relative_path)
def terminate_process(process_name):
try:
print(f"[INFO] Attempting to terminate process: {process_name}")
subprocess.run(f"taskkill /F /IM {process_name}", shell=True, check=True)
print(f"[INFO] Successfully terminated process: {process_name}")
return 0
except subprocess.CalledProcessError as e:
if "128" in str(e): print(f"[WARN] Process '{process_name}' not found or already terminated.")
else: print(f"[ERROR] Failed to terminate process '{process_name}'. Error: {e}")
except Exception as e:
print(f"[ERROR] Unexpected error while terminating '{process_name}'. Error: {e}")
return 1
def start_process(process_name):
try:
print(f"[INFO] Attempting to start process: {process_name}")
subprocess.run(f"start {process_name}", shell=True, check=True)
print(f"[INFO] Successfully started process: {process_name}")
return 0
except FileNotFoundError:
print(f"[ERROR] Process '{process_name}' not found. Please check the path or name.")
except subprocess.CalledProcessError as e:
print(f"[ERROR] Failed to start process '{process_name}'. Error: {e}")
except Exception as e:
print(f"[ERROR] Unexpected error while starting '{process_name}'. Error: {e}")
return 1
"""
Pyinstaller splash screen related
"""
def check_alpha(img):
"""Check if the image contains translucent pixels."""
alpha_channel = img.getchannel('A')
alpha_data = alpha_channel.load()
for y in range(img.height):
for x in range(img.width):
alpha_value = alpha_data[x, y]
if 0 < alpha_value < 255: # Check if translucent
return True
return False
def processing_image(img_path):
img = Image.open(img_path).convert("RGBA") # Ensure image is in RGBA format
print("Contain translucency pixels (Before):", check_alpha(img))
# Remove translucency: Set all non-opaque pixels (alpha != 255) to transparent (alpha = 0)
pixels = img.load()
for y in range(img.height):
for x in range(img.width):
r, g, b, a = pixels[x, y]
if a != 255: # If alpha is not fully opaque
pixels[x, y] = (r, g, b, 0) # Make pixel fully transparent
print("Contain translucency pixels (After):", check_alpha(img))
# Save the modified image
output_path = img_path.replace(".png", "_transparent.png")
img.save(output_path, "PNG")
print(f"Processed image saved to {output_path}")
return output_path