Skip to content

Commit

Permalink
feat: creates config file
Browse files Browse the repository at this point in the history
  • Loading branch information
netpoe committed May 28, 2024
1 parent 2bc3b94 commit 0ffe9ac
Show file tree
Hide file tree
Showing 2 changed files with 112 additions and 79 deletions.
36 changes: 36 additions & 0 deletions erc721/config.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
from openai import OpenAI
import os
import anthropic
from dotenv import load_dotenv

load_dotenv()

PY_ENV = "prod"

open_ai_client = OpenAI(
api_key=os.getenv("OPENAI_API_KEY"),
)

claude_anthropic_client = anthropic.Anthropic(api_key=os.getenv("ANTHROPICAI_API_KEY"))

src_dir_path = "./files/hellheadz/20240523_0-21"
thumbnails_dir_path = "./files/hellheadz/20240523_0-21/thumbnails"
watermarks_dir_path = "./files/hellheadz/20240523_0-21/watermarked"
token_uris_dir_path = "./files/hellheadz/20240523_0-21"

thumbnail_extension = "_thumbnail"

metadata_file_path = "metadata/hellheadz/20240523_metadata-batch-0-21.json"
metadata_ai_descriptions_file_name = "20240523_metadata-batch-0-21.json"

watermark_text = "LarsKristo"
author_text = "LarsKristo"
generic_description_text = "Larskristo Hellheadz"

token_uris_filename = f"{token_uris_dir_path}/20240523_token-uris-batch-0-21.json"

ipfs_gateway_url = "https://blockchainassetregistry.infura-ipfs.io/ipfs/"

# Get the values of projectId and projectSecret from the environment variables
infura_project_id = os.getenv("INFURA_PROJECT_ID")
infura_project_secret = os.getenv("INFURA_PROJECT_SECRET")
155 changes: 76 additions & 79 deletions erc721/img-to-erc721.py
Original file line number Diff line number Diff line change
@@ -1,53 +1,21 @@
import base64
import os
import json
from openai import OpenAI

import requests
from dotenv import load_dotenv
from PIL import Image
from PIL import ImageFont
from PIL import ImageDraw
import re
import anthropic

PY_ENV = "prod"

# Load the environment variables from the .env file
load_dotenv()

open_ai_client = OpenAI(
api_key=os.getenv("OPENAI_API_KEY"),
)

claude_anthropic_client = anthropic.Anthropic(api_key=os.getenv("ANTHROPICAI_API_KEY"))

src_dir_path = "./files/hellheads/217-to-222-watermarks"
watermarks_dir_path = "./files/hellheads/217-to-222-watermarks"
token_uris_dir_path = "./files/hellheads/token-URIs-217-to-222"

thumbnail_extension = "_thumbnail"

metadata_file_name = "metadata-217-to-222.json"
metadata_ai_descriptions_file_name = "metadata-217-to-222.json"

watermark_text = "@larskristo"
author_text = "@larskristo"
generic_description_text = "Larskristo Hellheadz"

token_uris_filename = f"{token_uris_dir_path}/token-uris.json"

ipfs_gateway_url = "https://blockchainassetregistry.infura-ipfs.io/ipfs/"

# Get the values of projectId and projectSecret from the environment variables
infura_project_id = os.getenv("INFURA_PROJECT_ID")
infura_project_secret = os.getenv("INFURA_PROJECT_SECRET")
from config import *


def set_metadata_token_id_from_0_index():
metadata_list = []

# Read the metadata from the JSON file
with open(metadata_file_name, "r") as file:
with open(metadata_file_path, "r") as file:
existing_json_metadata = json.load(file)

# Loop through each metadata object
Expand Down Expand Up @@ -75,68 +43,65 @@ def create_token_uri_batches():
print(f"create_token_uri_batches: {i}")


def create_token_uri_from_metadata_object():
def create_token_uri_from_metadata_object(metadata_filename, to_filename_path):
token_uris = []

# Read the metadata from the JSON file
with open(metadata_file_name, "r") as file:
with open(metadata_filename, "r") as file:
metadata_list = json.load(file)

# Loop through each metadata object
for metadata in metadata_list:
# Create a temporary JSON file for the metadata object
temp_file_path = "./temp_metadata.json"
with open(temp_file_path, "w") as temp_file:
json.dump(metadata, temp_file)

# Upload the temporary JSON file to IPFS
token_uri = upload_file_to_ipfs(temp_file_path)

print(
f"create_token_uri_from_metadata_object.token_uri: {token_uri} for {metadata['id']}"
)

token_uris.append(token_uri)

# Remove the temporary JSON file
os.remove(temp_file_path)

update_json_document(token_uris, token_uris_filename)
update_json_document(token_uris, to_filename_path)


def encode_image(image_path):
with open(image_path, "rb") as image_file:
return base64.b64encode(image_file.read()).decode("utf-8")


def create_img_thumbnails():
# Loop through each file in the directory
for filename in os.listdir(src_dir_path):
# Check if the file is an image file
if filename.lower().endswith(".png") or filename.lower().endswith(".jpg"):
# Open an image file
file_path = os.path.join(src_dir_path, filename)
def is_image_extension(filename):
return filename.lower().endswith(".png") or filename.lower().endswith(".jpg")


def create_img_thumbnails(from_directory_path, to_directory_path):
for filename in os.listdir(from_directory_path):

if is_image_extension(filename):

file_path = os.path.join(from_directory_path, filename)

with Image.open(file_path) as img:
# Specify the size of the thumbnail
size = (187, 187)

img.thumbnail(size)

# get the file extension
file_extension = filename.split(".")[-1]

# change the file name to include _thumbnail
filename = (
filename.split(".")[0] + thumbnail_extension + "." + file_extension
)

# Save the thumbnail
img.save(os.path.join(src_dir_path, filename))
img.save(os.path.join(to_directory_path, filename))


def add_image_watermark():
# Loop through each file in the directory
for filename in os.listdir(src_dir_path):
# Check if the file is an image file
if filename.lower().endswith(".png") or filename.lower().endswith(".jpg"):
if is_image_extension(filename):
print(f"add_image_watermark: {filename}")
# Open an image file
file_path = os.path.join(src_dir_path, filename)
Expand All @@ -145,7 +110,7 @@ def add_image_watermark():
w, h = img.size
font_size = int(w * 0.02)
x, y = w - font_size * 4, h - font_size * 4
font = ImageFont.truetype("PPNeueMachina-InktrapRegular.otf", font_size)
font = ImageFont.truetype("oldenglishtextmt.ttf", font_size)

draw.text(
(x, y), watermark_text, fill=(64, 64, 74), font=font, anchor="ms"
Expand Down Expand Up @@ -182,6 +147,7 @@ def upload_file_to_ipfs(file_path):
if response.status_code == 200:
# Extract the IPFS hash from the response
ipfs_hash = response.json()["Hash"]
print(f"upload_to_ipfs: {file_path}, {ipfs_hash}")
return ipfs_hash
else:
# Handle the error case
Expand Down Expand Up @@ -268,7 +234,7 @@ def extend_image_description_of_metadata_items():
with open(metadata_ai_descriptions_file_name, "r") as final_descriptions_json_file:
existing_json_metadata = json.load(final_descriptions_json_file)

with open(metadata_file_name, "r") as descriptions_json_file:
with open(metadata_file_path, "r") as descriptions_json_file:
json_metadata = json.load(descriptions_json_file)

existing_names = [metadata["name"] for metadata in existing_json_metadata]
Expand Down Expand Up @@ -301,7 +267,7 @@ def append_real_image_urls_to_metadata_file():
metadata_list = []

# Read the metadata from the JSON file
with open(metadata_file_name, "r") as f:
with open(metadata_file_path, "r") as f:
json_metadata = json.load(f)
with open("metadata.json", "r") as f:
unsorted_metadata_json = json.load(f)
Expand All @@ -322,12 +288,12 @@ def append_real_image_urls_to_metadata_file():

metadata_list.append(new_metadata)

update_json_document(metadata_list, metadata_file_name)
update_json_document(metadata_list, metadata_file_path)


def sort_metadata_file():
# Read the metadata from the JSON file
with open(metadata_file_name, "r") as f:
with open(metadata_file_path, "r") as f:
metadata_list = json.load(f)

# Loop through each metadata object
Expand All @@ -345,24 +311,23 @@ def sort_metadata_file():
# Sort the metadata list by the "name" property ascending
sorted_metadata = sorted(metadata_list, key=lambda x: x["id"])

update_json_document(metadata_list, metadata_file_name)
update_json_document(metadata_list, metadata_file_path)

return sorted_metadata


def complete_image_filenames():
with open(metadata_file_name, "r") as f:
with open(metadata_file_path, "r") as f:
metadata_list = json.load(f)

for filename in sorted(os.listdir(watermarks_dir_path)):
if thumbnail_extension not in filename and (
filename.lower().endswith(".png") or filename.lower().endswith(".jpg")
):
if thumbnail_extension not in filename and is_image_extension(filename):

for metadata in metadata_list:
name = metadata["name"]
filename_digit = re.match(r"\d+", filename).group()
token_id = re.match(r"\d+", name).group()

if token_id in filename_digit:
print(f"complete_image_filenames: {filename}:{name}")
with Image.open(os.path.join(watermarks_dir_path, filename)) as img:
Expand All @@ -375,19 +340,18 @@ def complete_image_filenames():
)


def create_metadata_from_image_files():
def create_metadata_from_image_files(dir_path):
counter = 0
total_items = len([filename for filename in os.listdir(watermarks_dir_path)])

with open(metadata_file_name, "r") as metadata_file:
total_items = len([filename for filename in os.listdir(dir_path)])

with open(metadata_file_path, "r") as metadata_file:
existing_json_metadata = json.load(metadata_file)

for filename in sorted(os.listdir(watermarks_dir_path)):
if thumbnail_extension not in filename and (
filename.lower().endswith(".png") or filename.lower().endswith(".jpg")
):
for filename in sorted(os.listdir(dir_path)):
if thumbnail_extension not in filename and is_image_extension(filename):
# Create a variable for the actual file
file_path = os.path.join(watermarks_dir_path, filename)
file_path = os.path.join(dir_path, filename)
# base64_image = encode_image(file_path)

# Upload the file to IPFS and get the IPFS hash
Expand All @@ -407,7 +371,7 @@ def create_metadata_from_image_files():
token_id = ""

thumbnail_filename = name + thumbnail_extension + "." + file_extension
thumbnail_path = os.path.join(watermarks_dir_path, thumbnail_filename)
thumbnail_path = os.path.join(dir_path, thumbnail_filename)
thumbnail_ipfs_hash = upload_file_to_ipfs(thumbnail_path)
print(f"create_metadata.thumbnail_ipfs_hash: {thumbnail_ipfs_hash}")

Expand All @@ -429,7 +393,7 @@ def create_metadata_from_image_files():
counter += 1
print(f"create_metadata: {counter} of {total_items} items")

update_json_document(existing_json_metadata, metadata_file_name)
update_json_document(existing_json_metadata, metadata_file_path)


def update_json_document(data, filename):
Expand All @@ -442,18 +406,51 @@ def update_json_document(data, filename):
)


def update_image_urls_on_existing_metadata_file(
from_directory_path, metadata_filename, metadata_prop_key
):
print(
f"update_image_urls_on_existing_metadata_file: {from_directory_path}, {metadata_filename}, {metadata_prop_key}"
)

with open(metadata_filename, "r") as file:
existing_json_metadata = json.load(file)

for index, existing_metadata in enumerate(existing_json_metadata):
for filename in sorted(os.listdir(from_directory_path)):
if existing_metadata["name"] in filename:
print(
f'update_image_urls_on_existing_metadata_file: {filename}, {existing_metadata["name"]}'
)

file_path = os.path.join(from_directory_path, filename)
image_ipfs_hash = upload_file_to_ipfs(file_path)

existing_json_metadata[index][
metadata_prop_key
] = f"{ipfs_gateway_url}{image_ipfs_hash}"

update_json_document(existing_json_metadata, metadata_filename)


def main():
# sort_metadata_file()
# complete_image_filenames()
# create_metadata_from_image_files()
# create_metadata_from_image_files(watermarks_dir_path)
# update_image_urls_on_existing_metadata_file(
# watermarks_dir_path, metadata_file_path, "image"
# )
# update_image_urls_on_existing_metadata_file(
# thumbnails_dir_path, metadata_file_path, "thumbnail"
# )
# append_real_image_urls_to_metadata_file()
# remove_digits_from_name()
# create_metadata()
create_token_uri_from_metadata_object()
create_token_uri_from_metadata_object(metadata_file_path, token_uris_filename)
# create_token_uri_batches()
# set_metadata_token_id_from_0_index()
# extend_image_description_of_metadata_items()
# create_img_thumbnails()
# create_img_thumbnails(watermarks_dir_path, thumbnails_dir_path)
# add_image_watermark()


Expand Down

0 comments on commit 0ffe9ac

Please sign in to comment.