Ejemplo n.º 1
0
def transfer_s3_structure_to_db(name, path, parent_directory, is_dir=False):
    if parent_directory is not None:
        par_dir = Directory.get(Directory.key == parent_directory)
    else:
        par_dir = FakeDirectory()
        par_dir.id = None
    if is_dir:
        try:
            Directory.get(Directory.key == path)
            return False
        except DoesNotExist:
            Directory.create(
                name = name,
                key = path,
                parent_directory = par_dir.id
            )
            return True
    else:
        try:
            Image.get(Image.key == path)
            return False
        except DoesNotExist:
            Image.create(
                name = name,
                key = path,
                directory = par_dir.id
            )
            return True
Ejemplo n.º 2
0
    def process_data(data, kigo):
        image_id = urlsafe_b64encode(
            uuid1().bytes).rstrip(b'=').decode('ascii')
        with open(f"static/{image_id}.png", 'wb') as f:
            f.write(data['image']['content'])

        kigo = Kigo.get(kigo=kigo)
        Image.create(image=image_id, kigo=kigo).save()
Ejemplo n.º 3
0
    def post(self):
        # get the info about the uploaded images
        upload_files_front = self.get_file_infos('front_image')
        upload_files_ing = self.get_file_infos('ingredients_image')
        if not len(upload_files_front) or not len(upload_files_ing):
            self.response.out.write('Images not uploaded')
            return

        front_image = upload_files_front[0]
        ingredients_image = upload_files_ing[0]

        # check whether the image has been uploaded
        if not front_image.gs_object_name or not ingredients_image.gs_object_name:
            self.response.out.write(
                'Image info not found. Seems to be upload error!')
            return

        ingredients = self.request.get('ingredients')
        if not ingredients:
            self.response.out.write(
                'You must provide a list of ingredients for the product to be created.'
            )
            return

        user = User.load(users.get_current_user())

        product = Product.create_by_barcode(self.PRODUCT_BARCODE, user)
        product.name = self.PRODUCT_NAME
        product.put()

        Image.create(creator=user,
                     product=product,
                     blob=blobstore.create_gs_key(front_image.gs_object_name),
                     image_type=ImageType.Front,
                     ocr_result='',
                     featured=True)

        Image.create(creator=user,
                     product=product,
                     blob=blobstore.create_gs_key(
                         ingredients_image.gs_object_name),
                     image_type=ImageType.Ingredient,
                     ocr_result=ingredients,
                     featured=False)

        for ingredient_name in ingredients.splitlines():
            ingredient = Ingredient.find_by_name(ingredient_name)
            if ingredient:
                ProductIngredient.add_entry(product, ingredient, user)

        self.redirect('/utils')
Ejemplo n.º 4
0
 def test_blob_creation(self):
     import imghdr, StringIO
     data = 'some_image_data'
     when(StringIO).StringIO(data).thenReturn('data_file')
     when(imghdr).what('data_file').thenReturn('jpeg')
     
     image = Image.create(data=data)
     reader = BlobReader(image.blob_key)
     self.assertEqual(data, reader.read())
     image = image.key.get()
     self.assertEqual('image/jpeg', image.format)
Ejemplo n.º 5
0
 def test_thumbnail_creation(self):        
     data = 'big_image_data'        
     compressed_data = 'cmprsd_data'
     image = Image.create(data=data)
     mock_service_image = mock()
     when(images).Image(blob_key=image.blob_key).thenReturn(mock_service_image)
     when(mock_service_image).execute_transforms(output_encoding = images.JPEG).thenReturn(compressed_data)
     options = Options(dict(width=420))
     thumbnail = Image.thumbnail(image.short_key, options)
     verify(mock_service_image).resize(width=420)
     self.assertEqual(compressed_data, read_blob(thumbnail.blob_key))
     self.assertEqual(thumbnail.key, key.Key(Image, options.key, parent = image.key))
     original_blob_key = thumbnail.blob_key
     thumbnail = Image.thumbnail(image.short_key, options)
     self.assertEqual(original_blob_key, thumbnail.blob_key)
Ejemplo n.º 6
0
 def create_new_image(self, imguri, imgsize):
     global gl_image
     
     """ Creates a new image instance and queries
         metadata thats already included in the file
         resulting in new fragments, metadata and annotations
     """ 
     
     imageob = Image.create(width=imgsize[0], height=imgsize[1],)
     imageob.save()
     imageuri = Imageuri.create(
         image=imageob,
         uri=imguri,
     )
     imageuri.save()
     
     gl_image = imageob
     self.extract_def_metadata(imguri)
     
     return imageob
def save_to_db(line, path, url):

    name = line["pageURL"].split('/')[-2]
    url = url
    path = path
    pixabay_id = line['id']
    created_at = datetime.datetime.now()
    status = "Not Posted"
    tags = line['tags'].replace(",", "")
    caption = create_random_caption(tags)

    image = Image.create(name=name,
                         url=url,
                         path=path,
                         pixabay_id=pixabay_id,
                         created_at=created_at,
                         status=status,
                         tags=tags,
                         caption=caption)
    image.save()
    return image
Ejemplo n.º 8
0
def upload(request):
    image = Image.create(data = request.get('image'))
    return send(dict(image_key = image.short_key))
Ejemplo n.º 9
0
def getImages(post_id, makeThumb=True):

    # Image HTTP content types
    # TODO: Add others -- this should be a standard function
    extensions = {'image/gif': 'gif', 'image/jpeg': 'jpg', 'image/pjpeg': 'jpg', 'image/png': 'png'}

    post = Post.get(Post.id == post_id)
    html = post.content
    feed = post.feed_id

    soup = BS(html)

    img_num = 0

    # Attempt to get images in HTML fragment
    for tag in soup.find_all('img'):
        image_url = tag['src']
        try:
            f = urllib.request.urlopen(image_url) # TODO: Replace with Request
            http = f.info()
            content_type = http.type  # Get HTTP Content-Type to determine file extension
        except urllib.error.HTTPError:
            continue

        # If unrecognised content-type, skip this URL
        if content_type not in extensions:
            continue

        # TODO: Check image size, skip below size limit (avoids saving 1x1 pixel bugs)
        # Reasonable default would be existing thumbnail size

        # Check to see if URL already exists in Image table
        url_num = Image.select().where(Image.url == image_url).count()

        # If not found, add to cache
        if url_num == 0:

            log.info("Found image %s, writing to cache", image_url)

            # Read image data from url
            image_data = f.read()

            # Create feed directory as required
            image_path = '{0}{1}'.format(IMAGE_PATH, str(feed))
            if not os.path.exists(image_path):
                os.makedirs(image_path)

            # Use HTTP content-type to decide extension
            # IMAGE_PATH/[feed_id]/[post_id]_[image_number].[ext]

            image_file = '{0}{1}/img_{2}.{3}'.format(IMAGE_PATH, str(feed), str(img_num), extensions[content_type])

            with open(image_file, 'wb') as img:
                img.write(image_data)
            img.close()

            # Add to Image database table
            Image.create(post_id=post_id, feed_id=feed, url=image_url, path=image_file)

            if makeThumb:

                # Create corresponding thumbnail using Pillow, add to thumbnail cache dir
                thumb_file = '{0}{1}/thumb_{2}.{3}'.format(THUMB_PATH, str(feed), str(img_num), extensions[content_type])

                try:
                    thumb = Im.open(image_file)
                    thumb.thumbnail(THUMB_SIZE)
                    thumb.save(thumb_file, "JPEG")
                except IOError:
                    log.error("Cannot create thumbnail for %s", image_file)

            # increment image counter
            img_num += 1