pip install PySimpleGUI
pip install Pillow
python image_manager.py
Last active
November 6, 2021 20:13
-
-
Save nac-39/981ff3714a3799d8b8a1106b27665a06 to your computer and use it in GitHub Desktop.
PySimpleGUIの画像編集スニペット
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
# coding: utf -8 | |
import PySimpleGUI as sg # パッケージの読み込み | |
from PIL import Image, ImageFilter, ImageDraw | |
filename = '' | |
def png_checker(path): | |
sp = path.rsplit('.',1) | |
print(sp) | |
ex = sp[-1] | |
if ex=='jpg' or ex=='jpeg': | |
img = Image.open(path).convert('RGBA').save(sp[0]+'.png') | |
path = sp[0]+'.png' | |
return path | |
def icon_image(img,fin_size): #imgから任意のサイズのアイコン画像を作成 | |
size = min(img.width, img.height) | |
img = img.crop(((img.width/2-size/2),(img.height/2-size/2),(img.width/2+size/2),(img.height/2+size/2)))#正方形にクロップ | |
mask = Image.new("L", (size,size), 0) | |
draw_mask = ImageDraw.Draw(mask) | |
draw_mask.ellipse((0,0, img.width, img.height), fill=255)#マスク用の円 | |
mask = mask.filter(ImageFilter.GaussianBlur(1)) | |
img.putalpha(mask)#mask以外の背景を透明に | |
img.resize((fin_size,fin_size),Image.BICUBIC)#BICUBICを選択するとなんか画質がいいらしい | |
return img | |
def save(img,param,operation): | |
global filename | |
old_filename = filename | |
filename = new_name(filename,operation) | |
img.save(filename, quality=95) | |
image.update(filename) | |
filename_input.update(filename) | |
logger(old_filename,param,filename) | |
def logger(old_name,param,new_name): | |
f = open("./log.txt","a+",encoding="utf-8") | |
f.write("{},{},{}\n".format(old_name,param,new_name)) | |
def gauss(img,num): | |
num = int(num) | |
new_img = img.filter(ImageFilter.GaussianBlur(num)) | |
return(new_img) | |
def new_name(path_to_file,operation):#./hoge.png→./hoge_new.png→./hoge_new_new.pngになります | |
path_to_file = path_to_file.rsplit('/',maxsplit=1) | |
name = path_to_file[-1] | |
path = path_to_file[0] | |
name = name.split('.')[0] | |
return path + "/" + name + "_" + operation + "." + "png" | |
guidance_txt = sg.Text('Click Brouse btn first.\nimage must be png.',key='guidance') | |
filename_input = sg.Input(filename,(30,10),tooltip='ファイルまでのパス',key='filename') | |
browse_btn = sg.FileBrowse(key='browse') | |
load_btn = sg.Button('Load',key='load') | |
gauss_input = sg.Input('4',(20,10),key="g_input") | |
gauss_btn = sg.Button('Gauss',key='gauss') | |
icon_input = sg.Input('100',(20,10),key='i_input') | |
icon_btn = sg.Button('Icon',key='icon') | |
image = sg.Image('',key='image') | |
layout = [[guidance_txt], | |
[filename_input,browse_btn], | |
[load_btn], | |
[gauss_input,gauss_btn], | |
[icon_input,icon_btn], | |
[image]] | |
window = sg.Window('image manager', layout) | |
window.finalize() | |
while True: | |
event, values = window.read() | |
print(event,values) | |
if event=='load': | |
filename = values['filename'] | |
if filename=='': | |
filename_input.update('ファイルパスを入力') | |
else: | |
filename = png_checker(filename) | |
image.update(filename) | |
if event=='gauss': | |
if values['filename']=='': | |
guidance_txt.update('Error: Fill in the path to image.') | |
else: | |
img = Image.open(filename).convert('RGBA') | |
blur = values['g_input'] | |
img = gauss(img,blur) | |
#filename = | |
save(img,blur,'gauss') | |
if event=='icon': | |
if filename=='': | |
guidance_txt.update('Error: Fill in the path to image.') | |
else: | |
img = Image.open(filename).convert('RGBA') | |
size = int(values['i_input']) | |
img = icon_image(img,size) | |
save(img,size,'icon') | |
if event == sg.WIN_CLOSED: | |
break | |
window.close() | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment