def crop_and_upload_image_to_popit(self, image_filename, crop_bounds, moderator_why_allowed, make_primary): original = PillowImage.open(image_filename) # 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_extra = PersonExtra.objects.get(base__id=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) + '.png' source = _( 'Uploaded by {uploaded_by}: Approved from photo moderation queue' ).format(uploaded_by=self.queued_image.user.username) ImageExtra.objects.create_from_file( ntf.name, join('images', filename), base_kwargs={ 'source': source, 'is_primary': make_primary, 'content_object': person_extra, }, extra_kwargs={ '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'), }, )
def crop_and_upload_image_to_popit(self, image_filename, crop_bounds, moderator_why_allowed): original = Image.open(image_filename) cropped = original.crop(crop_bounds) ntf = NamedTemporaryFile(delete=False) cropped.save(ntf.name, 'PNG') # Upload the image to PopIt... image_upload_url = '{base}persons/{person_id}/image'.format( base=self.get_base_url(), person_id=self.queued_image.popit_person_id) data = { 'md5sum': get_file_md5sum(ntf.name), 'user_why_allowed': self.queued_image.why_allowed, 'user_justification_for_use': self.queued_image.justification_for_use, 'moderator_why_allowed': moderator_why_allowed, 'mime_type': 'image/png', 'notes': 'Approved from photo moderation queue', 'uploaded_by_user': self.queued_image.user.username, } with open(ntf.name) as f: requests.post(image_upload_url, data=data, files={'image': f.read()}, headers={'APIKey': self.api.api_key}) # Remove the cropped temporary image file: os.remove(ntf.name)
def crop_and_upload_image_to_popit(self, image_filename, crop_bounds, moderator_why_allowed, make_primary): original = Image.open(image_filename) # Some uploaded images are CYMK, which gives you an error when # you try to write them as PNG, so convert to RGB: original = original.convert('RGB') cropped = original.crop(crop_bounds) ntf = NamedTemporaryFile(delete=False) cropped.save(ntf.name, 'PNG') # Upload the image to PopIt... person_id = self.queued_image.popit_person_id person = PopItPerson.create_from_popit(self.api, person_id) image_upload_url = '{base}persons/{person_id}/image'.format( base=get_base_url(), person_id=person_id) data = { 'md5sum': get_file_md5sum(ntf.name), 'user_why_allowed': self.queued_image.why_allowed, 'user_justification_for_use': self.queued_image.justification_for_use, 'moderator_why_allowed': moderator_why_allowed, 'mime_type': 'image/png', 'notes': _('Approved from photo moderation queue'), 'uploaded_by_user': self.queued_image.user.username, 'created': None, } if make_primary: data['index'] = 'first' with open(ntf.name) as f: requests.post(image_upload_url, data=data, files={'image': f.read()}, headers={'APIKey': self.api.api_key}) person.invalidate_cache_entries() # Remove the cropped temporary image file: os.remove(ntf.name)
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 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_extra = PersonExtra.objects.get(base__id=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) + '.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) ImageExtra.objects.create_from_file( ntf.name, join('images', filename), base_kwargs={ 'source': source, 'is_primary': make_primary, 'content_object': person_extra, }, extra_kwargs={ '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'), }, )
def crop_and_upload_image_to_popit(self, image_filename, crop_bounds, moderator_why_allowed, make_primary): original = Image.open(image_filename) # 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). original = original.convert('RGBA') cropped = original.crop(crop_bounds) ntf = NamedTemporaryFile(delete=False) cropped.save(ntf.name, 'PNG') # Upload the image to PopIt... person_id = self.queued_image.popit_person_id person = PopItPerson.create_from_popit(self.api, person_id) image_upload_url = '{base}persons/{person_id}/image'.format( base=get_base_url(), person_id=person_id ) data = { 'md5sum': get_file_md5sum(ntf.name), 'user_why_allowed': self.queued_image.why_allowed, 'user_justification_for_use': self.queued_image.justification_for_use, 'moderator_why_allowed': moderator_why_allowed, 'mime_type': 'image/png', 'notes': _('Approved from photo moderation queue'), 'uploaded_by_user': self.queued_image.user.username, 'created': None, } if make_primary: data['index'] = 'first' with open(ntf.name) as f: requests.post( image_upload_url, data=data, files={'image': f.read()}, headers={'APIKey': self.api.api_key} ) person.invalidate_cache_entries() # Remove the cropped temporary image file: os.remove(ntf.name)