Example #1
0
def read_lines(txt_file):
    """
    This method reads the text file and
    call appropriate function to download the image
    """
    # Using readlines method
    file1 = open(txt_file, 'r')
    lines = file1.readlines()
    # Strips the newline character
    for line in lines:
        image_downloader.download_image(line.strip())
Example #2
0
def download_hubble_images(image_id=0, collection=False, page="all"):
    ''' Downloads Hubble images '''
    hubble_images_urls = get_hubble_images_urls(image_id, collection, page)

    for hubble_image_url in hubble_images_urls:
        image_id = hubble_image_url.split('/')[-2]
        if not collection:
            download_image('http:' + hubble_image_url, f'hubble{os.sep}{image_id}',
                           verify=False)
        elif collection:
            download_image('http:' + hubble_image_url,
                           f'hubble{os.sep}{collection}{os.sep}{image_id}',
                           verify=False)
Example #3
0
def colorize_and_upload_from_url(image_url,verbose=True):
    #1) Download the image
    img_path = image_downloader.download_image(image_url)

    #didn't mange to download photo
    if len(img_path) == 0:
        'Problem downloading %s' % image_url
        return ''

    verbose_print(['link is : ', image_url, 'img_path is ',img_path],verbose)

    if not image_util.is_color_image(img_path):

        #2) Make some action - Colorize the image
        colorized_image_path = colorize_image(img_path,verbose)
    
        if len(colorized_image_path) == 0:
            print 'Error colorizing the photo!'
            return ''

        #3) Upload the image
        uploaded_colorized_image_url = upload_image(colorized_image_path)
        if len(uploaded_colorized_image_url) == 0:
            print 'Error uploading the image'
            return ''
    else: #Color image was given
        uploaded_colorized_image_url = image_url

    return uploaded_colorized_image_url
Example #4
0
def bot_action(c, verbose=True, respond=False):

    if verbose:
        img_url = c.link_url
        img_path = image_downloader.download_image(img_url)
        print 'link is : ', img_url, 'img_path is ',img_path

        #didn't mange to download photo
        if len(img_path) == 0:
            return

        img = cv2.imread(img_path)
        if img is not None:
            h,w = (img.shape[0],img.shape[1])
            if h > MAX_IMAGE_HEIGHT or w > MAX_IMAGE_WIDTH:
                print '-----Resizing image!!------'
                ratio = float(w)/float(h)
                if h > MAX_IMAGE_HEIGHT:
                    factor = float(h)/float(MAX_IMAGE_HEIGHT)
                else:
                    factor = float(w)/float(MAX_IMAGE_WIDTH)
                img = cv2.resize(img,None,fx=1/factor, fy=1/factor, interpolation = cv2.INTER_CUBIC)
                print '---- after resize image shape is  ----',img.shape
                cv2.imwrite(img_path,img,[cv2.IMWRITE_JPEG_QUALITY,40])
            #if h > 1080 or w > 1920:
            #    try:
            #        c.reply("Sorry image is too big! we currently only support images as big as 1920x1080")
            #        database.add_to_database(c.id)
            #        database.save_database()
            #    except:
            #        return
            #    return
            #1)Run DNN on the b&w image
            print 'Image downloaded and is ok'
            if useDNN:
                coloredImage = colorize.runDNN(img_path)
            else:
                coloredImage = img
            print 'after DNN'
            image_name = 'colorized_'+img_path
            cv2.imwrite(image_name,coloredImage)

            #2)Upload image
            print 'Uploading image'
            if args.replicate:
                uploaded_image_link = img_url
            else:
                uploaded_image_link = image_uploader.upload_image(image_name)

            #3)Reply to the one who summned the bot
            if uploaded_image_link is not None:
                msg = 'Hi I\'m colorizebot. I was trained to color b&w photos (not comics or rgb photos! Please do not abuse me :{}).\n\n This is my attempt to color your image, here you go : %s \n\n This is still a **beta-bot**. If you called the bot and didn\'t get a response, pm us and help us make it better. \n\n  [For full explanation about this bot\'s procedure](http://whatimade.today/our-frst-reddit-bot-coloring-b-2/) | [code](https://github.com/dannyvai/reddit_crawlers/tree/master/redditBotColorize)'%(uploaded_image_link)
                try:
                    res = c.reply(msg)
                    database.add_to_database(c.id)
                    database.save_database()
                except:
                    upload_queue.append((c,msg))
                    traceback.print_exc()
Example #5
0
def download_spacex_launch_images(max_counts):
    ''' Downloads images of SpaceX spaceships '''
    image_counter = 0

    spacex_images_urls = get_spacex_launch_images_urls(max_counts)
    if os.path.exists(f'images{os.sep}spacex'):
        if len(os.listdir(f'images{os.sep}spacex')) == 0:
            pass
        else:
            last_image_index = max([
                int(image_title.strip('spacex_img').split('.')[0])
                for image_title in os.listdir(f'images{os.sep}spacex')
            ])
            image_counter = last_image_index
            spacex_images_urls = get_spacex_launch_images_urls(
                max_counts, last_image_index)

    for spacex_image_url in spacex_images_urls:
        image_counter += 1
        download_image(spacex_image_url,
                       f'spacex{os.sep}spacex_img{image_counter}')
Example #6
0
def colorize_and_upload_from_url(image_url,verbose=True):
    #1) Download the image
    img_path = image_downloader.download_image(image_url)

    #didn't manage to download photo
    if len(img_path) == 0:
        print 'Problem downloading %s' % image_url
        return ''

    verbose_print(['link is : ', image_url, 'img_path is ',img_path],verbose)

    if not image_util.is_color_image(img_path):

        #2) Make some action - Colorize the image
        colorized_image_path = colorize_image(img_path,verbose)
    
        if len(colorized_image_path) == 0:
            print 'Error colorizing the photo!'
            return ''

        #3) Upload the image
        uploaded_colorized_image_url = upload_image(colorized_image_path)
        if len(uploaded_colorized_image_url) == 0:
            print 'Error uploading the image'
            return ''
    else: #Color image was given
        uploaded_colorized_image_url = 'already_colorized'
        #2) Make some action - Colorize the image
        colorized_image_path = colorize_already_colorized_image(img_path)

    #if len(colorized_image_path) == 0:
    #    print 'Error colorizing the photo!'
    #    return ''

    #Upload the image
    #uploaded_colorized_image_url = upload_image(colorized_image_path)
    #if len(uploaded_colorized_image_url) == 0:
    #    print 'Error uploading the image'
    #    return ''

        
    return uploaded_colorized_image_url
 def test_image_download(self):
     self.urls = ["https://images.pexels.com/photos/248797/pexels-photo-248797.jpeg?auto=compress&cs=tinysrgb&dpr=1&w=500"]
     for i, url in enumerate(self.urls):
         assert os.path.isfile(download_image(url,i,self.image_path)), "Files not downloaded successfully"
Example #8
0
def test_wrong_content_type(tmpdir):
    url = 'http://abc.de/bla/foobar.jpg'
    responses.add(responses.GET, url, body='', content_type='text/plain')
    with pytest.raises(ValueError) as excinfo:
        image_downloader.download_image(url, str(tmpdir))
    excinfo.match('Invalid image type')
Example #9
0
def test_fail404(tmpdir):
    url = 'http://abc.de/bla/foobar.jpg'
    responses.add(responses.GET, url, status=404, content_type='text/plain')
    with pytest.raises(requests.exceptions.HTTPError):
        image_downloader.download_image(url, str(tmpdir))
Example #10
0
def test_invalid_url(tmpdir):
    with pytest.raises(requests.exceptions.ConnectionError):
        image_downloader.download_image('http://nonexistent.de/image.png', str(tmpdir))
Example #11
0
def test_download_image(resource_name, content_type, expected_filename, tmpdir):
    url = 'http://abc.de/bla/' + resource_name
    responses.add(responses.GET, url, content_type=content_type, stream=io.StringIO(u''))
    filename = image_downloader.download_image(url, str(tmpdir))
    assert filename == expected_filename
    assert tmpdir.join(filename).exists()
    submissions = r.get_subreddit(args.subreddit).get_top(limit=int(args.num))
else:
    submissions = r.get_subreddit(args.subreddit).get_hot(limit=int(args.num))


def show_image(image_path):
    img = cv2.imread(image_name)
    if img is not None:
        cv2.imshow('downloaded image', img)
        cv2.waitKey(0)


for x in submissions:
    print(x.url)
    url = x.url
    if image_downloader.is_supported_image_url(url):
        image_name = image_downloader.get_image_name_from_url(url)
        image_downloader.download_image(url, image_name)
        if args.debug:
            print '%s downloaded from %s' % (image_name, url)
            show_image(image_name)

#        if args.debug:

    elif image_downloader.is_imgur_url(url):
        image_name = image_downloader.download_image_from_imgur(url)
        if image_name is not None:
            if args.debug:
                print '%s downloaded from %s' % (image_name, url)
                show_image(image_name)
 def test_download_image(self):
     self.assertEqual(download_image(URL_VALID, 'google-logo.jpg'), True)
     self.assertEqual(download_image(URL_VALID_WO_PARAMS, 'py.png'), True)
     self.assertEqual(download_image(URL_INVALID, ''), False)
     self.assertEqual(download_image(URL_INVALID_WO_HTTP, ''), False)
     self.assertEqual(download_image(URL_EMPTY, ''), False)
    submissions = r.get_subreddit(args.subreddit).get_new(limit=int(args.num))
elif args.type == 'top':
    submissions = r.get_subreddit(args.subreddit).get_top(limit=int(args.num))
else:
    submissions = r.get_subreddit(args.subreddit).get_hot(limit=int(args.num))

def show_image(image_path):
    img = cv2.imread(image_name)
    if img is not None:
        cv2.imshow('downloaded image',img)
        cv2.waitKey(0)

for x in submissions:
    print (x.url)
    url = x.url
    if image_downloader.is_supported_image_url(url):
        image_name = image_downloader.get_image_name_from_url(url)
        image_downloader.download_image(url,image_name)
        if args.debug:
            print '%s downloaded from %s'%(image_name,url)
            show_image(image_name)

#        if args.debug:

    elif image_downloader.is_imgur_url(url):
        image_name = image_downloader.download_image_from_imgur(url)
        if image_name is not None:
            if args.debug:
                print '%s downloaded from %s'%(image_name,url)
                show_image(image_name)