def crop_and_upload_image_to_popit( self, image_file, crop_bounds, moderator_why_allowed, make_primary ): original = PillowImage.open(image_file) # Some uploaded images are CYMK, which gives you an error when # you try to write them as PNG, so convert to RGBA (this is # RGBA rather than RGB so that any alpha channel (transparency) # is preserved). person_id = self.queued_image.person.id person = Person.objects.get(pk=person_id) original = original.convert("RGBA") cropped = original.crop(crop_bounds) ntf = NamedTemporaryFile(delete=False) cropped.save(ntf.name, "PNG") md5sum = get_file_md5sum(ntf.name) filename = str(person_id) + "-" + str(uuid.uuid4()) + ".png" if self.queued_image.user: uploaded_by = self.queued_image.user.username else: uploaded_by = "a script" source = "Uploaded by {uploaded_by}: Approved from photo moderation queue".format( uploaded_by=uploaded_by ) if make_primary: # Unset the is_primary flag on all other images PersonImage.objects.filter(person_id=person_id).update( is_primary=False ) PersonImage.objects.create_from_file( ntf.name, join("images", filename), defaults={ "person": person, "source": source, "is_primary": make_primary, "md5sum": md5sum, "uploading_user": self.queued_image.user, "user_notes": self.queued_image.justification_for_use, "copyright": moderator_why_allowed, "user_copyright": self.queued_image.why_allowed, "notes": "Approved from photo moderation queue", }, ) if make_primary: sorl_delete(person.primary_image.file, delete_file=False) # Update the last modified date, so this is picked up # as a recent edit by API consumers person.save()
def _invertcolors(self, id): from django.http.response import HttpResponse photo = Photo.objects.filter(pk=id.split('/')[0]).first() if photo: photo_path = settings.MEDIA_ROOT + '/' + str(photo.image) img = Image.open(photo_path) inverted_grayscale_image = ImageOps.invert(img).convert('L') inverted_grayscale_image.save(photo_path) photo.invert = not photo.invert sorl_delete(photo.image, delete_file=False) photo.light_save() return HttpResponse(u'Photo inverted!') return HttpResponse(u'Failed to invert photo!')