def add_key_frames(request, image_sequence_id): try: image_sequence = ImageSequence.objects.get(pk=image_sequence_id) except ImageSequence.DoesNotExist: raise Http404('Image sequence does not exist') if request.method == 'POST': frame_list = request.POST.getlist('frames') if len(frame_list) == 0: messages.error(request, 'You must select at least 1 frame') else: # Add frames to db for frame_nr in frame_list: # Create image image = Image() image.filename = image_sequence.format.replace('#', str(frame_nr)) image.dataset = image_sequence.dataset image.save() # Create associated key frame key_frame = KeyFrame() key_frame.frame_nr = int(frame_nr) key_frame.image_sequence = image_sequence key_frame.image = image key_frame.save() messages.success(request, 'The image sequence and frames were stored.') return redirect('annotation:datasets') return render(request, 'annotation/add_key_frames.html', {'image_sequence': image_sequence})
def import_images(path, dataset): for root, dirnames, filenames in os.walk(path): for filename in fnmatch.filter(filenames, '*.png'): image = Image() image.filename = os.path.join(root, filename) image.dataset = dataset image.save() print('Saved image ', image.filename)
def from_file(filename, page, fileobj=None, newpath=None, upload=None, **kwargs): if not newpath: newpath = g.images_path # We dont' want drive letters or other path elements showing up here... # Unfortunately, basename only handles path seperators from the # platform it's running on (ie. on unix, it fails to handle windows # paths, so we have to convert the path seperators to match the local # platform. from communityalmanac.lib.helpers import normalize_filename filename = normalize_filename(filename) _, ext = os.path.splitext(filename) mimetype, _ = mimetypes.guess_type(filename) if not mimetype.startswith('image/'): raise ValueError(u'Invalid image file %s' % filename) if upload is not None: upload.make_file() fileobj = upload.file assert fileobj is not None, 'Programming error: file object not set' image_data = fileobj.read() new_uuid = str(uuid.uuid4()) path = os.path.join(newpath, new_uuid) + ext with open(path, 'w') as f: f.write(image_data) image = Image(**kwargs) page.media.append(image) image.order = len(page.media) image.path = path image.create_scales(newpath) image.filename = filename return image
def add_key_frames(request, image_sequence_id): try: image_sequence = ImageSequence.objects.get(pk=image_sequence_id) except ImageSequence.DoesNotExist: raise Http404('Image sequence does not exist') if request.method == 'POST': frame_list = request.POST.getlist('frames') if len(frame_list) == 0: messages.error(request, 'You must select at least 1 frame') else: # Add frames to db for frame_nr in frame_list: # Create image image = Image() image.filename = image_sequence.format.replace( '#', str(frame_nr)) image.dataset = image_sequence.dataset image.save() # Create associated key frame key_frame = KeyFrame() key_frame.frame_nr = int(frame_nr) key_frame.image_sequence = image_sequence key_frame.image = image key_frame.save() messages.success(request, 'The image sequence and frames were stored.') return redirect('annotation:datasets') return render(request, 'annotation/add_key_frames.html', {'image_sequence': image_sequence})
def prepare_image(image: Image) -> Image: allowed_colors = 0xF image_filename = image.filename palette_indexes = set(image.tobytes()) # If the image had transparent rows or columns, crop them out for efficiency border_color = get_transparency(image) solid_top = True solid_left = True image_width_in_pixels, image_height_in_pixels = image.size while solid_top or solid_left: pixel_data = image.convert('RGBA') current_color = border_color for x in range(image_width_in_pixels): # Scan the top and bottom rows of pixels, cropping out the row if it is fully transparent if not current_color == pixel_data.getpixel((x, 0))[3]: solid_top = False if not solid_top: break if x == image_width_in_pixels - 1 and solid_top: # If the top row was fully transparent, crop it out image = image.crop((0, 1, image_width_in_pixels, image_height_in_pixels)) image_width_in_pixels, image_height_in_pixels = image.size for y in range(image_height_in_pixels): # Scan the left and right columns of pixels, cropping out the column if it is fully transparent current_color = border_color if not current_color == pixel_data.getpixel((0, y))[3]: solid_left = False if not solid_left: break if y == image_height_in_pixels - 1 and solid_left: # If the left column was fully transparent, crop it out image = image.crop((1, 0, image_width_in_pixels, image_height_in_pixels)) image_width_in_pixels, image_height_in_pixels = image.size # Tiles are 8x8, so we ensure the image's width and height are divisible by 8 image_width_in_pixels, image_height_in_pixels = image.size if not image_width_in_pixels % 8 == 0: border_width = 8 - (image_width_in_pixels % 8) image = ImageOps.expand(image, border=(0, 0, border_width, 0), fill=border_color) if not image_height_in_pixels % 8 == 0: border_width = 8 - (image_height_in_pixels % 8) image = ImageOps.expand(image, border=(0, 0, 0, border_width), fill=border_color) # If the image has too many colors, convert it into a form with reduced colors if max(palette_indexes) > allowed_colors: if image.mode == "P": # Images already in P mode cannot be converted to P mode to shrink their allowed colors, so # temporarily convert them back to RGB image = image.convert("RGB") image = image.convert("P", palette=Image.ADAPTIVE, colors=allowed_colors) image.filename = image_filename return image
def create(user, image: Image, caption: str, tags: [tag.Tag]): """ Create a post.. :param image: Post's image. :param caption: Caption provided with the post. :param tags: List of this post's tags. :type user: user.User :return: The id of the newly created post. """ files.prepare_directory(user.directory) resized_image = images.resize_image(image, config.IMAGE_DIMENSION) create_post_query = f""" START TRANSACTION; INSERT INTO Post VALUES( NULL, {user.id}, NOW(), "{caption}" ); SELECT LAST_INSERT_ID(); COMMIT; """ iterable = user.cursor.execute(create_post_query, multi=True) # 4 request are made a the same time. The third is the select one. index = 0 result: list = [] for i in iterable: if index == 2: result = i.fetchall() index += 1 post_id = result[0]["LAST_INSERT_ID()"] image.filename = f"{post_id}.png" # Dir where the image is stored. final_image_path = os.path.join(user.directory, image.filename) # Saving image. resized_image.save(final_image_path) for i in tags: try: i.save(post_id) except errors.UserNotExisting: print( f"User not existing. post_id : {post_id}, user_id : {i.user_id}" ) return Post(id=post_id, user=user)