[pre-commit.ci] auto fixes from pre-commit.com hooks

for more information, see https://pre-commit.ci
This commit is contained in:
pre-commit-ci[bot] 2023-12-09 12:07:11 +00:00
parent 7eb8393c5b
commit 50c1a19a54
4 changed files with 86 additions and 67 deletions

View File

@ -1,11 +1,12 @@
import io import io
import os import os
from PIL import Image
from cryptography.fernet import Fernet from cryptography.fernet import Fernet
from PIL import Image
class ImageEncryption: class ImageEncryption:
def __init__(self, image_path, key_path=None): def __init__(self, image_path, key_path=None):
""" """
클래스를 초기화합니다. 클래스를 초기화합니다.
@ -32,10 +33,10 @@ class ImageEncryption:
""" """
self.image_path = image_path self.image_path = image_path
if key_path is None: if key_path is None:
self.key = Fernet.generate_key() self.key = Fernet.generate_key()
self.image = Image.open(image_path) self.image = Image.open(image_path)
else: else:
with open(key_path, 'r') as key_file: with open(key_path) as key_file:
self.key = key_file.read().encode() self.key = key_file.read().encode()
print(self.key) print(self.key)
self.cipher_suite = Fernet(self.key) self.cipher_suite = Fernet(self.key)
@ -59,21 +60,21 @@ class ImageEncryption:
Reads the image file as bytes and encrypts the data using Fernet. Reads the image file as bytes and encrypts the data using Fernet.
Saves the encrypted data in binary format at the specified path. Saves the encrypted data in binary format at the specified path.
""" """
with open(self.image_path, 'rb') as image_file: with open(self.image_path, "rb") as image_file:
original_image_data = image_file.read() original_image_data = image_file.read()
encrypted_data = self.cipher_suite.encrypt(original_image_data) encrypted_data = self.cipher_suite.encrypt(original_image_data)
with open(savePath, 'wb') as encrypted_image_file: with open(savePath, "wb") as encrypted_image_file:
encrypted_image_file.write(encrypted_data) encrypted_image_file.write(encrypted_data)
# Get the directory of the image file # Get the directory of the image file
directory = os.path.dirname(savePath) directory = os.path.dirname(savePath)
# Create the path for the key file # Create the path for the key file
key_path = os.path.join(directory, 'key.txt') key_path = os.path.join(directory, "key.txt")
with open(key_path, 'w') as key_file: with open(key_path, "w") as key_file:
key_file.write(self.key.decode()) key_file.write(self.key.decode())
#이미지를 복호화 (암호화 해제)하는 경우 savePath에 복호화된 이미지를 저장할 경로를 넣어주면 된다. # 이미지를 복호화 (암호화 해제)하는 경우 savePath에 복호화된 이미지를 저장할 경로를 넣어주면 된다.
def decrypt_image(self, savePath): def decrypt_image(self, savePath):
""" """
이미지를 복호화합니다. 이미지를 복호화합니다.
@ -92,16 +93,15 @@ class ImageEncryption:
Reads the encrypted image file as bytes, decrypts the data using Fernet, Reads the encrypted image file as bytes, decrypts the data using Fernet,
and saves the decrypted image at the specified path. and saves the decrypted image at the specified path.
""" """
with open(self.image_path, 'rb') as encrypted_image_file: with open(self.image_path, "rb") as encrypted_image_file:
encrypted_data = encrypted_image_file.read() encrypted_data = encrypted_image_file.read()
decrypted_data = self.cipher_suite.decrypt(encrypted_data) decrypted_data = self.cipher_suite.decrypt(encrypted_data)
decrypted_image = Image.open(io.BytesIO(decrypted_data)) decrypted_image = Image.open(io.BytesIO(decrypted_data))
if decrypted_image.mode == 'RGBA': if decrypted_image.mode == "RGBA":
decrypted_image = decrypted_image.convert('RGB') decrypted_image = decrypted_image.convert("RGB")
decrypted_image.save(savePath) decrypted_image.save(savePath)
# to be deleted
#to be deleted
def decrypt_show_image(self, savePath): def decrypt_show_image(self, savePath):
""" """
이미지를 복호화하고 복호화된 이미지 파일을 보여줍니다. 이미지를 복호화하고 복호화된 이미지 파일을 보여줍니다.
@ -120,11 +120,11 @@ class ImageEncryption:
Reads the encrypted image file as bytes, decrypts the data using Fernet, Reads the encrypted image file as bytes, decrypts the data using Fernet,
and saves the decrypted image at the specified path. and saves the decrypted image at the specified path.
""" """
with open(self.image_path, 'rb') as encrypted_image_file: with open(self.image_path, "rb") as encrypted_image_file:
encrypted_data = encrypted_image_file.read() encrypted_data = encrypted_image_file.read()
decrypted_data = self.cipher_suite.decrypt(encrypted_data) decrypted_data = self.cipher_suite.decrypt(encrypted_data)
decrypted_image = Image.open(io.BytesIO(decrypted_data)) decrypted_image = Image.open(io.BytesIO(decrypted_data))
if decrypted_image.mode == 'RGBA': if decrypted_image.mode == "RGBA":
decrypted_image = decrypted_image.convert('RGB') decrypted_image = decrypted_image.convert("RGB")
decrypted_image.save(savePath) decrypted_image.save(savePath)
Image.open(savePath).show() Image.open(savePath).show()

View File

@ -1,53 +1,68 @@
import math import math
import Image, ImageDraw,ImageFilter
import Image
import ImageDraw
import ImageFilter
class Gradation(ImageFilter): class Gradation(ImageFilter):
name = "Gradation"
name ="Gradation" def __init__(self, start_color, end_color, grad_dir="row"):
self.grad1 = start_color
self.grad2 = end_color
self.shape = grad_dir
def __init__(self,start_color,end_color,grad_dir='row'): def apply_gradation_filter(self, image):
self.grad1=start_color
self.grad2=end_color
self.shape=grad_dir
def apply_gradation_filter(self,image):
# 그라데이션 필터 적용 # 그라데이션 필터 적용
width, height = image.size width, height = image.size
if self.shape == 'row' or self.shape == 'col': if self.shape == "row" or self.shape == "col":
grad_image = self.visual_gradation(width, height) grad_image = self.visual_gradation(width, height)
elif self.shape == 'circular': elif self.shape == "circular":
grad_image = self.circular_gradation(width, height, self.grad1, self.grad2) grad_image = self.circular_gradation(width, height, self.grad1, self.grad2)
else: else:
raise ValueError("Invalid shape. Use 'row', 'col', or 'circular'.") raise ValueError("Invalid shape. Use 'row', 'col', or 'circular'.")
# 원본 이미지와 그라데이션 이미지를 합침 # 원본 이미지와 그라데이션 이미지를 합침
blended_image = Image.blend(image.convert('RGB'), grad_image, 0.5) blended_image = Image.blend(image.convert("RGB"), grad_image, 0.5)
blended_image = blended_image.convert('RGB') blended_image = blended_image.convert("RGB")
return blended_image return blended_image
def visual_gradation(self,width, height): def visual_gradation(self, width, height):
image = Image.new("RGB", (width, height)) image = Image.new("RGB", (width, height))
# 이미지 Draw 객체 생성 # 이미지 Draw 객체 생성
draw = ImageDraw.Draw(image) draw = ImageDraw.Draw(image)
if self.shape == 'col': if self.shape == "col":
for x in range(width): for x in range(width):
# 색상 그라데이션 계산 # 색상 그라데이션 계산
red = int(self.grad1[0] * (1.0 - x / width) + self.grad2[0] * (x / width)) red = int(
green = int(self.grad1[1] * (1.0 - x / width) + self.grad2[1] * (x / width)) self.grad1[0] * (1.0 - x / width) + self.grad2[0] * (x / width)
blue = int(self.grad1[2] * (1.0 - x / width) + self.grad2[2] * (x / width)) )
green = int(
self.grad1[1] * (1.0 - x / width) + self.grad2[1] * (x / width)
)
blue = int(
self.grad1[2] * (1.0 - x / width) + self.grad2[2] * (x / width)
)
# 현재 열에 대한 선 그리기 # 현재 열에 대한 선 그리기
draw.line([(x, 0), (x, height)], fill=(red, green, blue)) draw.line([(x, 0), (x, height)], fill=(red, green, blue))
elif self.shape == 'row': elif self.shape == "row":
for y in range(height): for y in range(height):
# 색상 그라데이션 계산 # 색상 그라데이션 계산
red = int(self.grad1[0] * (1.0 - y / height) + self.grad2[0] * (y / height)) red = int(
green = int(self.grad1[1] * (1.0 - y / height) + self.grad2[1] * (y / height)) self.grad1[0] * (1.0 - y / height) + self.grad2[0] * (y / height)
blue = int(self.grad1[2] * (1.0 - y / height) + self.grad2[2] * (y / height)) )
green = int(
self.grad1[1] * (1.0 - y / height) + self.grad2[1] * (y / height)
)
blue = int(
self.grad1[2] * (1.0 - y / height) + self.grad2[2] * (y / height)
)
# 현재 열에 대한 선 그리기 # 현재 열에 대한 선 그리기
draw.line([(0, y), (width, y)], fill=(red, green, blue)) draw.line([(0, y), (width, y)], fill=(red, green, blue))
@ -56,8 +71,7 @@ class Gradation(ImageFilter):
return image return image
def circular_gradation(self,width, height): def circular_gradation(self, width, height):
# 새로운 이미지 생성 # 새로운 이미지 생성
image = Image.new("RGB", (width, height)) image = Image.new("RGB", (width, height))
@ -72,7 +86,7 @@ class Gradation(ImageFilter):
for y in range(height): for y in range(height):
for x in range(width): for x in range(width):
# 현재 좌표에서 중심 좌표까지의 거리 계산 # 현재 좌표에서 중심 좌표까지의 거리 계산
distance = math.sqrt((x - center_x)**2 + (y - center_y)**2) distance = math.sqrt((x - center_x) ** 2 + (y - center_y) ** 2)
# 반지름보다 크면 거리를 반지름으로 설정 # 반지름보다 크면 거리를 반지름으로 설정
distance = min(distance, radius) distance = min(distance, radius)

View File

@ -1,5 +1,5 @@
from PIL import Image from PIL import Image
import os
def apply_mosaic_filter(image, block_size=20): def apply_mosaic_filter(image, block_size=20):
""" """
@ -20,7 +20,6 @@ def apply_mosaic_filter(image, block_size=20):
for x in range(0, width, block_size): for x in range(0, width, block_size):
for y in range(0, height, block_size): for y in range(0, height, block_size):
# 원본 이미지에서 블록 잘라내기 # 원본 이미지에서 블록 잘라내기
box = (x, y, x + block_size, y + block_size) box = (x, y, x + block_size, y + block_size)
block = image.crop(box) block = image.crop(box)
@ -29,7 +28,7 @@ def apply_mosaic_filter(image, block_size=20):
average_color = ( average_color = (
sum(p[0] for p in block.getdata()) // block_size**2, sum(p[0] for p in block.getdata()) // block_size**2,
sum(p[1] for p in block.getdata()) // block_size**2, sum(p[1] for p in block.getdata()) // block_size**2,
sum(p[2] for p in block.getdata()) // block_size**2 sum(p[2] for p in block.getdata()) // block_size**2,
) )
# 평균 색상으로 새 이미지를 생성하여 필터링된 이미지에 붙여넣기 # 평균 색상으로 새 이미지를 생성하여 필터링된 이미지에 붙여넣기

View File

@ -1,17 +1,22 @@
from PIL import Image, ImageDraw from PIL import Image, ImageDraw
class GradationFilter: class GradationFilter:
def __init__(self, image_path): def __init__(self, image_path):
self.original_image = Image.open(image_path) self.original_image = Image.open(image_path)
def apply_gradation_filter(self, start_color, end_color, input_alpha=0.5): def apply_gradation_filter(self, start_color, end_color, input_alpha=0.5):
alpha_mask = self.original_image.convert("L").point(lambda x: 0 if x == 0 else 255) alpha_mask = self.original_image.convert("L").point(
lambda x: 0 if x == 0 else 255
)
self.original_image.putalpha(alpha_mask) self.original_image.putalpha(alpha_mask)
width, height = self.original_image.size width, height = self.original_image.size
grad_image = self.visual_gradation(width, height, start_color, end_color) grad_image = self.visual_gradation(width, height, start_color, end_color)
result_image = Image.alpha_composite(Image.new("RGBA", self.original_image.size, (0, 0, 0, 0)), grad_image) result_image = Image.alpha_composite(
Image.new("RGBA", self.original_image.size, (0, 0, 0, 0)), grad_image
)
result_image.paste(self.original_image, (0, 0), self.original_image) result_image.paste(self.original_image, (0, 0), self.original_image)
return result_image return result_image
@ -30,7 +35,8 @@ class GradationFilter:
@staticmethod @staticmethod
def hex_to_rgb(hex_color): def hex_to_rgb(hex_color):
return tuple(int(hex_color[i:i+2], 16) for i in (0, 2, 4)) return tuple(int(hex_color[i : i + 2], 16) for i in (0, 2, 4))
try: try:
image_path = "Gradation/60D2151A-A12A-4F89-9D79-D7FD5DBEA4C4.png" image_path = "Gradation/60D2151A-A12A-4F89-9D79-D7FD5DBEA4C4.png"
@ -46,7 +52,7 @@ try:
filtered_image = gradation_filter.apply_gradation_filter(start_color, end_color) filtered_image = gradation_filter.apply_gradation_filter(start_color, end_color)
filtered_image.show() filtered_image.show()
except IOError: except OSError:
print("Error: Not a PNG file.") print("Error: Not a PNG file.")
except Exception as e: except Exception as e:
print(f"An unexpected error occurred: {e}") print(f"An unexpected error occurred: {e}")