Y
Y
Yura Khlyan2016-03-30 12:38:19
Django
Yura Khlyan, 2016-03-30 12:38:19

The transparent background turns black. What's the matter?

Good day.
I need to import images to the site. To do this, I wrote an importer, but when I import an image with a transparent background, it is already displayed on the site with black. If you do it manually, then everything is fine.
Here is the importer code:

def handle(self, data_list, path_to_images):
        CIP = [row[1].strip() for row in data_list[1:]]
        UPC = [row[0].strip() for row in data_list[1:]]
        NAME = [row[2].strip() for row in data_list[1:]]

        EMPTY_FIELDS = ("-", " ")

        num_items = len(NAME)
        for i, name in enumerate(NAME):
            flag = False

            self.logger.info(
                u"   - Importing image({}/{})".format(i + 1, num_items))

            if name in EMPTY_FIELDS:
                self.logger.error(u'      - No path to image')
                continue

            cip_code = CIP[i]
            upc_code = UPC[i]

            if upc_code != u'':
                .....
                # проверка на наличее продукта

            file_path = os.path.join(path_to_images, name)

            new_img = open(file_path, 'rb').read()
            next_index = 0
            for existing in item.images.all():
                next_index = existing.display_order + 1
                try:
                    if new_img == existing.original.read():
                        self.logger.error(u'      - Image allredy imported')
                        flag = True
                        break
                except IOError:
                    # File probably doesn't exist
                    existing.delete()
                    self.logger.error(u'      - Image did not found')

            if flag:
                continue

            new_file = File(open(file_path, 'rb'))
            im = ProductImage(product=item, display_order=next_index)
            im.original.save(name, new_file, save=False)
            im.save()
            self.logger.debug(u'      - Image added to "%s"' % item)

Here is the model from the picture:
class AbstractProductImage(models.Model):
    """
    An image of a product
    """
    product = models.ForeignKey(
        'catalogue.Product', related_name='images', verbose_name=_("Product"))
    original = models.ImageField(
        _("Original"), upload_to=settings.OSCAR_IMAGE_FOLDER, max_length=255)
    ...

What could be the problem? I will be very grateful for your help.

Answer the question

In order to leave comments, you need to log in

1 answer(s)
Y
Yura Khlyan, 2016-03-30
@MAGistr_MTM

The problem was with the picture format. The image was inserted into the template as follows:
Since formatthe thumbnaildefault is 'JPEG', and .JPEG does not support transparency, that's why there were such problems. Everything was solved very simply:

{% thumbnail image.original "440x400" upscale=False format="PNG" as thumb %}

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question