Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add licensing metadata in images uploaded from reports #173

Open
epou opened this issue Nov 21, 2023 · 0 comments
Open

Add licensing metadata in images uploaded from reports #173

epou opened this issue Nov 21, 2023 · 0 comments

Comments

@epou
Copy link
Member

epou commented Nov 21, 2023

CC BY 4.0 Deed (https://creativecommons.org/licenses/by/4.0/)

Using EXIF and/or IPTC metadata.

Using exiftool:

from PIL import Image

def add_metadata(image_path, metadata):
    img = Image.open(image_path)
    
    # Convert metadata dictionary to a string
    metadata_string = "\n".join([f"{key}: {value}" for key, value in metadata.items()])
    
    # Add metadata to the image
    img.info["metadata"] = metadata_string
    
    # Save the image with the new metadata
    img.save("output_image.jpg")

# Example usage
image_path = "input_image.jpg"
metadata = {
    "Title": "Your Image Title",
    "Artist": "Your Name",
    "License": "CC BY 4.0 - https://creativecommons.org/licenses/by/4.0/",
    # Add other relevant metadata fields
}

add_metadata(image_path, metadata)

Using piexif:

import piexif
from PIL import Image

def add_copyright_to_exif(input_image_path, output_image_path, copyright_text):
    # Open the image using Pillow (PIL)
    image = Image.open(input_image_path)

    # Get the Exif data from the image
    exif_dict = piexif.load(image.info["exif"])

    # Set the copyright information in the Exif data
    exif_dict["0th"][piexif.ImageIFD.Artist] = copyright_text

    # Save the modified Exif data back to the image
    exif_bytes = piexif.dump(exif_dict)
    image.save(output_image_path, exif=exif_bytes)

# Example usage
input_image_path = "input.jpg"
output_image_path = "output.jpg"
copyright_text = "Your Copyright Text Here"
add_copyright_to_exif(input_image_path, output_image_path, copyright_text)

Using iptcinfo3:

from PIL import Image
import iptcinfo3

def add_iptc_metadata(image_path, metadata):
    # Open the image
    img = Image.open(image_path)

    # Open the IPTC info
    info = iptcinfo3.IPTCInfo(image_path, force=True)

    # Update the IPTC info with metadata
    for key, value in metadata.items():
        info[key] = value

    # Save the IPTC info back to the image
    info.save_as(image_path)

# Example usage
image_path = "your_image.jpg"
metadata = {
    (2, 5): "Your Image Title",
    (2, 80): "Your Name",
    (2, 116): "CC BY 4.0 - https://creativecommons.org/licenses/by/4.0/",
    # Add other relevant IPTC metadata fields
}

add_iptc_metadata(image_path, metadata)

In Django would look like:

class YourImageModel(models.Model):
    image = models.ImageField(upload_to='your_image_directory')

    def add_iptc_metadata(self, metadata):
        # Open the image
        # Open the IPTC info
        # Update the IPTC info with metadata
         # Save the IPTC info back to the image
  
# Example usage in a Django view
def upload_image(request):
    if request.method == 'POST':
        form = YourImageForm(request.POST, request.FILES)
        if form.is_valid():
            # Save the image
            image_instance = form.save()

            # Add IPTC metadata to the image
            metadata = {
                (2, 5): "Your Image Title",
                (2, 80): "Your Name",
                (2, 116): "CC BY 4.0 - https://creativecommons.org/licenses/by/4.0/",
                # Add other relevant IPTC metadata fields
            }
            image_instance.add_iptc_metadata(metadata)

            return redirect('success_page')
    else:
        form = YourImageForm()

    return render(request, 'upload_image.html', {'form': form})

Links:

@epou epou self-assigned this Nov 21, 2023
@epou epou added this to the v2 beta release milestone Nov 21, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

1 participant