Upload 2 files
Browse files- AgPixelGen.json +0 -0
- AgPixelGen.py +90 -0
AgPixelGen.json
ADDED
|
The diff for this file is too large to render.
See raw diff
|
|
|
AgPixelGen.py
ADDED
|
@@ -0,0 +1,90 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import json
|
| 2 |
+
import base64
|
| 3 |
+
import random
|
| 4 |
+
from PIL import Image, ImageEnhance
|
| 5 |
+
from io import BytesIO
|
| 6 |
+
import numpy as np
|
| 7 |
+
import time
|
| 8 |
+
|
| 9 |
+
def load_image_data(json_path='AgPixelGen.json'):
|
| 10 |
+
with open(json_path, 'r') as f:
|
| 11 |
+
return json.load(f)
|
| 12 |
+
|
| 13 |
+
def decode_image_from_base64(base64_string):
|
| 14 |
+
image_bytes = base64.b64decode(base64_string)
|
| 15 |
+
image = Image.open(BytesIO(image_bytes))
|
| 16 |
+
return image
|
| 17 |
+
|
| 18 |
+
def random_transform(image):
|
| 19 |
+
random.seed(time.time())
|
| 20 |
+
transforms = ['color_change', 'noise']
|
| 21 |
+
random.shuffle(transforms)
|
| 22 |
+
|
| 23 |
+
for transform_type in transforms:
|
| 24 |
+
if transform_type == 'color_change':
|
| 25 |
+
image = color_change(image)
|
| 26 |
+
|
| 27 |
+
elif transform_type == 'noise':
|
| 28 |
+
image = add_noise(image)
|
| 29 |
+
|
| 30 |
+
return image
|
| 31 |
+
|
| 32 |
+
def color_change(image):
|
| 33 |
+
enhancer = ImageEnhance.Color(image)
|
| 34 |
+
image = enhancer.enhance(random.uniform(0.2, 3.0))
|
| 35 |
+
|
| 36 |
+
enhancer = ImageEnhance.Brightness(image)
|
| 37 |
+
image = enhancer.enhance(random.uniform(0.5, 2.0))
|
| 38 |
+
|
| 39 |
+
enhancer = ImageEnhance.Contrast(image)
|
| 40 |
+
image = enhancer.enhance(random.uniform(0.5, 2.0))
|
| 41 |
+
|
| 42 |
+
return image
|
| 43 |
+
|
| 44 |
+
def add_noise(image):
|
| 45 |
+
width, height = image.size
|
| 46 |
+
pixels = np.array(image)
|
| 47 |
+
|
| 48 |
+
noise_factor = random.uniform(0.02, 0.1)
|
| 49 |
+
noise = np.random.normal(scale=noise_factor, size=(height, width, 3))
|
| 50 |
+
|
| 51 |
+
noisy_pixels = pixels + noise * 255
|
| 52 |
+
noisy_pixels = np.clip(noisy_pixels, 0, 255).astype(np.uint8)
|
| 53 |
+
|
| 54 |
+
return Image.fromarray(noisy_pixels)
|
| 55 |
+
|
| 56 |
+
def decode_and_transform_image(image_data, key):
|
| 57 |
+
image = decode_image_from_base64(image_data[key])
|
| 58 |
+
transformed_image = random_transform(image)
|
| 59 |
+
return transformed_image
|
| 60 |
+
|
| 61 |
+
def save_image(image, output_path='output.jpg'):
|
| 62 |
+
image.save(output_path)
|
| 63 |
+
|
| 64 |
+
def generate_image(user_prompt=""):
|
| 65 |
+
image_data = load_image_data()
|
| 66 |
+
if user_prompt:
|
| 67 |
+
key = user_prompt.replace(' ', '+')
|
| 68 |
+
else:
|
| 69 |
+
matched_keys = [k for k in image_data.keys() if all(word.lower() in k.lower() for word in user_prompt.split())]
|
| 70 |
+
if matched_keys:
|
| 71 |
+
key = random.choice(matched_keys)
|
| 72 |
+
else:
|
| 73 |
+
key = None
|
| 74 |
+
|
| 75 |
+
if not key or key not in image_data:
|
| 76 |
+
matched_keys = [k for k in image_data.keys() if all(word.lower() in k.lower() for word in user_prompt.split())]
|
| 77 |
+
|
| 78 |
+
if matched_keys:
|
| 79 |
+
key = random.choice(matched_keys)
|
| 80 |
+
else:
|
| 81 |
+
return "<error>"
|
| 82 |
+
|
| 83 |
+
transformed_image = decode_and_transform_image(image_data, key)
|
| 84 |
+
save_image(transformed_image)
|
| 85 |
+
return "Image generated and saved."
|
| 86 |
+
|
| 87 |
+
if __name__ == "__main__":
|
| 88 |
+
user_prompt = input("Enter your prompt: ")
|
| 89 |
+
result = generate_image(user_prompt)
|
| 90 |
+
print(result)
|