Esempio n. 1
0
def upload_image(id):
    url = IMAGE_ORIGINAL_URL.format(id)
    image = download_file(url)
    image = '{}/{}'.format(Image.src.field.upload_to, image)
    image = Image(src=image, order=99)
    image.save()
    return image
Esempio n. 2
0
    def post(self, request, image_tag):
        print("tags")
        user_id = request.GET.get('user_id', '')
        project_id = request.GET.get('project_id', '')
        uploaded_files = request.FILES.getlist('files')
        image_tag = image_tag.lower()
        user = User.objects.get(id=user_id)
        project = Project.objects.get(id=project_id)
        timestamp = datetime.datetime.now().strftime("%Y-%m-%d-%H:%M:%S") + "-"
        image_folder = settings.MEDIA_ROOT + project.location + "images/" + image_tag.lower(
        ) + "/"
        if not os.path.exists(image_folder):
            os.makedirs(image_folder)
        fs = FileSystemStorage(location=image_folder)

        index = 0
        for image in uploaded_files:
            image_title = timestamp + str(index) + ".jpg"
            fs.save(image_title, image)
            new_image = MyImage(title=image_title,
                                location=project.location + "images/" +
                                image_tag.lower() + "/" + image_title,
                                url=settings.MEDIA_URL_DATADASE +
                                project.location + "images/" +
                                image_tag.lower() + "/" + image_title,
                                description="default",
                                type=image_tag.lower(),
                                user=user,
                                project=project,
                                isTrain=True)
            new_image.save()
            index = index + 1
        all_New_Images = MyImage.objects.filter(title__startswith=timestamp)
        serializer = ImageSerializer(all_New_Images, many=True)
        return JsonResponse(serializer.data, safe=False)
Esempio n. 3
0
def create(request):
    if request.method == "POST":
        if request.user.is_authenticated:
            content = json.loads(request.POST['content'])
            establishment_uuid = uuid.uuid4()
            image_url = 'https://outtolunchstatic.s3.amazonaws.com/media/images/download.png'
            if request.user.elo >= 1000:
                if 'image' in request.FILES:
                    request.FILES['image'].name = uuid.uuid4().__str__()
                    image = Image(file=request.FILES['image'],
                                  type='E',
                                  uuid=establishment_uuid)
                    image.save()
                    image_url = image.file.url
                establishment = Establishment(
                    establishment_id=establishment_uuid,
                    name=content['name'],
                    location=content['location'],
                    description=content['description'],
                    rating=0,
                    owner=request.user,
                    rating_count=0,
                    image=image_url)
                establishment.save()
                return JsonResponse({"success": "success"})
            else:
                return JsonResponse({"error": "You do not have enough elo!"})
        else:
            return JsonResponse(
                {"error": "You are not logged in you silly goose!"})
    else:
        return redirect('/')
Esempio n. 4
0
    def create(self, request, *args, **kwargs):
        serializer = ImageNewSerializer(data=request.data)
        if serializer.is_valid():
            form_url = serializer.validated_data.get('form_url', None)
            form_file = serializer.validated_data.get('form_file', None)
            image_instance = Image()
            if form_url:
                file_name = os.path.basename(
                    urllib.parse.urlparse(form_url).path)
                req = urllib.request.Request(form_url)
                try:
                    response = urllib.request.urlopen(req)
                except urllib.error.HTTPError:
                    return Response(status=status.HTTP_400_BAD_REQUEST,
                                    data={"form_url": "error"})
                django_file = ContentFile(BytesIO(response.read()).getvalue())
                image_instance.file.save(file_name, django_file)

            elif form_file:
                image_instance.file = form_file
            image_instance.save()
            return Response(data=ImageSerializer(image_instance).data,
                            status=status.HTTP_201_CREATED)
        else:
            return Response(serializer.errors,
                            status=status.HTTP_400_BAD_REQUEST)
Esempio n. 5
0
def create(request):
    if request.method == "POST":
        if request.user.is_authenticated:
            content = json.loads(request.POST['content'])
            post_id = uuid.uuid4().__str__()
            image_url = 'https://outtolunchstatic.s3.amazonaws.com/media/images/download.png'
            if 'image' in request.FILES:
                request.FILES['image'].name = uuid.uuid4().__str__()
                image = Image(file=request.FILES['image'], type='P', uuid=post_id)
                image.save()
                image_url = image.file.url
            post_event = Event(event_id=uuid.uuid4().__str__(),
                               type='PostCreatedEvent',
                               timestamp=datetime.now(),
                               data=Post(post_id=post_id,
                                         post_user=content['userId'],
                                         post_rating=content['rating'],
                                         post_subject=content['subject'],
                                         establishment_id=content['establishmentId'],
                                         post_content=content['content'],
                                         post_photo_location=image_url
                                         )
                               )
            post_event.save()
            establishment = Establishment.objects.get(establishment_id=content['establishmentId'])
            establishment.rating_count += 1
            establishment.rating = ((establishment.rating * (establishment.rating_count - 1)) + content[
                'rating']) / establishment.rating_count
            establishment.save()
            return JsonResponse({'success': 'success'})
        else:
            return JsonResponse({'error': 'You have to login first in order to post!'})
    else:
        return redirect('/')
Esempio n. 6
0
def update(request):
    if request.method == 'POST':
        if request.user.is_authenticated:
            if 'image' in request.FILES:
                request.FILES['image'].name = uuid.uuid4().__str__()
                try:
                    image = Image.objects.get(uuid=request.user.id, type='U')
                    image.file = request.FILES['image']
                    image.save()
                    request.user.image = image.file.url
                    request.user.save()
                except Image.DoesNotExist:
                    request.FILES['image'].name = uuid.uuid4().__str__()
                    image = Image(file=request.FILES['image'],
                                  type='U',
                                  uuid=request.user.id)
                    image.save()
                    request.user.image = image.file.url
                    request.user.save()
                return successful_message({})
            else:
                return error_message("No image provided!")
        else:
            return error_message("You must be signed in!")
    else:
        return redirect('/')
Esempio n. 7
0
File: tests.py Progetto: sdob/dsapi
 def test_nothing_explodes(self):
     i = Image(content_object=self.divesite, owner=self.owner)
     i.save()
     # Associated item is set correctly
     self.assertEqual(i.content_object, self.divesite)
     # Divesite knows about this image
     self.assertIn(i, self.divesite.images.all())
 def test_can_created_encrypted_key(self):
     """
     Tests that the DES key encrypts properly
     """
     i = Image(pk=1)
     self.assertEquals('fQj1bFDkn3g', i.generate_encrypted_key())
     i.pk = 2
     self.assertEquals('i76tPgXaavF', i.generate_encrypted_key())
Esempio n. 9
0
 def test_can_created_encrypted_key(self):
     """
     Tests that the DES key encrypts properly
     """
     i = Image(pk=1)
     self.assertEquals('fQj1bFDkn3g', i.generate_encrypted_key())
     i.pk = 2
     self.assertEquals('i76tPgXaavF', i.generate_encrypted_key())
    def test_upload(self):
        i1 = Image()
        i1.title = "aaa"
        i1.thumb = File(open("static/images/comments4.png"))
        i1.save()

        p = Image.objects.get(id=1).thumb.path

        self.failUnless(open(p), 'file not found')
Esempio n. 11
0
def download(request, id):
    remote_file, path = RemoteModel().download(id)

    image = Image()
    image.title = remote_file.title
    image.pathname = path
    image.save_remote()
    
    return HttpResponseRedirect(reverse('local_list'))
Esempio n. 12
0
def importZip(zipfile, galleryName, galleryDescription):
    # Stuff bgins here ...
    imagesObj = imagesFromZip(zipfile)
    imagesList = imagesObj.listImages()
    # Is there images in this zip ?
    if len(imagesList) > 0:
        # regexp for extractiong name ...
        nameRe = re.compile('/([^/]+)$')
        # Create corresponding gallery
        gallery = Gallery()
        gallery.title = galleryName
        gallery.desc = galleryDescription
        gallery.save()  # Must save gallery
        # Now, for each images in it, create an image object and bind it to gallery
        for imgPath in imagesList:
            src = PILImage.open(imgPath)
            m = nameRe.search(imgPath)
            imgObj = Image()
            imgObj.gallery = gallery
            # First, put the original sized picture
            # use StringIO hack to save image to a string ...
            output = StringIO.StringIO()
            src.save(output, 'JPEG')
            imgObj.original.save(m.groups()[0], ContentFile(output.getvalue()))
            output.close()
            # Then, resize it to something like 1600*1200
            (orig_w, orig_h) = src.size
            orig_s = orig_w * orig_h
            new_s = 1600 * 1200
            ratio = orig_s / new_s
            # Resize only if requested size is lower than original
            if ratio > 1:
                (new_w, new_h) = (orig_w / (sqrt(ratio)),
                                  orig_h / (sqrt(ratio)))
                resized = src.resize((int(new_w), int(new_h)))
            else:  # Else, just keep original one ...
                (new_w, new_h) = (orig_w, orig_h)
                resized = src
            output = StringIO.StringIO()
            resized.save(output, 'JPEG')
            imgObj.screen.save(m.groups()[0], ContentFile(output.getvalue()))
            output.close()
            # Finally, get a thumb of 150px*150px, work on resized picture to be faster
            if new_w < new_h:
                maxi = new_w
            else:
                maxi = new_h
            thumb = resized.transform((150, 150), PILImage.EXTENT,
                                      (0, 0, maxi, maxi))
            output = StringIO.StringIO()
            thumb.save(output, 'JPEG')
            imgObj.thumb.save(m.groups()[0], ContentFile(output.getvalue()))
            output.close()
            imgObj.save()
        gallery.save()
        print "done !"
Esempio n. 13
0
def index_view(request):
    # POST
    if request.method == 'POST':
        # list of symbols
        char_list = string.ascii_lowercase + string.digits
        # generate unique id from list
        unique = ''.join(random.sample(char_list, 15))
        alt = ''
        if request.POST['alt']:
            alt = request.POST['alt']
        try:
            img = Image(image_file=request.FILES['image'], unique_id=unique, alt_text=alt)
        except MultiValueDictKeyError:
            msg = 'Пожалуйста, выберите изображение для загрузки!'
            return render(request, 'images/index.html', {'msg': msg})
        img.save()
        # create different link types
        site_url = 'http://picture-sharing.info'
        img.link_page = site_url + '/images/' + img.unique_id
        img.link_direct = site_url + img.image_file.url
        img.link_forums = '[url={0}][img]{0}[/img][/url]'.format(site_url + img.image_file.url)
        img.link_a = '<a href="{0}" target="_blank"><img src="{0}" \
border="0" alt="{1}"></a>'.format(site_url + img.image_file.url, alt)
        img.save()
        msg = 'Файл успешно загружен!'
        height, width = image_resize(img.image_file.height, img.image_file.width)
        content = {'image': img, 'height': height, 'width': width, 'msg': msg}
        return render(request, 'images/image.html', content)

    # GET
    return render(request, 'images/index.html')
Esempio n. 14
0
    def post(self, request):
        print("receiving images....")
        model_id = request.data['model_id']
        uploaded_files = request.FILES.getlist('files')
        user_id = request.GET.get('user_id', '')
        project_id = request.GET.get('project_id', '')
        user = User.objects.get(id=user_id)
        project = Project.objects.get(id=project_id)
        timestamp = datetime.datetime.now().strftime("%Y-%m-%d-%H:%M:%S") + "-"
        # https://stackoverflow.com/questions/13821866/queryset-object-has-no-attribute-name
        # model = Model.objects.filter(title=model_title) return a collection
        model = Model.objects.get(id=model_id)
        model_path = settings.MEDIA_ROOT + model.location
        print(model_path)
        # https://github.com/qubvel/efficientnet/issues/62 to fix ValueError: Unknown activation function:swish
        keras_model = tf.keras.models.load_model(model_path)
        index = 0
        for unPredictedImage in uploaded_files:
            # Get the predict result:
            label_path = settings.MEDIA_ROOT + model.label_location
            print("***********************************************")
            print(label_path)
            print(model_path)
            fr = open(label_path)
            dic = eval(fr.read())
            print(dic)
            label = predictLabel(unPredictedImage, keras_model, label_path,
                                 False)
            # Save the image to file-system according to the label:
            image_path = settings.MEDIA_ROOT + project.location + "images/" + label + "/"
            if not os.path.exists(image_path):
                os.makedirs(image_path)
            fs = FileSystemStorage(location=image_path)
            image_title = timestamp + "-" + str(index) + ".jpg"
            fs.save(image_title, unPredictedImage)

            # Save the image to database:
            new_image = MyImage(title=timestamp + "-" + str(index) + ".jpg",
                                location=project.location + "images/" + label +
                                "/" + image_title,
                                url=settings.MEDIA_URL_DATADASE +
                                project.location + "images/" + label + "/" +
                                image_title,
                                description="default",
                                type=label,
                                user=user,
                                project=project,
                                isTrain=True)
            new_image.save()
            index = index + 1

        predictedImage = MyImage.objects.filter(title__startswith=timestamp)
        serializer = ImageSerializer(predictedImage, many=True)
        return JsonResponse(serializer.data, safe=False)
Esempio n. 15
0
    def handle(self, *args, **options):
        for i in os.listdir(options['directory']):
            im = Image()
            path = options['directory'] + os.sep + i
            with open(path) as f:
              data = f.read()

            # obj.image is the ImageField
            im.image = i
            im.image.save(i, ContentFile(data))
            im.save()
Esempio n. 16
0
def importZip(zipfile,galleryName,galleryDescription):
    # Stuff bgins here ...
    imagesObj = imagesFromZip(zipfile)
    imagesList = imagesObj.listImages()
    # Is there images in this zip ?
    if len(imagesList) > 0:
        # regexp for extractiong name ...
        nameRe = re.compile('/([^/]+)$')
        # Create corresponding gallery
        gallery = Gallery()
        gallery.title = galleryName
        gallery.desc  = galleryDescription
        gallery.save() # Must save gallery 
        # Now, for each images in it, create an image object and bind it to gallery
        for imgPath in imagesList:
            src = PILImage.open(imgPath)
            m = nameRe.search(imgPath)
            imgObj = Image()
            imgObj.gallery = gallery
            # First, put the original sized picture
            # use StringIO hack to save image to a string ...
            output = StringIO.StringIO()
            src.save(output,'JPEG')
            imgObj.original.save(m.groups()[0],ContentFile(output.getvalue()))
            output.close()
            # Then, resize it to something like 1600*1200
            (orig_w,orig_h) = src.size
            orig_s = orig_w * orig_h
            new_s = 1600*1200
            ratio = orig_s/new_s
            # Resize only if requested size is lower than original
            if ratio > 1:
                (new_w, new_h) = ( orig_w/(sqrt(ratio)),orig_h/(sqrt(ratio)) )
                resized = src.resize((int(new_w),int(new_h)))
            else: # Else, just keep original one ...
                (new_w, new_h) = (orig_w, orig_h)
                resized = src
            output = StringIO.StringIO()
            resized.save(output,'JPEG')
            imgObj.screen.save(m.groups()[0],ContentFile(output.getvalue()))
            output.close()
            # Finally, get a thumb of 150px*150px, work on resized picture to be faster
            if new_w < new_h:
                maxi = new_w
            else:
                maxi = new_h
            thumb = resized.transform((150,150),PILImage.EXTENT,(0,0,maxi,maxi))
            output = StringIO.StringIO()
            thumb.save(output,'JPEG')
            imgObj.thumb.save(m.groups()[0],ContentFile(output.getvalue()))
            output.close()
            imgObj.save()
        gallery.save()
        print "done !"
Esempio n. 17
0
def upload_image(request):
    if request.method == "POST":
        context = RequestContext(request)
        auth = Author.objects.get(user=request.user)
        form = ImageUploadForm(request.POST, request.FILES)
        access = request.POST['access']
        if form.is_valid():
            url = "/media/Images/" + str(form.cleaned_data['image']) 
            image = Image(image=form.cleaned_data['image'],url=url, visibility=access, author=auth)
            image.save()
    return HttpResponseRedirect("/images/")
Esempio n. 18
0
 def test_if_no_title_uses_image_name(self):
     """
     If there's no title then the image's name should be the default.
     """
     with open(
             os.path.join(settings.BASE_DIR, 'images', 'tests', 'data',
                          'image.png'), "rb") as image_file:
         image = Image(
             image=SimpleUploadedFile("test.png", image_file.read()))
     image.set_title()
     self.assertEquals('test.png', image.title)
Esempio n. 19
0
    def create(self, request, **kwargs):

        serializer = self.get_serializer(data=request.data)

        latitude = self.request.POST.get('latitude', None)
        longitude = self.request.POST.get('longitude', None)

        if serializer.is_valid():

            state = get_object_or_404(State, pk=1)

            upload_files = request.FILES.getlist('upload_image')

            if latitude is None:
                return Response({"latitude": "Not have latitude"}, status=status.HTTP_400_BAD_REQUEST)

            if longitude is None:
                return Response({"longitude": "Not have longitude"}, status=status.HTTP_400_BAD_REQUEST)

            race = self.request.POST.get('race', None)

            if race is None and request.data['category'] != 1:
                race = get_object_or_404(Race, pk=10000)
                product = serializer.save(seller=request.user.userdetail,location=Point(float(longitude), float(latitude)),active=True, state=state, race=race)
            else:
                product = serializer.save(seller=request.user.userdetail,location=Point(float(longitude), float(latitude)),active=True, state=state)

            session = Session(aws_access_key_id='AKIAJYDV7TEBJS6JWEEQ',
                  aws_secret_access_key='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')

            s3 = session.resource('s3')
            bucket = s3.Bucket('walladog')

            for index in range(len(upload_files)):

                uuid_id = uuid.uuid4()
                up_file = upload_files[index]
                key_file = str(uuid_id) + ".jpeg"
                bucket.put_object(ACL='public-read', Key=key_file, Body=up_file, ContentType='image/jpeg')

                photo_url = "https://s3.amazonaws.com/walladog/" + str(uuid_id) + ".jpeg"
                photo_thumbnail_url = "https://s3.amazonaws.com/walladog/thumbnails/" + str(uuid_id) + ".png"
                image_product = Image(name=str(uuid_id),
                                      product=product,
                                      photo_url=photo_url,
                                      photo_thumbnail_url=photo_thumbnail_url)
                image_product.save()

            serialize_list = ProductsListSerializer(product)

            return Response(serialize_list.data, status=status.HTTP_201_CREATED)

        else:
            return Response(serializer.errors, status=status.HTTP_400_BAD_REQUEST)
Esempio n. 20
0
 def test_if_no_title_uses_image_name(self):
     """
     If there's no title then the image's name should be the default.
     """
     with open(
         os.path.join(settings.BASE_DIR, 'images', 'tests', 'data', 'image.png'), "rb"
     ) as image_file:
         image = Image(
             image=SimpleUploadedFile("test.png", image_file.read())
         )
     image.set_title()
     self.assertEquals('test.png', image.title)
Esempio n. 21
0
 def post(self, request, *args, **kwargs):
     user = request.user
     
     if user.is_authenticated():
         data = request.FILES['Filedata']
         
         image = Image(image=data, thumb=data, owner=user)
         image.save()
         
         return HttpResponse()
     else:
         return HttpResponseForbidden()
Esempio n. 22
0
File: tests.py Progetto: sdob/dsapi
 def test_can_retrieve_user_images(self, mock):
     response = self.client.get(reverse('profile-images', args=[self.owner.profile.id]))
     self.assertEqual(response.status_code, status.HTTP_200_OK)
     self.assertIsInstance(response.data, list)
     self.assertEqual(len(response.data), 0)
     num_images = 5
     for _ in range(num_images):
         i = Image(content_object=self.divesite, owner=self.owner)
         i.save()
     response = self.client.get(reverse('profile-images', args=[self.owner.profile.id]))
     self.assertEqual(response.status_code, status.HTTP_200_OK)
     self.assertIsInstance(response.data, list)
     self.assertEqual(len(response.data), num_images)
Esempio n. 23
0
File: utils.py Progetto: zeyuliu/amt
def bulk_add_images(path):
    try:
        csv_file = open(PROJECT_ROOT + "/" + path)
        file_reader = csv.reader(csv_file, delimiter=',', quotechar='|')
        reader_as_list = list(file_reader)
        for group in reader_as_list:
            image_group = ImageGroup(status=0)
            image_group.save()
            for image_id in group:
                image = Image(image_id=image_id, image_group=image_group)
                image.save()
        csv_file.close()
    except Error as e:
        print("Corrupt file format")
Esempio n. 24
0
def populate_data_base_with_images(apps, schema_editor):

    context = {}

    with open('data/context.json') as file:
        context = json.load(file)

    products = []

    products = context["products"]

    for data in products:
        image = Image(name=data["image"], value=data["image"])
        image.save()
Esempio n. 25
0
    def create(self, validated_data):
        user = None
        request = self.context.get("request")
        if request and hasattr(request, "user"):
            user = request.user

        image = Image(
            comments=validated_data["comments"],
            data=validated_data["data"],
            uploaded_by=user,
        )
        image.save()

        return image
Esempio n. 26
0
def _update_v1(request, name, os, payload):
    """Update API v1."""
    start_time = timezone.now()

    try:
        im = Image.objects.get(name=name)
        im.os = os
        im.save()  # Always update at least the modification time
        images_packages = {
            image_pkg.package.name: image_pkg
            for image_pkg in ImagePackage.objects.filter(image=im)
        }

    except Image.DoesNotExist:
        im = Image(name=name, os=os)
        im.save()
        images_packages = {}
        logger.info("Created image '%s'", name)

    existing_not_updated = []
    existing_upgradable_not_updated = []

    installed = payload.get('installed', [])
    for item in installed:
        _process_installed(im, os, images_packages, existing_not_updated, item)

    logger.info("Tracked %d installed packages for image '%s'", len(installed),
                name)

    uninstalled = payload.get('uninstalled', [])
    for item in uninstalled:
        existing = images_packages.get(item['name'], None)
        if existing is not None:
            existing.delete()

    logger.info("Untracked %d uninstalled packages for image '%s'",
                len(uninstalled), name)

    upgradable = payload.get('upgradable', [])
    for item in upgradable:
        _process_upgradable(im, os, images_packages,
                            existing_upgradable_not_updated, item)

    logger.info("Tracked %d upgradable packages for image '%s'",
                len(upgradable), name)

    if payload['update_type'] == 'full':
        _garbage_collection(im, name, start_time, existing_not_updated,
                            existing_upgradable_not_updated)
Esempio n. 27
0
    def handle(self, *args, **options):
        img_formats = ('.jpg', '.jpeg', '.png')
        imgs_dir = options['directory']

        # path = os.path.join(settings.MEDIA_ROOT, imgs_dir)
        path = os.path.join(settings.BASE_DIR, imgs_dir)
        file_list = os.listdir(path)

        if options['del']:
            Image.objects.all().delete()

        for f in file_list:
            if f.endswith(img_formats):
                image = Image(name=f, img=f)
                image.save()
Esempio n. 28
0
def create_account(request):
    if request.method == 'POST':
        content = json.loads(request.POST['content'])
        user_id = uuid.uuid4()
        image_url = 'https://outtolunchstatic.s3.amazonaws.com/media/images/download.png'
        if 'image' in request.FILES:
            request.FILES['image'].name = uuid.uuid4().__str__()
            image = Image(file=request.FILES['image'], type='U', uuid=user_id)
            image.save()
            image_url = image.file.url
        return register_user(user_id, content['username'], content['password'],
                             content['email'], content['FName'],
                             content['LName'], image_url)
    else:
        return redirect('/')
Esempio n. 29
0
    def create(self, request, *args, **kwargs):

        image = Image()
        image.title = request.data['title']
        image.format = request.data['format']
        image.file = request.data['file']
        image.date_created = timezone.now()
        image.date_updated = timezone.now()
        #return super().create(request, *args, **kwargs)
        image.save()
        return Response({"message": "Image entry successful"},
                        status=status.HTTP_201_CREATED)
Esempio n. 30
0
    def get_segmentation_crop(self, annotation_segmentation_id):
        '''
        return pil image object by annotation_segmentation_id
        '''
        sg = Annotation.segmentation_objects.get(id=annotation_segmentation_id)

        if sg.segmentation:
            mask = np.zeros([[sg.image.height, sg.image.width], count],
                            dtype=np.uint8)  # wrong order?
            for i, landmarks in enumerate(sg.segmentation):
                img = Image.new('L', (sg.image.width, sg.image.height), 0)
                for landmark in landmarks:
                    ImageDraw.Draw(img).polygon(landmark, outline=0, fill=1)
                mask[:, :, i] = np.array(img)
        elif sg.mask:
            rle_struct = [{'counts': sg.mask, 'size': [sg.height, sg.width]}]

            decoded_mask = mask.decode(rle_struct)
        mask = np.squeeze(decoded_mask)

        mask_image = PImage.fromarray(mask)
        mat = np.reshape(mask_image, (sg.height, sg.width))

        overlay = PImage.fromarray(np.uint8(mat * 255), 'L')

        return overlay
Esempio n. 31
0
    def setUp(self):
        self.category = Category(name="Test cat")
        self.category.save_category()
        self.location = Location(name="Test Loc")
        self.location.save_location()

        self.image = Image(
            name="Test Img",
            location_id=self.location,
            category_id=self.category,
            image=SimpleUploadedFile(
                name='image_test.jpg',
                content=open(
                    'photo_gallery/static/images/default_location.jpg',
                    'rb').read(),
                content_type='image/jpeg'))
Esempio n. 32
0
def update_picture_for_candidate(candidate_data, cache_directory, **options):
    picture_intro = 'Picture from the IEBC API for candidate'
    candidate_code = candidate_data['code']
    filename = os.path.join(cache_directory,
                            "candidate-%s.jpg" % (candidate_code, ))
    if not os.path.exists(filename):
        image_url = candidate_data['picture']
        r = requests.get(image_url)
        if r.status_code == 200:
            with open(filename, 'w') as fp:
                fp.write(r.content)
    # If that image now exists, use it:
    if os.path.exists(filename):
        # Find the position from the candidate code, so we can get the right person:
        positions = Position.objects.filter(
            external_id=candidate_code).currently_active()
        if not positions:
            print "#### Missing position for:", candidate_code
        elif len(positions) > 1:
            print "#### Multiple positions for:", candidate_code
        else:
            person = positions[0].person
            if options['commit']:
                # Remove old IEBC images for that person:
                person.images.filter(source__startswith=picture_intro).delete()
                # And now create the new one:
                new_image = Image(content_object=person,
                                  source="%s %s" %
                                  (picture_intro, candidate_code))
                with open(filename) as fp:
                    new_image.image.save(
                        name="%s-%s.jpg" %
                        (candidate_code, file_mtime_iso8601(filename)),
                        content=ContentFile(fp.read()))
Esempio n. 33
0
class TestImage(object):

    def setup(self):
	self.image = Image(title="Hellow World",
                           description="My first test image. Yay!",
                           filename="test.png")


    def test_basic_save(self):
        self.image.save()

    def test_image_url(self):
        nt.assert_equal(self.image.image_url(), "/media/test.png")

    def test_thumb_url(self):
        nt.assert_equal(self.image.thumb_url(), "/media/test_thumbnail.png")
Esempio n. 34
0
    def filter_images(self, request, *args, **kwargs):
        query = request.GET.get('q')
        if query:
            if self.search_fields is None:
                self.search_fields = ('keywords', )
            lookups = ["%s__icontains" % field for field in self.search_fields]
            bits = query.split()

            site = None
            if request.session[CURRENT_SITE_FILTER] != '':
                site = Site.query().get(
                    pk=request.session[CURRENT_SITE_FILTER])

            queryset = Image.query()
            for bit in bits:
                if site and WIDGET_SITE_SPECIFIC_IMAGES:
                    queryset = queryset.filter(pk__in=[
                        str(pk)
                        for pk in site['images'].values_list('pk', flat=True)
                    ])
                queryset = queryset.filter_if_any(*[{
                    lookup: bit
                } for lookup in lookups])
            return JsonResponse(queryset.values('name'))
        return JsonResponse()
Esempio n. 35
0
    def transform(self, data):
        path = self.find_biggest_image(data['props']['srcset'])
        name = os.path.basename(path)

        image = Image(caption=data['props'].get('caption', ''),
                      title=data['props']['alt'] or self.page.title)

        # save temp file
        img_temp = NamedTemporaryFile()
        img_temp.write(self.get_image_data_from_file(path))
        img_temp.flush()

        # save file and image
        image.file.save(name, File(img_temp), save=True)
        image.save()

        return {'type': 'image', 'value': image.id}
Esempio n. 36
0
def images_add(request):
    if not request.user.is_authenticated():
        return HttpResponseRedirect('/staff/login/')
    elif request.user.has_perm('images.add_image'):
        if request.method == 'POST':
            form = ImageForm(request.POST, request.FILES)
            if form.is_valid():
                # save new image
                new_image = Image(name=form.cleaned_data['name'])
                if form.cleaned_data.get('caption'):
                    new_image.caption = form.cleaned_data['caption']
                if form.cleaned_data.get('credit'):
                    new_image.credit = form.cleaned_data['credit']
                new_image.image = form.cleaned_data['image']
                new_image.save()
                return HttpResponseRedirect(
                    reverse('images.views.images_add_to_markup',
                            args=[new_image.id]))
            else:
                return render_to_response(
                    'images/widget_add.html', {'form': form},
                    context_instance=RequestContext(request))
        else:
            form = ImageForm()
            return render_to_response('images/widget_add.html', {'form': form},
                                      context_instance=RequestContext(request))
    else:
        return render_to_response('staff/access_denied.html', {
            'missing': 'add photos to',
            'staffapp': 'this entry'
        },
                                  context_instance=RequestContext(request))
Esempio n. 37
0
def upload_images(request):
    """
    View to store upload images specific to an event.
    Models used: Folder, Image
    """
    try:
        is_loggedin, username = get_session_variables(request)
        
        # User is not logged in
        if not is_loggedin:
            return HttpResponseRedirect('/register/login')
        
        # User is logged in
        else:
            if request.method == 'POST':
                form = FolderForm(request.POST, request.FILES)
    
                # form is not valid          
                if not form.is_valid():
                    error = "Invalid inputs"
                    return render_to_response('images/upload_images.html', \
                        {'form':form, 'error':error, }, \
                        RequestContext(request))
                
                # form is valid
                else:
                    cleaned_form_data = form.cleaned_data
                    folder_name = cleaned_form_data['folder_name']
                    # create a new folder
                    folder = form.save(commit=False)
                    folder.save()

                    # save images
                    image = request.FILES['image']
                    new_image_object = Image(img=image, \
                        folder_name = folder)
                    img_name = new_image_object.img.name 
                    new_image_object.save()
                    return render_to_response("images/success.html")
            else:
                return render_to_response('images/upload_images.html', \
                    {'form':FolderForm(), 'is_loggedin': is_loggedin },\
                     RequestContext(request))
    except KeyError:
        return error_key(request)
    def upload_base64(self, request, *args, **kwargs):
        if request.user.is_verified:
            data = request.data
            extension = 'jpg'
            if 'image' in data:
                if len(data['image']) > 0:
                    if re.compile('^data:image/jpeg;').match(
                            data['image']) is not None:
                        image_type = 'image/jpeg'
                        extension = 'jpg'

                    if re.compile('^data:image/png;').match(
                            data['image']) is not None:
                        image_type = 'image/png'
                        extension = 'png'

                    image_data = binascii.a2b_base64(data['image'].replace(
                        'data:' + image_type + ';base64,', ''))
                    f = BytesIO(image_data)
                    pil_image = PilImage.open(f)
                    f.seek(0)

                    # TODO: make them all jpegs
                    average_color = average_image_color(pil_image)
                    color = "rgb(%s,%s,%s)" % (average_color['R'],
                                               average_color['G'],
                                               average_color['B'])
                    image = Image(width=pil_image.width,
                                  height=pil_image.height,
                                  created_by=request.user,
                                  modified_by=request.user,
                                  color=color)

                    image.image = UploadedFile(file=f,
                                               name='%s.%s' %
                                               (image.id, extension),
                                               content_type=image_type,
                                               size=len(image_data))
                    image.save()

                    f.close()

                return Response(ImageSerializer(image).data)

        raise NotFound()
Esempio n. 39
0
def upload_images(request):
    """
    View to store upload images specific to an event.
    Models used: Folder, Image
    """
    try:
        is_loggedin, username = get_session_variables(request)

        # User is not logged in
        if not is_loggedin:
            return HttpResponseRedirect('/register/login')

        # User is logged in
        else:
            if request.method == 'POST':
                form = FolderForm(request.POST, request.FILES)

                # form is not valid
                if not form.is_valid():
                    error = "Invalid inputs"
                    return render(request, 'images/upload_images.html', \
                        {'form':form, 'error':error, }, \
                        RequestContext(request))

                # form is valid
                else:
                    cleaned_form_data = form.cleaned_data
                    folder_name = cleaned_form_data['folder_name']
                    # create a new folder
                    folder = form.save(commit=False)
                    folder.save()

                    # save images
                    image = request.FILES['image']
                    new_image_object = Image(img=image, \
                        folder_name = folder)
                    img_name = new_image_object.img.name
                    new_image_object.save()
                    return render(request, "images/success.html")
            else:
                return render(request, 'images/upload_images.html', \
                    {'form':FolderForm(), 'is_loggedin': is_loggedin },\
                     RequestContext(request))
    except KeyError:
        return error_key(request)
Esempio n. 40
0
    def get_context_data(self, **kwargs):
        context = super().get_context_data(**kwargs)

        context['title'] = self.get_object().title
        context['images'] = self.get_object().images
        context['image_default'] = Image.default()
        context['form'] = UploadFileForm({'album_id': self.get_object().id})

        return context
Esempio n. 41
0
def save_latest_images(image_file=None, image_path=None):
    """
    Processes the file with images URLs and saves them into db
    :param image_file: The file containing image urls, if empty it defaults to the one in settings
    :param image_path: The place to store images, if empty it defaults to the one in settings
    """
    from images.tasks import logger
    images_file = image_file if image_file else image_settings.IMAGES_FILE
    saved_images_path = image_path if image_path else image_settings.IMAGES_PATH
    # Check if file is present, skip otherwise
    if os.path.isfile(images_file):
        # Open images file and process each line
        file = open(images_file, 'r')
        for image in file:
            image = image.rstrip('\n')
            response = requests.get(image, stream=True)
            # Get file name
            image_name = os.path.basename(image)
            try:
                with open(saved_images_path + image_name, 'xb') as out_file:
                    shutil.copyfileobj(response.raw, out_file)
                # Save object in bd if does not exist
                if not Image.objects.filter(image_url=image).exists():
                    image = Image(
                        title=image_name,
                        image_url=image,
                        location=saved_images_path + image_name,
                        created_on=datetime.datetime.utcnow(),
                    )
                    image.save()
                    logger.info(
                        'Saved image {0} correctly to db and to disk'.format(
                            image_name))
                del response
            except IOError:
                logger.error(
                    'An error occured trying to save image to disk for image {0}'
                    .format(image_name))
                continue
    else:
        logger.error(
            'Could not find file {0} with images urls'.format(images_file))
Esempio n. 42
0
def import_glance_image(request):
    """
        Imports Openstack Glance image into Wistar.

        :param request: JSON payload that contains a single object with the following properties:
            image_name, image_type, image_descr
        :return: a JSON object with at least the following properties: status (boolean) and message
        """

    logger.debug("<---- API call import_glance_images ---->")

    if openstackUtils.connect_to_openstack():

        json_string = request.body

        try:
            json_body = json.loads(json_string)
        except ValueError as ve:
            logger.error(
                'Could not parse json payload with error <{0}>'.format(
                    ve.message))
            return apiUtils.return_json(False, "Could not parse json payload!")

        logger.debug(json_body)
        required_fields = {'image_name', 'image_type', 'image_descr'}

        if not required_fields.issubset(json_body):
            logger.error("Invalid parameters in json body")
            return apiUtils.return_json(False,
                                        "Invalid parameters in json payload")

        image_name = json_body["image_name"]
        image_type = json_body["image_type"]
        image_decr = json_body["image_descr"]

        logger.debug(
            "<---- API call import_glance_image: <{0}> START ---->".format(
                image_name))

        if Image.objects.filter(name=image_name).exists():
            logger.info('image with name %s already exists' % image_name)
            return apiUtils.return_json(True, "Glance images already imported")

        image = Image()
        image.description = image_decr + ' image imported from Glance'
        image.name = image_name
        image.type = image_type
        image.save()
        logger.debug(
            "<---- API call import_glance_image: <{0}> DONE ---->".format(
                image_name))

        return apiUtils.return_json(True,
                                    "Glance images successfully imported")

    else:
        return apiUtils.return_json(False, "Failed to authenticate")
Esempio n. 43
0
    def handle(self, *args, **options):
        try:
            project = Project.objects.get(pk=options['project_id'])
        except Project.DoesNotExist:
            raise CommandError('Project "%s" does not exist' %
                               options['project_id'])

        for path, dirs, files in os.walk(options['folder']):
            match = []
            match.extend(fnmatch.filter(files, '*.[Jj][Pp][Gg]'))
            match.extend(fnmatch.filter(files, '*.[Jj][Pp][Ee][Gg]'))
            match.extend(fnmatch.filter(files, '*.[Pp][Nn][Gg]'))
            match.extend(fnmatch.filter(files, '*.[Tt][Ii][Ff]'))
            match.extend(fnmatch.filter(files, '*.[Tt][Ii][Ff][Ff]'))

            for m in match:
                # FIXME: filename should be relative to settings.MEDIA_ROOT
                im = Image(project=project)
                im.image.save(m, File(open(os.path.join(path, m))))
                im.save()
                print '.',
Esempio n. 44
0
def upload_tar(request, projectid):
    import tarfile

    if request.method == "POST":
        targzfile = request.FILES.get('tarfile', None)
        project = Project.objects.get(id=projectid)

        if tarfile:
            tar = tarfile.open(targzfile.temporary_file_path())
            tar.extractall()
            files = tar.getnames()
            tar.close()
            for image in files:
                i = Image(project=project)
                i.image.save(image, File(open(image, 'r')))
                i.save()

            return render(request, "images/upload_result.html", {
                "project": project,
                "files": files
            })
Esempio n. 45
0
def upload(request):
    """
    ""  The homepage for Posts. Displays the 5 most current entries and their
    ""  entire accompanining posts. 
    """
    
    if request.method == 'POST':
        form = ImageUploadForm(request.POST, request.FILES)
        if form.is_valid():
             
            filename, extension = os.path.splitext(request.FILES['file'].name)
            if extension in ('.jpg', '.jpeg', '.png', '.gif'):
            
                image = Image(user=request.user, filename=filename, extension=extension)
                image.save()
                image.save_image(request.FILES['file'])
                


                    
    return HttpResponseRedirect("/images/")
Esempio n. 46
0
def images_add(request):
    if not request.user.is_authenticated():
        return HttpResponseRedirect("/staff/login/")
    elif request.user.has_perm("images.add_image"):
        if request.method == "POST":
            form = ImageForm(request.POST, request.FILES)
            if form.is_valid():
                # save new image
                new_image = Image(name=form.cleaned_data["name"])
                if form.cleaned_data.get("caption"):
                    new_image.caption = form.cleaned_data["caption"]
                if form.cleaned_data.get("credit"):
                    new_image.credit = form.cleaned_data["credit"]
                new_image.image = form.cleaned_data["image"]
                new_image.save()
                return HttpResponseRedirect(reverse("images.views.images_add_to_markup", args=[new_image.id]))
            else:
                return render_to_response(
                    "images/widget_add.html", {"form": form}, context_instance=RequestContext(request)
                )
        else:
            form = ImageForm()
            return render_to_response(
                "images/widget_add.html", {"form": form}, context_instance=RequestContext(request)
            )
    else:
        return render_to_response(
            "staff/access_denied.html",
            {"missing": "add photos to", "staffapp": "this entry"},
            context_instance=RequestContext(request),
        )
Esempio n. 47
0
def create_from_instance(request, uuid):
    logger.debug("Creating new image from instance")
    domain = libvirtUtils.get_domain_by_uuid(uuid)

    logger.debug("got domain " + domain.name())
    domain_image = libvirtUtils.get_image_for_domain(uuid)
    logger.debug("got domain_image: " + domain_image)

    if osUtils.is_image_thin_provisioned(domain_image):
        logger.error(
            "Cannot clone disk that is thinly provisioned! Please perform a block pull before continuing"
        )
        context = {
            'error':
            "Cannot Clone thinly provisioned disk! Please perform a block pull!"
        }
        return render(request, 'error.html', context)

    domain_name = domain.name()

    # FIXME - make these variable names a bit more clear about their meaning
    # we need to get the path of the image relative to the MEDIA_ROOT
    media_root = settings.MEDIA_ROOT
    media_root_array = media_root.split("/")
    len_media_root = len(media_root_array)

    full_path_array = domain_image.split("/")
    full_path = "/".join(full_path_array[:full_path_array.index('instances')])

    # grab the file path of the domain image without the MEDIA_ROOT prepended
    file_path_array = domain_image.split('/')[len_media_root:]
    images_dir = "/".join(file_path_array[:file_path_array.index('instances')])

    new_relative_image_path = images_dir + "/image_" + str(
        domain.UUIDString()) + ".img"
    new_full_image_path = full_path + "/image_" + str(
        domain.UUIDString()) + ".img"

    if osUtils.check_path(new_full_image_path):
        logger.info("Image has already been cloned")
        context = {'error': "Instance has already been cloned!"}
        return render(request, 'error.html', context)

    logger.debug("Copying image from " + domain_image)

    logger.debug("To " + new_full_image_path)

    osUtils.copy_image_to_clone(domain_image, new_full_image_path)

    image = Image()
    image.name = "image_" + str(domain.UUIDString())
    image.description = "Clone of " + domain_name
    image.filePath = new_relative_image_path
    image.save()
    return HttpResponseRedirect('/images/')
Esempio n. 48
0
def upload_image(request):
    """
    Uploads an image from the form.
    """
    # Make sure you can only upload photos if you're logged in.
    if not request.user.is_authenticated():
        return HttpResponseRedirect('/login/')

    if request.method == 'POST':
        form = UploadImageForm(request.POST, request.FILES)
        if form.is_valid():
            image = Image(
                user=request.user,
                image=request.FILES['image'],
                title=form.cleaned_data['title']
            )
            image.save()
            return HttpResponseRedirect('/')
    else:
        form = UploadImageForm()
    variables = RequestContext(request, {'form': form})
    return render_to_response('image_upload.html', variables)
Esempio n. 49
0
 def _render_images(self, source):
     if source is not None:
         regex = re.compile("\{IMAGE:(.*?)\}")
         list_image = regex.findall(source)
 
         for name in list_image:
             try:
                 obj = Image.query().filter(name=name).get()
                 alt = None
                 if obj['alt_text']:
                     alt = ' alt="%s"' % obj['alt_text']
                 img = '<img src="%s"%s />' % (obj['image'].url, alt)
                 source = source.replace("{IMAGE:%s}" % name, img)
             except:
                 continue
     return source
Esempio n. 50
0
 def filter_images(self, request, *args, **kwargs):
     query = request.GET.get('q')
     if query:
         if self.search_fields is None:
             self.search_fields = ('keywords',)
         lookups = ["%s__icontains" % field for field in self.search_fields]
         bits = query.split()
         
         site = None
         if request.session[CURRENT_SITE_FILTER] != '':
             site = Site.query().get(pk = request.session[CURRENT_SITE_FILTER])
         
         queryset = Image.query()
         for bit in bits:
             if site and WIDGET_SITE_SPECIFIC_IMAGES:
                 queryset = queryset.filter(pk__in = [str(pk) for pk in site['images'].values_list('pk', flat=True)])
             queryset = queryset.filter_if_any(*[{lookup: bit} for lookup in lookups])
         return JsonResponse(queryset.values('name'))
     return JsonResponse()
Esempio n. 51
0
    def post(self, request):
        if not request.FILES.get('attachment'):
            return HttpResponseBadRequest('A file must be provided under the `attachment` POST parameter')

        fr = request.FILES['attachment']

        mimetype, encoding = mimetypes.guess_type(fr.name)
        if not mimetype in settings.ALLOWED_FORMATS:
            return HttpResponseForbidden('The provided file\'s mimetype of `%s` is not in the ALLOWED_FORMATS list' % mimetype)


        # Ugh, we should change this; needs to be a better way to get a unique ID
        # rather than relying on creating instance first
        image_identifier = ImageModel.generate_hash()

        storage_instance = STORAGE_LIBRARY(filename=image_identifier)
        storage_instance.store(fr, content_type=fr.content_type)

        image_instance = ImageModel(file_name=fr.name, content_type=fr.content_type)
        image_instance.hash = image_identifier
        image_instance.path = storage_instance.get_remote_path()
        image_instance.save()

        return HttpResponse(image_identifier)
Esempio n. 52
0
def multiuploader(request):
    """
    Main Multiuploader module.
    Parses data from jQuery plugin and makes database changes.
    """

    """ 
    CL
    Instead of using MultiuploaderImage as the model, need to use our own
    Image model.
    """

    user = User.objects.filter(username=request.user)[0]

    if request.method == 'POST':
        log.info('received POST to main multiuploader view')
        if request.FILES == None:
            return HttpResponseBadRequest('Must have files attached!')

        #getting file data for farther manipulations
        file = request.FILES[u'files[]']
        wrapped_file = UploadedFile(file)
        filename = wrapped_file.name
        file_size = wrapped_file.file.size
        log.info ('Got file: "%s"' % str(filename))
        log.info('Content type: "$s" % file.content_type')

        image = Image()
        image.filename = str(filename)
        image.image = file
        image.title = "" 
        image.caption = "" 
        image.owner = user
        image.save()

        log.info('File saving done')

        #getting thumbnail url using sorl-thumbnail
        if 'image' in file.content_type.lower():
            im = get_thumbnail(image, "80x80", quality=50)
            thumb_url = im.url
        else:
            thumb_url = ''

        #settings imports
        try:
            file_delete_url = settings.MULTI_FILE_DELETE_URL+'/'
            file_url = settings.MULTI_IMAGE_URL+'/'
        except AttributeError:
            file_delete_url = 'multi_delete/'
            file_url = 'multi_image/'

#        file_url='image/1/edit' # becomes /upload/image/1/edit
        file_url = '/account/image/' + str(image.id) + '/edit'

        #generating json response array
        result = []
        result.append({"name":filename, 
                       "size":file_size, 
                       "url":file_url, 
                       "thumbnail_url":thumb_url,
                       "delete_url":file_delete_url+str(image.pk)+'/', 
                       "delete_type":"POST",})
        response_data = simplejson.dumps(result)
        
        #checking for json data type
        #big thanks to Guy Shapiro
        if "application/json" in request.META['HTTP_ACCEPT_ENCODING']:
            mimetype = 'application/json'
        else:
            mimetype = 'text/plain'
        return HttpResponse(response_data, mimetype=mimetype)
    else: #GET
        return HttpResponse('Only POST accepted')
Esempio n. 53
0
    def setup(self):
	self.image = Image(title="Hellow World",
                           description="My first test image. Yay!",
                           filename="test.png")
    def handle(self, *args, **options):
        if len(args) != 1:
            raise CommandError(
                "You must provide the path to the CSV file as an argument."
                )

        constituency_offices = Organisation.objects.filter(
            kind__slug='constituency-office')

        storage = FileSystemStorage()

        storage_path = os.path.join('images', 'constituency-offices')
        images_directory = os.path.join(
            settings.MEDIA_ROOT,
            storage_path
            )
        mkdir_p(images_directory)

        with open(args[0]) as csvfile:
            reader = csv.DictReader(csvfile)

            for row in reader:
                poi_ref = row['POI_REFERENCE']
                party, name = (
                    re.match(
                        '(.*) Constituency Office (.*)', poi_ref
                        ).groups()
                    )
                name = re.sub('\s+', ' ', name)

                qs = constituency_offices.filter(
                    name__regex=r'^{}.+: {}$'.format(party, name))

                try:
                    org = qs.get()
                except Organisation.MultipleObjectsReturned:
                    print "Skipping {} as multiple orgs returned: {}".format(
                        poi_ref,
                        repr(qs),
                        )
                    continue
                except Organisation.DoesNotExist:
                    # Fall back to searching for the name and the party in the
                    # constituency office name
                    qs = (
                        Organisation.objects
                        .filter(kind__slug='constituency-office')
                        .filter(name__contains=name)
                        .filter(name__contains=party)
                        )

                    org = qs.get()

                place = (
                    org.place_set
                    .filter(name__contains='Approximate position of')
                    .filter(kind__slug='constituency-office')
                    .get()  # There should be only one.
                    )

                lon, lat = location_re.match(row['Constituency_Pin']).groups()

                place.location = Point(float(lon), float(lat))
                place.save()

                # Now get the photo
                photo_url = row.get('Constituency_Photo')

                try:
                    org.images.get(source=photo_url)
                    print (
                        "Skipping {} as url matches existing image."
                        .format(org.slug)
                        )
                    continue
                except Image.DoesNotExist:
                    print "Adding new image to {}.".format(org.slug)

                if photo_url:
                    response = requests.get(photo_url)
                    extension = get_image_extension(StringIO(response.content))

                    if extension is None:
                        continue

                    image_filename = '{}.{}'.format(org.slug, extension)

                    desired_storage_path = os.path.join(storage_path, image_filename)

                    storage_filename = storage.save(desired_storage_path, StringIO(response.content))

                    image = Image(
                        content_object=org,
                        source=photo_url,
                        is_primary=True,
                        image=storage_filename,
                        )
                    image.save()
Esempio n. 55
0
def image_upload_process(imageFiles, imageOptionsForm, annotationOptionsForm, source, currentUser, annoFile):
    """
    Helper method for the image upload view and the image+annotation
    import view.
    """

    uploadedImages = []
    duplicates = 0
    imagesUploaded = 0
    annotationsImported = 0
    importedUser = get_imported_user()

    dupeOption = imageOptionsForm.cleaned_data['skip_or_replace_duplicates']

    annotationData = None
    if annoFile:
        try:
            annotationData = annotations_file_to_python(annoFile, source)
        except FileContentError as errorDetail:
            return dict(error=True,
                message='Error reading labels file %s. %s' % (annoFile.name, errorDetail),
            )

    for imageFile in imageFiles:

        filename = imageFile.name
        metadataDict = None
        metadata = Metadata(height_in_cm=source.image_height_in_cm)

        if imageOptionsForm.cleaned_data['specify_metadata'] == 'filenames':

            try:
                metadataDict = filename_to_metadata(filename, source)

            # Filename parse error.
            # TODO: check for validity of the file type and contents, too.
            except (ValueError, StopIteration):
                return dict(error=True,
                    message='Upload failed - Error when parsing the filename %s for metadata.' % filename,
                )

            # Detect duplicate images and handle them
            dupe = find_dupe_image(source, **metadataDict)
            if dupe:
                duplicates += 1
                if dupeOption == 'skip':
                    # Skip uploading this file.
                    continue
                elif dupeOption == 'replace':
                    # Proceed uploading this file, and delete the dupe.
                    dupe.delete()

            # Set the metadata
            valueDict = get_location_value_objs(source, metadataDict['values'], createNewValues=True)
            photoDate = datetime.date(year = int(metadataDict['year']),
                             month = int(metadataDict['month']),
                             day = int(metadataDict['day']))

            metadata.name = metadataDict['name']
            metadata.photo_date = photoDate
            for key, value in valueDict.iteritems():
                setattr(metadata, key, value)

        else:
            metadata.name = filename

        # Image + annotation import form
        # Assumes we got the images' metadata (from filenames or otherwise)
        if annotationData:

            pointsOnlyOption = annotationOptionsForm.cleaned_data['points_only']

            # Use the location values and the year to build a string identifier for the image, such as:
            # Shore1;Reef5;...;2008
            imageIdentifier = get_image_identifier(metadataDict['values'], metadataDict['year'])

            # Use the identifier as the index into the annotation file's data.
            if not annotationData.has_key(imageIdentifier):
                return dict(error=True,
                    message='%s seems to have no annotations for the image file %s, which has the following keys:\n%s' % (
                        annoFile.name, imageFile.name, imageIdentifier.replace(';',' '))
                )

            imageAnnotations = annotationData[imageIdentifier]

            status = ImageStatus()
            status.save()

            metadata.annotation_area = AnnotationAreaUtils.IMPORTED_STR
            metadata.save()

            img = Image(original_file=imageFile,
                    uploaded_by=currentUser,
                    point_generation_method=PointGen.args_to_db_format(
                        point_generation_type=PointGen.Types.IMPORTED,
                        imported_number_of_points=len(imageAnnotations)
                    ),
                    metadata=metadata,
                    source=source,
                    status=status,
                  )
            img.save()

            # Iterate over this image's annotations and save them.
            pointNum = 1
            for anno in imageAnnotations:

                # Save the Point in the database.
                point = Point(row=anno['row'], column=anno['col'], point_number=pointNum, image=img)
                point.save()

                if not pointsOnlyOption:
                    label = Label.objects.filter(code=anno['label'])[0]

                    # Save the Annotation in the database, marking the annotations as imported.
                    annotation = Annotation(user=importedUser,
                                            point=point, image=img, label=label, source=source)
                    annotation.save()

                    annotationsImported += 1

                pointNum += 1

            img.status.hasRandomPoints = True
            if not pointsOnlyOption:
                img.status.annotatedByHuman = True
            img.status.save()

        # Image upload form, no annotations
        else:
            status = ImageStatus()
            status.save()

            metadata.annotation_area = source.image_annotation_area
            metadata.save()

            # Save the image into the DB
            img = Image(original_file=imageFile,
                    uploaded_by=currentUser,
                    point_generation_method=source.default_point_generation_method,
                    metadata=metadata,
                    source=source,
                    status=status,
                  )
            img.save()

            # Generate and save points
            generate_points(img)

        # Up to 5 uploaded images will be shown
        # upon successful upload.
        # Prepend to list, so most recent image comes first
        uploadedImages.insert(0, img)
        if len(uploadedImages) > 5:
            uploadedImages = uploadedImages[:5]

        imagesUploaded += 1

    # Construct success message.
    success_message = image_upload_success_message(
        num_images_uploaded=imagesUploaded,
        num_dupes=duplicates,
        dupe_option=dupeOption,
        num_annotations=annotationsImported,
    )

    return dict(error=False,
        uploadedImages=uploadedImages,
        message=success_message,
    )
Esempio n. 56
0
 def render(self, name, value, attrs=None):
     images = Image.query().active()
     if 'site' in self.attrs and self.attrs['site'] and WIDGET_SITE_SPECIFIC_IMAGES:
         images = images.filter(pk__in = [str(pk) for pk in self.attrs['site']['images'].values_list('pk', flat=True)])
     rows = self.partition(list(images),self.images_per_row)
     return render_to_string('image_list.html', {'images':rows, 'image_height':self.image_height })
Esempio n. 57
0
def image_upload_process(imageFile, imageOptionsForm,
                         annotation_dict_id,
                         csv_dict_id, metadata_import_form_class,
                         annotation_options_form,
                         source, currentUser):

    is_uploading_points_or_annotations = annotation_options_form.cleaned_data['is_uploading_points_or_annotations']

    filename = imageFile.name
    metadata_dict = None
    metadata_obj = Metadata(height_in_cm=source.image_height_in_cm)

    if imageOptionsForm.cleaned_data['specify_metadata'] == 'filenames':

        filename_check_result = check_image_filename(filename, source)
        filename_status = filename_check_result['status']

        if filename_status == 'error':
            # This case should never happen if the pre-upload
            # status checking is doing its job, but just in case...
            return dict(
                status=filename_status,
                message=u"{m}".format(m=filename_check_result['message']),
                link=None,
                title=None,
            )

        # Set the metadata
        metadata_dict = filename_check_result['metadata_dict']

        value_dict = get_location_value_objs(source, metadata_dict['values'], createNewValues=True)
        photo_date = datetime.date(
            year = int(metadata_dict['year']),
            month = int(metadata_dict['month']),
            day = int(metadata_dict['day'])
        )

        metadata_obj.name = metadata_dict['name']
        metadata_obj.photo_date = photo_date
        for key, value in value_dict.iteritems():
            setattr(metadata_obj, key, value)

    elif imageOptionsForm.cleaned_data['specify_metadata'] == 'csv':

        if not csv_dict_id:
            return dict(
                status='error',
                message=u"{m}".format(m="CSV file was not uploaded."),
                link=None,
                title=None,
            )

        csv_dict_filename = os.path.join(
            settings.SHELVED_ANNOTATIONS_DIR,
            'csv_source{source_id}_{dict_id}.db'.format(
                source_id=source.id,
                dict_id=csv_dict_id,
            ),
        )

        # Corner case: the specified shelved annotation file doesn't exist.
        # Perhaps the file was created a while ago and has been pruned since,
        # or perhaps there is a bug.
        if not os.path.isfile(csv_dict_filename):
            return dict(
                status='error',
                message="CSV file could not be found - if you provided the .csv file a while ago, maybe it just timed out. Please retry the upload.",
                link=None,
                title=None,
            )

        csv_dict = shelve.open(csv_dict_filename)

        #index into the csv_dict with the filename. the str() is to handle
        #the case where the filename is a unicode object instead of a str;
        #unicode objects can't index into dicts.
        filename_str = str(filename)

        if filename_str in csv_dict:

            # There is CSV metadata for this file.

            metadata_dict = csv_dict[str(filename)]
            csv_dict.close()

            # The reason this uses metadata_import_form_class instead of
            # importing MetadataImportForm is that I'm too lazy to deal with the
            # circular-import implications of the latter solution right now.
            # -Stephen
            metadata_import_form = metadata_import_form_class(
                source.id, True, metadata_dict,
            )

            if not metadata_import_form.is_valid():
                return dict(
                    status='error',
                    message="Unknown error with the CSV metadata.",
                    link=None,
                    title=None,
                )

            fields = ['photo_date', 'value1', 'value2', 'value3', 'value4',
                      'value5', 'height_in_cm', 'latitude', 'longitude',
                      'depth', 'camera', 'photographer', 'water_quality',
                      'strobes', 'framing', 'balance']

            for field in fields:

                if not field in metadata_import_form.fields:
                    # A location value field that's not in this form
                    continue

                value = metadata_import_form.cleaned_data[field]
                # Check for a non-empty value; don't want empty values to
                # override default values that we've already set on the
                # metadata_obj
                if value:
                    setattr(metadata_obj, field, value)

        else:

            # No CSV metadata for this file.

            csv_dict.close()

        metadata_obj.name = filename

    else:

        # Not specifying any metadata at upload time.
        metadata_obj.name = filename


    image_annotations = None
    has_points_or_annotations = False

    if is_uploading_points_or_annotations:

        # Corner case: somehow, we're uploading with points+annotations and without
        # a checked annotation file specified.  This probably indicates a bug.
        if not annotation_dict_id:
            return dict(
                status='error',
                message=u"{m}".format(m=str_consts.UPLOAD_ANNOTATIONS_ON_AND_NO_ANNOTATION_DICT_ERROR_STR),
                link=None,
                title=None,
            )

        annotation_dict_filename = os.path.join(
            settings.SHELVED_ANNOTATIONS_DIR,
            'source{source_id}_{dict_id}'.format(
                source_id=source.id,
                dict_id=annotation_dict_id,
            ),
        )

        # Corner case: the specified shelved annotation file doesn't exist.
        # Perhaps the file was created a while ago and has been pruned since,
        # or perhaps there is a bug.
        if not os.path.isfile(annotation_dict_filename):
            return dict(
                status='error',
                message="Annotations could not be found - if you provided the .txt file a while ago, maybe it just timed out. Please retry the upload.",
                link=None,
                title=None,
            )


        # Use the location values and the year to build a string identifier for the image, such as:
        # Shore1;Reef5;...;2008
        # Convert to a string (instead of a unicode string) for the shelve key lookup.
        image_identifier = str(get_image_identifier(metadata_dict['values'], metadata_dict['year']))

        annotation_dict = shelve.open(annotation_dict_filename)

        if annotation_dict.has_key(image_identifier):
            image_annotations = annotation_dict[image_identifier]
            has_points_or_annotations = True
        annotation_dict.close()

    if has_points_or_annotations:
        # Image upload with points/annotations

        is_uploading_annotations_not_just_points = annotation_options_form.cleaned_data['is_uploading_annotations_not_just_points']
        imported_user = get_imported_user()

        status = ImageStatus()
        status.save()

        metadata_obj.annotation_area = AnnotationAreaUtils.IMPORTED_STR
        metadata_obj.save()

        img = Image(
            original_file=imageFile,
            uploaded_by=currentUser,
            point_generation_method=PointGen.args_to_db_format(
                point_generation_type=PointGen.Types.IMPORTED,
                imported_number_of_points=len(image_annotations)
            ),
            metadata=metadata_obj,
            source=source,
            status=status,
        )
        img.save()

        # Iterate over this image's annotations and save them.
        point_num = 0
        for anno in image_annotations:

            # Save the Point in the database.
            point_num += 1
            point = Point(row=anno['row'], column=anno['col'], point_number=point_num, image=img)
            point.save()

            if is_uploading_annotations_not_just_points:
                label = Label.objects.filter(code=anno['label'])[0]

                # Save the Annotation in the database, marking the annotations as imported.
                annotation = Annotation(user=imported_user,
                    point=point, image=img, label=label, source=source)
                annotation.save()

        img.status.hasRandomPoints = True
        if is_uploading_annotations_not_just_points:
            img.status.annotatedByHuman = True
        img.status.save()
    else:
        # Image upload, no points/annotations
        image_status = ImageStatus()
        image_status.save()

        metadata_obj.annotation_area = source.image_annotation_area
        metadata_obj.save()

        # Save the image into the DB
        img = Image(original_file=imageFile,
            uploaded_by=currentUser,
            point_generation_method=source.default_point_generation_method,
            metadata=metadata_obj,
            source=source,
            status=image_status,
        )
        img.save()

        # Generate and save points
        generate_points(img)

    success_message = "Uploaded"

    return dict(
        status='ok',
        message=success_message,
        link=reverse('image_detail', args=[img.id]),
        title=img.get_image_element_title(),
        image_id=img.id,
    )
Esempio n. 58
0
File: tests.py Progetto: sdob/dsapi
 def test_can_retrieve_images(self, mock):
     i = Image(content_object=self.divesite, owner=self.owner)
     i.save()
     response = self.client.get(reverse('divesite-image-list', args=[self.divesite.id]))
     self.assertEqual(response.status_code, status.HTTP_200_OK)
     self.assertEqual(len(response.data), 1)
Esempio n. 59
0
def node_save(node, args, request):
    node.title = args.get('title', node.title)
    node.description = args.get('description', node.description)
    node.gbif_id = args.get('gbif_id', node.gbif_id)
    node.rank = args.get('rank', node.rank)
    node.edibility = args.get('edibility', node.edibility)

    parent_id = args.get('parent')
    if parent_id:
        gid_type, gid = from_global_id(parent_id)
        node.parent = Document._meta.model.objects.get(pk=gid)

    prev_images = []
    if node.pk:
        prev_images = node.images.values_list('id', flat=True)

    prev_commonNames = []
    if node.pk:
        prev_commonNames = node.commonNames.values_list('id', flat=True)

    node.save(request=request)

    for prev_img_id in prev_images:
        node.images.add(prev_img_id)

    node.commonNames.clear()
    for prev_name_id in prev_commonNames:
        node.commonNames.add(prev_name_id)

    commonNames = args.get('commonNames', [])
    for commonNameDict in commonNames:
        commonName_id = commonNameDict.get('id', '').strip(' \t\n\r')
        commonName_str = commonNameDict['name'].strip(' \t\n\r')

        if len(commonName_str) == 0:
            # don't save empty
            continue

        if len(commonName_id) > 0:
            cn_gid_type, cn_gid = from_global_id(commonName_id)
            get = {'document_id': cn_gid}
        else:
            get = {
                'name': commonName_str,
                'document__lifeNode_commonName': node
            }

        try:
            commonName = CommonName.objects.get(**get)
        except CommonName.DoesNotExist:
            commonName = CommonName()

        commonName.name = commonName_str
        commonName.language = commonNameDict['language']
        commonName.save(request=request)
        if commonName.document_id not in prev_commonNames:
            node.commonNames.add(commonName.document)

    imagesToAdd = args.get('imagesToAdd', [])
    for imageToAdd in imagesToAdd:
        form = ImageForm(imageToAdd, MultiValueDict({
            'image': request.FILES.getlist(imageToAdd['field'])
        }))
        if form.is_valid():
            data = form.cleaned_data
            image = ImageModel(
                image=data['image'],
                description=data['description']
            )
            image.save(request=request)
            node.images.add(image.document)

    return node
Esempio n. 60
0
def an_image_with_title_exists(step, title):
    image = Image(title=title,
                  filename=title.replace(" ", "_").lower() + ".png",
                  description=title + " description")
    image.save()