示例#1
0
def request_image(url, image_name):
    print(u"正在下载 %s" % image_name)
    try:
        path = 'output/images/%s' % image_name

        req = urllib2.Request(url)
        req.add_header('User-Agent', DEFAULT_REQUEST_HEADERS['User-Agent'])
        req.add_header('Cache-Control', 'no-cache')
        req.add_header('Accept', '*/*')
        req.add_header('Accept-Encoding', 'gzip, deflate')
        req.add_header('Connection', 'Keep-Alive')

        if USE_PROXIES == 'true':
            opener = urllib2.build_opener(urllib2.ProxyHandler(PROXIES))
            urllib2.install_opener(opener)

        resp = urllib2.urlopen(req)

        respHtml = resp.read()
        path = 'output/images/%s' % image_name

        binfile = open(path, "wb")
        binfile.write(respHtml)

        binfile.close()
        Image.update(state=1).where(Image.url == url).execute()
        print(u"%s 下载成功" % image_name)
        return True
    except Exception as e:
        print e
        print(u"%s 下载失败" % image_name)
        return False
示例#2
0
 def receive_image(self, name):
     i = Image()
     i.url = name
     try:
         db.session.add(i)
         db.session.commit()
     except:
         db.session.rollback()
     finally:
         db.session.close()
     return i.url
示例#3
0
文件: app.py 项目: YKo20010/haven
def add_image(listing_id):
    listing = Listing.query.filter_by(id=listing_id).first()
    if not listing:
        return json.dumps({
            'success': False,
            'error': 'Listing not found.'
        }), 404
    post_body = json.loads(request.data)
    image = Image(image=post_body.get('image', ''), listing_id=listing_id)
    listing.images.append(image)
    db.session.add(image)
    db.session.commit()
    return json.dumps({'success': True, 'data': image.serialize()}), 201
示例#4
0
def save_file(file, path):
    try:
        db = opendb()
        ext = file.type.split('/')[1]  # second piece
        img = Image(filename=file.name, extension=ext, filepath=path)
        db.add(img)
        db.commit()
        db.close()
        return True
    except Exception as e:
        st.write("database error:", e)
        return False
示例#5
0
def image(_id):
    try:
        img = Image.objects(id=_id).first()

        if img:
            if flask.request.method == "POST":
                return flask.jsonify(data=img.json())

            return flask.jsonify(data=img.json(),
                                 bitmap=str(base64.b64encode(img.file.read())))

    except Exception as e:
        return flask.jsonify(msg=str(e))
示例#6
0
def save_file(file, path):
    try:
        db = opendb()
        file = os.path.basename(path)
        name, ext = file.split('.')  # second piece
        vid = Image(filename=name, extension=ext, filepath=path)
        db.add(vid)
        db.commit()
        db.close()
        return True
    except Exception as e:
        st.write("database error:", e)
        return False
示例#7
0
    def store_images(self, project, images, purpose=0):
        # store images added by the user
        image_names = DB.getImageNames( project['id'], purpose )
        for image in images:
            #self.copy_annotations( image, project )
            image = Image( id=image['image_id'], purpose=purpose)
            DB.storeImage(project['id'], project['model_type'], image )
            if image.id in image_names:
                image_names.remove(image.id)

        # discard images removed by the user
        for image_name in image_names:
            DB.removeImage(project['id'], image_name )
示例#8
0
def img_analise():
    global counter
    counter += 1

    image_file = flask.request.files['image']
    print(f"\n{counter}-Received image File name : {image_file.filename}")

    img_bytes = image_file.read()

    img = Image()
    img.file.new_file()
    img.file.write(img_bytes)
    img.file.close()
    img.save()
    img.recognized = find_faces(img)
    img.save()

    return flask.jsonify(data=img.json())
示例#9
0
def _get_or_create_image(session, fp, imgur_image, file_hash):
    existing_image = session.query(Image).get(file_hash)
    if existing_image:
        return existing_image
    file_ext = os.path.splitext(imgur_image.link)[1]
    file_name = file_hash + file_ext
    if not os.path.exists(settings.IMAGES_FOLDER_PATH):
        os.makedirs(settings.IMAGES_FOLDER_PATH)
    shutil.copy(fp.name, os.path.join(settings.IMAGES_FOLDER_PATH, file_name))
    image = Image(file_hash=file_hash,
                  file_ext=file_ext,
                  content_type=imgur_image.type,
                  width=imgur_image.width,
                  height=imgur_image.height,
                  size=imgur_image.size)
    session.add(image)
    session.flush()
    return image
示例#10
0
# -*- coding: utf-8 -*-

import re, sys, os
import time
from settings import PROXIES
from settings import USE_PROXIES
from settings import DEFAULT_REQUEST_HEADERS
from db import Image
import urllib2
import threading
import Queue

reload(sys)
sys.setdefaultencoding('utf-8')

images = Image.select().where(Image.state != 1).execute()


def get_image_name(url):
    path = os.path.basename(url)
    group = re.findall('\S+\.[jpg|png|gif|jpeg|bmp]+', path)
    if len(group) > 0:
        return group[0]
    else:
        return '%s.jpg' % (time.strftime(
            '%Y%m%d%H%M%S', time.localtime(time.time())) + str(time.time()))


def request_image(url, image_name):
    print(u"正在下载 %s" % image_name)
    try: