def bake_obi_image(self, request=None):
        """Bake the OBI JSON badge award assertion into a copy of the original
        badge's image, if one exists."""

        if request:
            base_url = request.build_absolute_uri('/')
        else:
            base_url = 'http://%s' % (Site.objects.get_current().domain,)

        if self.badge.image:
            # Make a duplicate of the badge image
            self.badge.image.open()
            img_copy_fh = StringIO(self.badge.image.file.read())
        else:
            # Make a copy of the default badge image
            img_copy_fh = StringIO(open(DEFAULT_BADGE_IMAGE, 'rb').read())

        try:
            # Try processing the image copy, bail if the image is bad.
            img = Image.open(img_copy_fh)
        except IOError:
            return False

        # Here's where the baking gets done. JSON representation of the OBI
        # assertion gets written into the "openbadges" metadata field
        # see: http://blog.client9.com/2007/08/python-pil-and-png-metadata-take-2.html
        # see: https://github.com/mozilla/openbadges/blob/development/lib/baker.js
        # see: https://github.com/mozilla/openbadges/blob/development/controllers/baker.js
        try:
            from PIL import PngImagePlugin
        except __HOLE__:
            import PngImagePlugin
        meta = PngImagePlugin.PngInfo()

        # TODO: Will need this, if we stop doing hosted assertions
        # assertion = self.as_obi_assertion(request)
        # meta.add_text('openbadges', json.dumps(assertion))
        hosted_assertion_url = '%s%s' % (
            base_url, reverse('badger.award_detail_json',
                              args=(self.badge.slug, self.id)))
        meta.add_text('openbadges', hosted_assertion_url)

        # And, finally save out the baked image.
        new_img = StringIO()
        img.save(new_img, "PNG", pnginfo=meta)
        img_data = new_img.getvalue()
        name_before = self.image.name
        self.image.save('', ContentFile(img_data), False)
        if (self.image.storage.exists(name_before)):
            self.image.storage.delete(name_before)

        # Update the image field with the new image name
        # NOTE: Can't do a full save(), because this gets called in save()
        Award.objects.filter(pk=self.pk).update(image=self.image)

        return True
dataset/ETHPy150Open mozilla/django-badger/badger/models.py/Award.bake_obi_image