def handle(self, *args, **options): self.stdout.write(self.style.SUCCESS("Importing images")) if not options["from_dir"].endswith("/"): options["from_dir"] = options["from_dir"] + "/" for filepath in sorted(glob(options["from_dir"] + "*-90x90.png")): with open(filepath, "rb") as image_file: name = filepath.split("/")[-1][:-10] image = Image(title=name) image.file = ImageFile(file=image_file, name=name + ".png") image.file_size = image.file.size image.file.seek(0) image._set_file_hash(image.file.read()) image.file.seek(0) # Reindex the image to make sure all tags are indexed search_index.insert_or_update_object(image) image.save() image.tags.add("illustration") self.stdout.write( self.style.SUCCESS(f"{image.pk},{image.title}")) self.stdout.write(self.style.SUCCESS("Importing images finished"))
def create_image(self, image_file, title, filename, tag): image = Image(title=title) image.file = ImageFile(file=image_file, name=filename) image.file_size = image.file.size image.file.seek(0) image._set_file_hash(image.file.read()) image.file.seek(0) # Reindex the image to make sure all tags are indexed search_index.insert_or_update_object(image) image.save() image.tags.add(tag) return image
def handle(self, *args, **options): self.stdout.write(self.style.SUCCESS("Importing images")) with open(options["speakers_csv"], "r") as file: for row in DictReader(file): if not row["filename"]: self.stdout.write( self.style.WARNING( f"====> skipping {row['post_name']}, {row['post_title']}" )) continue image_path = os.path.join(options["from_dir"], row["filename"]) with open(image_path, "rb") as image_file: image = Image(title=row["post_title"]) if row["filename"].lower().endswith(".jpg") or row[ "filename"].lower().endswith(".jpeg"): image_filename = f"{row['first_name']} {row['last_name']}.jpg" elif row["filename"].lower().endswith(".png"): image_filename = f"{row['first_name']} {row['last_name']}.png" else: raise ValueError("Unknown file format") image.file = ImageFile(file=image_file, name=image_filename) image.file_size = image.file.size image.file.seek(0) image._set_file_hash(image.file.read()) image.file.seek(0) # Reindex the image to make sure all tags are indexed search_index.insert_or_update_object(image) image.save() image.tags.add("speaker") speaker = Speaker.objects.get(wordpress_id=row["ID"]) speaker.photo = image speaker.save() self.stdout.write( self.style.SUCCESS(f"{image.pk},{image.title}")) self.stdout.write(self.style.SUCCESS("Importing images finished"))
def create_wagtail_image_from_remote(image_url=None, images_folder='original_images', collection=None): basename = os.path.basename(image_url) db_file_field = os.path.join(images_folder, basename).replace('\\', '/') destination_image = os.path.join( settings.MEDIA_ROOT, images_folder, os.path.basename(image_url) ) if collection is None: collection = get_behance_collection() r = requests.get(image_url) if Image.objects.filter(file=db_file_field).count() == 0: if r.status_code == 200: with open(destination_image, 'wb') as f: f.write(r.content) local_image = PILImage.open(destination_image) width, height = local_image.size img = Image() img.file = db_file_field img.title = basename img.width = width img.height = height img.collection = collection img.save() return img else: return Image.objects.get(file=db_file_field) return None