Tuesday, November 3, 2020

Linking an Image detection Project to Django

Hi,

I am working on a colour detection project and I am trying to link it to my Django Project, but I don't know where to start.

In my Django project, I have a Post CreateView where users can upload images.

Here is the function that I am trying to link the colours in the image to Post.colors but I don't know how to make it generated directly once the image is uploaded

Here is the models.py:
```
class Post(models.Model):
    title = models.TextField(max_length=100)
    design = models.ImageField(
        blank=False, null=True, upload_to='new designs')
    date_posted = models.DateTimeField(default=timezone.now)
    colors= models.TextField(max_length=10,blank=True, null=True)

    def __str__(self):
        return self.title

    def imagecolors(self, *args, **kwargs):
        img = Image.open(self.design)
        size = w, h = img.size
        data = img.load()

        colors = []
        for x in range(w):
            for y in range(h):
                color = data[x, y]
                hex_color_lower = ''.join([hex(c)[2:].rjust(2, '0') for c in color])
                hex_color = hex_color_lower.upper()
                colors.append(hex_color)

        total = w * h

        color_hex = []
        color_count = []
        color_percent = []

        df = pd.DataFrame()
        for color, count in Counter(colors).items():
            percent = count / total * \
                      100  # Do not make it int. Majority of colors are < 1%, unless you want >= 1%
            if percent > 1:
                color_hex.append(color)
                color_count.append(count)
                color_percent.append(percent)
            Post.colors=color_hex
            print(Post.colors)
```
with the above it showing none in the template:
```
{{ post.colors }}
```

--
You received this message because you are subscribed to the Google Groups "Django users" group.
To unsubscribe from this group and stop receiving emails from it, send an email to django-users+unsubscribe@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/django-users/fbdef2f2-f417-47fb-a967-bcaf3a862ffcn%40googlegroups.com.

No comments:

Post a Comment