Skip to content

Instantly share code, notes, and snippets.

@varunshenoy
Created June 21, 2023 01:28
Show Gist options
  • Save varunshenoy/63054e7a479f256974416ef45a51e6a0 to your computer and use it in GitHub Desktop.
Save varunshenoy/63054e7a479f256974416ef45a51e6a0 to your computer and use it in GitHub Desktop.
PhotoshopGPT: Describe photo edits in natural language (via a prompt) and automatically apply corresponding enhancements using a combination of Instagram filters and PIL functions.
from opendream import opendream
from opendream.layer import Layer, ImageLayer, MaskLayer
import openai
import os
import json
from PIL import Image, ImageEnhance, ImageFilter
import pilgram
@opendream.define_op
def gpt4_editor(image_layer: ImageLayer, prompt: str):
openai.api_key = os.environ["OPENAI_API_KEY"]
system_prompt = """You are a photo editor. Create a set of instructions in Python that edit an image to closely match a prompt in the order they should be applied. The result should be just a list of methods with their parameters.
You can use the following PIL modules: ImageEnhance, ImageFilter
You can also use pilgram with the following filters: _1977, aden, brannan, brooklyn, clarendon, earlybird, gingham, hudson, inkwell, kelvin, lark, lofi, maven, mayfair, moon, nashville, perpetua, reyes, rise, sierra, skyline, slumber, stinson, sutro, toaster, valencia, walden, willow, xpro2
Do not add a blur unless it is asked for.
prompt: make it dark and moody
result: ["img = ImageEnhance.Contrast(img).enhance(1.5)", "img = ImageEnhance.Brightness(img).enhance(0.5)"]"""
# call openai api
response = openai.ChatCompletion.create(
model="gpt-4",
messages=[
{"role": "system", "content": system_prompt},
{"role": "user", "content": "prompt: " + prompt + "\nresult: "},
]
)
# parse response as array
print(response.choices[0].message["content"])
new_operations = json.loads(response.choices[0].message["content"])
new_operations = [n.strip() for n in new_operations]
# apply operations to image
img = image_layer.get_image()
ldict = {'img': img, "Image": Image, "ImageEnhance": ImageEnhance, "ImageFilter": ImageFilter, "pilgram": pilgram}
for operation in new_operations:
exec(operation, globals(), ldict)
# show image
img = ldict["img"]
return Layer(image=img)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment