dithered images
still needs to be integrated with celery, but works for now
This commit is contained in:
@@ -1,5 +1,8 @@
|
||||
from celery import shared_task
|
||||
from .models import Track
|
||||
from django.core.files import File
|
||||
from django.core.files.uploadedfile import InMemoryUploadedFile
|
||||
from django.core.files.base import ContentFile
|
||||
|
||||
import os
|
||||
import dotenv
|
||||
@@ -7,6 +10,8 @@ import dotenv
|
||||
import json
|
||||
from requests import get, post
|
||||
from requests.exceptions import ConnectionError
|
||||
from PIL import Image
|
||||
from io import BytesIO
|
||||
|
||||
dotenv.load_dotenv()
|
||||
|
||||
@@ -52,3 +57,17 @@ def get_banter(id):
|
||||
track.banter_done = True
|
||||
track.save()
|
||||
return f"{track}: {e}"
|
||||
|
||||
@shared_task
|
||||
def get_and_dither_image(url, instance, attribute):
|
||||
"""Expects a url, and a Django model cls with an ImageField attribute"""
|
||||
print(f"Parsing {url} for {instance} with attribute {attribute}")
|
||||
r = get(url)
|
||||
print(r.content)
|
||||
i = Image.open(BytesIO(r.content))
|
||||
i = i.resize(size=(128, 128)).convert("P").quantize(colors=32)
|
||||
buffer = BytesIO()
|
||||
i.save(fp=buffer, format='PNG')
|
||||
file = InMemoryUploadedFile(ContentFile(buffer.getvalue()), None, instance.name + ".png", "image/png", buffer.tell, "utf-8")
|
||||
instance.image = file
|
||||
instance.save()
|
||||
Reference in New Issue
Block a user