{"id":1410,"date":"2017-10-26T00:23:03","date_gmt":"2017-10-26T00:23:03","guid":{"rendered":"http:\/\/intelligentonlinetools.com\/blog\/?p=1410"},"modified":"2017-11-03T00:08:05","modified_gmt":"2017-11-03T00:08:05","slug":"image-processing-using-pixabay-api-and-python","status":"publish","type":"post","link":"http:\/\/intelligentonlinetools.com\/blog\/2017\/10\/26\/image-processing-using-pixabay-api-and-python\/","title":{"rendered":"Image Processing Using Pixabay API and Python"},"content":{"rendered":"<p>Recently I visited great website Pixabay [1]  that offers a wide range of images from people all around the world. These images are free to use even for commercial use. And there is an API [2] for accessing images on Pixabay. This brings a lot of ideas for interesting web applications with using of machine learning technique.<\/p>\n<p>For example, what if we want find and download 10 images that very closely match to current image or theme. Or maybe there is a need to automatically scan new images that match some theme. As the first step in this direction, in this post we will look how to download images from Pixabay, save and do some analysis of images like calculating similarity between images.<\/p>\n<p>As usually with most of APIs, the first step is sign up and get API key. This is absolutely free on Pixabay.<\/p>\n<p>We will use python library python_pixabay to get links to images from Pixabay site.<br \/>\nTo download images to local folder the python library urllib.request is used in the script.<\/p>\n<p>Once the images are saved on local folder, we can calculate similarity between any chosen two images.<br \/>\nThe python code for similarity functions is taken from [4]. In this post image similarity histogram via PIL (python image library)  and image similarity vectors via numpy are calculated.  <\/p>\n<p>An image histogram is a type of histogram that acts as a graphical representation of the tonal distribution in a digital image. It plots the number of pixels for each tonal value. By looking at the histogram for a specific image a viewer will be able to judge the entire tonal distribution at a glance.[5]<\/p>\n<p>In the end of script, the image similarity via vectors between images in pair is calculated. They all are in the range between 0 and 1. The script is downloading only 8 images from Pixabay and is using default image search function.<\/p>\n<p>Thus we learned how to find and download images from Pixabay website. Also few techniques for calculating image similarities were tested.<\/p>\n<p>Here is the source code of the script.  <\/p>\n<pre><code>\r\n# -*- coding: utf-8 -*-\r\n\r\nimport python_pixabay\r\nimport urllib\r\n\r\nfrom PIL import Image\r\napikey=\"xxxxxxxxxxx\"\r\npix = python_pixabay.Pixabay(apikey)\r\n\r\n# default image search\r\nimg_search = pix.image_search()\r\n\r\n# view the content of the searches\r\nprint(img_search)\r\n\r\n\r\nhits=img_search.get(\"hits\")\r\n\r\nimages=[]\r\nfor hit in hits:\r\n    userImageURL = hit[\"userImageURL\"]\r\n    print (userImageURL)\r\n    images.append (userImageURL)\r\n\r\n\r\nprint (images)    \r\n\r\n\r\nfrom functools import reduce\r\n\r\nlocal_filenames=[]\r\nimport urllib.request\r\nimage_directory = 'C:\\\\Users\\\\Owner\\\\Desktop\\\\A\\\\Python_2016_A\\\\images'\r\nfor i in range(8):\r\n                \r\n   \r\n    local_filename, headers = urllib.request.urlretrieve(images[i])\r\n    print (local_filename)\r\n    local_filenames.append (local_filename)\r\n    \r\n          \r\n   \r\n\r\n\r\ndef image_similarity_histogram_via_pil(filepath1, filepath2):\r\n    from PIL import Image\r\n    import math\r\n    import operator\r\n    \r\n    image1 = Image.open(filepath1)\r\n    image2 = Image.open(filepath2)\r\n \r\n    image1 = get_thumbnail(image1)\r\n    image2 = get_thumbnail(image2)\r\n    \r\n    h1 = image1.histogram()\r\n    h2 = image2.histogram()\r\n \r\n    rms = math.sqrt(reduce(operator.add,  list(map(lambda a,b: (a-b)**2, h1, h2)))\/len(h1) )\r\n    print (rms)\r\n    return rms\r\n \r\ndef image_similarity_vectors_via_numpy(filepath1, filepath2):\r\n    # source: http:\/\/www.syntacticbayleaves.com\/2008\/12\/03\/determining-image-similarity\/\r\n    # may throw: Value Error: matrices are not aligned . \r\n    \r\n    from numpy import average, linalg, dot\r\n   \r\n    \r\n    image1 = Image.open(filepath1)\r\n    image2 = Image.open(filepath2)\r\n \r\n    image1 = get_thumbnail(image1, stretch_to_fit=True)\r\n    image2 = get_thumbnail(image2, stretch_to_fit=True)\r\n    \r\n    images = [image1, image2]\r\n    vectors = []\r\n    norms = []\r\n    for image in images:\r\n        vector = []\r\n        for pixel_tuple in image.getdata():\r\n            vector.append(average(pixel_tuple))\r\n        vectors.append(vector)\r\n        norms.append(linalg.norm(vector, 2))\r\n    a, b = vectors\r\n    a_norm, b_norm = norms\r\n    # ValueError: matrices are not aligned !\r\n    res = dot(a \/ a_norm, b \/ b_norm)\r\n    print (res)\r\n    return res\r\n\r\n\r\n\r\ndef get_thumbnail(image, size=(128,128), stretch_to_fit=False, greyscale=False):\r\n    \" get a smaller version of the image - makes comparison much faster\/easier\"\r\n    if not stretch_to_fit:\r\n        image.thumbnail(size, Image.ANTIALIAS)\r\n    else:\r\n        image = image.resize(size); # for faster computation\r\n    if greyscale:\r\n        image = image.convert(\"L\")  # Convert it to grayscale.\r\n    return image\r\n\r\n\r\nimage_similarity_histogram_via_pil(local_filenames[0], local_filenames[1])\r\nimage_similarity_vectors_via_numpy(local_filenames[0], local_filenames[1])\r\n    \r\n\r\n\r\nfor i in range(7):\r\n  print (local_filenames[i])  \r\n  for j in range(i+1,8):\r\n      print (local_filenames[j])\r\n      image_similarity_vectors_via_numpy(local_filenames[i], local_filenames[j])\r\n<\/code><\/pre>\n<p><strong>References<\/strong><br \/>\n1. <a href=https:\/\/pixabay.com\/ target=\"_blank\">Pixabay<\/a><br \/>\n2. <a href=https:\/\/pixabay.com\/api\/docs\/ target=\"_blank\">Pixabay API<\/a><br \/>\n3. <a href=https:\/\/pypi.python.org\/pypi\/python-pixabay\/1.1 target=\"_blank\">Python 2 &#038; 3 Pixabay API interface<\/a><br \/>\n4. <a href=http:\/\/www.guguncube.com\/1656\/python-image-similarity-comparison-using-several-techniques target=\"_blank\">Python \u2013 Image Similarity Comparison Using Several Techniques<\/a><br \/>\n5. <a href=https:\/\/en.wikipedia.org\/wiki\/Image_histogram target=\"_blank\">Image histogram<\/a> Wikipedia<\/p>\n","protected":false},"excerpt":{"rendered":"<p>Recently I visited great website Pixabay [1] that offers a wide range of images from people all around the world. These images are free to use even for commercial use. And there is an API [2] for accessing images on Pixabay. This brings a lot of ideas for interesting web applications with using of machine &#8230; <a title=\"Image Processing Using Pixabay API and Python\" class=\"read-more\" href=\"http:\/\/intelligentonlinetools.com\/blog\/2017\/10\/26\/image-processing-using-pixabay-api-and-python\/\">Read more<\/a><\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"jetpack_post_was_ever_published":false,"jetpack_publicize_message":"","jetpack_is_tweetstorm":false,"jetpack_publicize_feature_enabled":true,"jetpack_social_post_already_shared":true,"jetpack_social_options":[]},"categories":[11,2,9,10],"tags":[28,26,18,27],"jetpack_publicize_connections":[],"yoast_head":"<!-- This site is optimized with the Yoast SEO plugin v20.4 - https:\/\/yoast.com\/wordpress\/plugins\/seo\/ -->\n<title>Image Processing Using Pixabay API and Python - Machine Learning Applications<\/title>\n<meta name=\"robots\" content=\"index, follow, max-snippet:-1, max-image-preview:large, max-video-preview:-1\" \/>\n<link rel=\"canonical\" href=\"http:\/\/intelligentonlinetools.com\/blog\/2017\/10\/26\/image-processing-using-pixabay-api-and-python\/\" \/>\n<meta property=\"og:locale\" content=\"en_US\" \/>\n<meta property=\"og:type\" content=\"article\" \/>\n<meta property=\"og:title\" content=\"Image Processing Using Pixabay API and Python - Machine Learning Applications\" \/>\n<meta property=\"og:description\" content=\"Recently I visited great website Pixabay [1] that offers a wide range of images from people all around the world. These images are free to use even for commercial use. And there is an API [2] for accessing images on Pixabay. This brings a lot of ideas for interesting web applications with using of machine ... Read more\" \/>\n<meta property=\"og:url\" content=\"http:\/\/intelligentonlinetools.com\/blog\/2017\/10\/26\/image-processing-using-pixabay-api-and-python\/\" \/>\n<meta property=\"og:site_name\" content=\"Machine Learning Applications\" \/>\n<meta property=\"article:published_time\" content=\"2017-10-26T00:23:03+00:00\" \/>\n<meta property=\"article:modified_time\" content=\"2017-11-03T00:08:05+00:00\" \/>\n<meta name=\"author\" content=\"owygs156\" \/>\n<meta name=\"twitter:card\" content=\"summary_large_image\" \/>\n<meta name=\"twitter:label1\" content=\"Written by\" \/>\n\t<meta name=\"twitter:data1\" content=\"owygs156\" \/>\n\t<meta name=\"twitter:label2\" content=\"Est. reading time\" \/>\n\t<meta name=\"twitter:data2\" content=\"4 minutes\" \/>\n<script type=\"application\/ld+json\" class=\"yoast-schema-graph\">{\"@context\":\"https:\/\/schema.org\",\"@graph\":[{\"@type\":\"WebPage\",\"@id\":\"http:\/\/intelligentonlinetools.com\/blog\/2017\/10\/26\/image-processing-using-pixabay-api-and-python\/\",\"url\":\"http:\/\/intelligentonlinetools.com\/blog\/2017\/10\/26\/image-processing-using-pixabay-api-and-python\/\",\"name\":\"Image Processing Using Pixabay API and Python - Machine Learning Applications\",\"isPartOf\":{\"@id\":\"http:\/\/intelligentonlinetools.com\/blog\/#website\"},\"datePublished\":\"2017-10-26T00:23:03+00:00\",\"dateModified\":\"2017-11-03T00:08:05+00:00\",\"author\":{\"@id\":\"http:\/\/intelligentonlinetools.com\/blog\/#\/schema\/person\/7a886dc5eb9758369af2f6d2cb342478\"},\"breadcrumb\":{\"@id\":\"http:\/\/intelligentonlinetools.com\/blog\/2017\/10\/26\/image-processing-using-pixabay-api-and-python\/#breadcrumb\"},\"inLanguage\":\"en-US\",\"potentialAction\":[{\"@type\":\"ReadAction\",\"target\":[\"http:\/\/intelligentonlinetools.com\/blog\/2017\/10\/26\/image-processing-using-pixabay-api-and-python\/\"]}]},{\"@type\":\"BreadcrumbList\",\"@id\":\"http:\/\/intelligentonlinetools.com\/blog\/2017\/10\/26\/image-processing-using-pixabay-api-and-python\/#breadcrumb\",\"itemListElement\":[{\"@type\":\"ListItem\",\"position\":1,\"name\":\"Home\",\"item\":\"http:\/\/intelligentonlinetools.com\/blog\/\"},{\"@type\":\"ListItem\",\"position\":2,\"name\":\"Image Processing Using Pixabay API and Python\"}]},{\"@type\":\"WebSite\",\"@id\":\"http:\/\/intelligentonlinetools.com\/blog\/#website\",\"url\":\"http:\/\/intelligentonlinetools.com\/blog\/\",\"name\":\"Machine Learning Applications\",\"description\":\"Artificial intelligence, data mining and machine learning for building web based tools and services.\",\"potentialAction\":[{\"@type\":\"SearchAction\",\"target\":{\"@type\":\"EntryPoint\",\"urlTemplate\":\"http:\/\/intelligentonlinetools.com\/blog\/?s={search_term_string}\"},\"query-input\":\"required name=search_term_string\"}],\"inLanguage\":\"en-US\"},{\"@type\":\"Person\",\"@id\":\"http:\/\/intelligentonlinetools.com\/blog\/#\/schema\/person\/7a886dc5eb9758369af2f6d2cb342478\",\"name\":\"owygs156\",\"image\":{\"@type\":\"ImageObject\",\"inLanguage\":\"en-US\",\"@id\":\"http:\/\/intelligentonlinetools.com\/blog\/#\/schema\/person\/image\/\",\"url\":\"http:\/\/2.gravatar.com\/avatar\/b351def598609cb4c0b5bca26497c7e5?s=96&d=mm&r=g\",\"contentUrl\":\"http:\/\/2.gravatar.com\/avatar\/b351def598609cb4c0b5bca26497c7e5?s=96&d=mm&r=g\",\"caption\":\"owygs156\"}}]}<\/script>\n<!-- \/ Yoast SEO plugin. -->","yoast_head_json":{"title":"Image Processing Using Pixabay API and Python - Machine Learning Applications","robots":{"index":"index","follow":"follow","max-snippet":"max-snippet:-1","max-image-preview":"max-image-preview:large","max-video-preview":"max-video-preview:-1"},"canonical":"http:\/\/intelligentonlinetools.com\/blog\/2017\/10\/26\/image-processing-using-pixabay-api-and-python\/","og_locale":"en_US","og_type":"article","og_title":"Image Processing Using Pixabay API and Python - Machine Learning Applications","og_description":"Recently I visited great website Pixabay [1] that offers a wide range of images from people all around the world. These images are free to use even for commercial use. And there is an API [2] for accessing images on Pixabay. This brings a lot of ideas for interesting web applications with using of machine ... Read more","og_url":"http:\/\/intelligentonlinetools.com\/blog\/2017\/10\/26\/image-processing-using-pixabay-api-and-python\/","og_site_name":"Machine Learning Applications","article_published_time":"2017-10-26T00:23:03+00:00","article_modified_time":"2017-11-03T00:08:05+00:00","author":"owygs156","twitter_card":"summary_large_image","twitter_misc":{"Written by":"owygs156","Est. reading time":"4 minutes"},"schema":{"@context":"https:\/\/schema.org","@graph":[{"@type":"WebPage","@id":"http:\/\/intelligentonlinetools.com\/blog\/2017\/10\/26\/image-processing-using-pixabay-api-and-python\/","url":"http:\/\/intelligentonlinetools.com\/blog\/2017\/10\/26\/image-processing-using-pixabay-api-and-python\/","name":"Image Processing Using Pixabay API and Python - Machine Learning Applications","isPartOf":{"@id":"http:\/\/intelligentonlinetools.com\/blog\/#website"},"datePublished":"2017-10-26T00:23:03+00:00","dateModified":"2017-11-03T00:08:05+00:00","author":{"@id":"http:\/\/intelligentonlinetools.com\/blog\/#\/schema\/person\/7a886dc5eb9758369af2f6d2cb342478"},"breadcrumb":{"@id":"http:\/\/intelligentonlinetools.com\/blog\/2017\/10\/26\/image-processing-using-pixabay-api-and-python\/#breadcrumb"},"inLanguage":"en-US","potentialAction":[{"@type":"ReadAction","target":["http:\/\/intelligentonlinetools.com\/blog\/2017\/10\/26\/image-processing-using-pixabay-api-and-python\/"]}]},{"@type":"BreadcrumbList","@id":"http:\/\/intelligentonlinetools.com\/blog\/2017\/10\/26\/image-processing-using-pixabay-api-and-python\/#breadcrumb","itemListElement":[{"@type":"ListItem","position":1,"name":"Home","item":"http:\/\/intelligentonlinetools.com\/blog\/"},{"@type":"ListItem","position":2,"name":"Image Processing Using Pixabay API and Python"}]},{"@type":"WebSite","@id":"http:\/\/intelligentonlinetools.com\/blog\/#website","url":"http:\/\/intelligentonlinetools.com\/blog\/","name":"Machine Learning Applications","description":"Artificial intelligence, data mining and machine learning for building web based tools and services.","potentialAction":[{"@type":"SearchAction","target":{"@type":"EntryPoint","urlTemplate":"http:\/\/intelligentonlinetools.com\/blog\/?s={search_term_string}"},"query-input":"required name=search_term_string"}],"inLanguage":"en-US"},{"@type":"Person","@id":"http:\/\/intelligentonlinetools.com\/blog\/#\/schema\/person\/7a886dc5eb9758369af2f6d2cb342478","name":"owygs156","image":{"@type":"ImageObject","inLanguage":"en-US","@id":"http:\/\/intelligentonlinetools.com\/blog\/#\/schema\/person\/image\/","url":"http:\/\/2.gravatar.com\/avatar\/b351def598609cb4c0b5bca26497c7e5?s=96&d=mm&r=g","contentUrl":"http:\/\/2.gravatar.com\/avatar\/b351def598609cb4c0b5bca26497c7e5?s=96&d=mm&r=g","caption":"owygs156"}}]}},"jetpack_featured_media_url":"","jetpack_sharing_enabled":true,"jetpack_shortlink":"https:\/\/wp.me\/p7h1IJ-mK","jetpack-related-posts":[{"id":2427,"url":"http:\/\/intelligentonlinetools.com\/blog\/2018\/12\/16\/integrating-sentiment-analysis-api-python-django-web-application\/","url_meta":{"origin":1410,"position":0},"title":"Integrating Sentiment Analysis API Python Django into Web Application","date":"December 16, 2018","format":false,"excerpt":"In this post we will learn how to use sentiment analysis with API python from paralleldots.com. We will look at running this API from python environment on laptop and also in web application environment with python Django on pythonanywhere hosting site. In the one of previous post we set python\u2026","rel":"","context":"In &quot;API Programming&quot;","img":{"alt_text":"","src":"https:\/\/i0.wp.com\/intelligentonlinetools.com\/blog\/wp-content\/uploads\/2018\/12\/screenshots_from_sentiment_analysis_API_testing-e1545009465212.png?resize=350%2C200","width":350,"height":200},"classes":[]},{"id":827,"url":"http:\/\/intelligentonlinetools.com\/blog\/2017\/01\/11\/apis\/","url_meta":{"origin":1410,"position":1},"title":"Useful APIs for Your Web Site","date":"January 11, 2017","format":false,"excerpt":"Here\u2019s a useful list of resources on how to create an API, compiled from posts that were published recently on this blog. The included APIs can provide a fantastic ways to enhance websites. 1. The WordPress(WP) API exposes a simple yet powerful interface to WP Query, the posts API, post\u2026","rel":"","context":"In &quot;API Programming&quot;","img":{"alt_text":"","src":"","width":0,"height":0},"classes":[]},{"id":383,"url":"http:\/\/intelligentonlinetools.com\/blog\/2016\/07\/03\/getting-the-data-from-the-web-using-php-for-api-using-the-api-with-php\/","url_meta":{"origin":1410,"position":2},"title":"Getting the Data from the Web using PHP or Python for API","date":"July 3, 2016","format":false,"excerpt":"In the previous posts [1],[2] perl was used to get content from the web through Faroo API and Guardian APIs. In this post PHP and Pyhton will be used to get web data using same APIs. PHP has a powerful JSON parsing mechanism, which, because PHP is a dynamic language,\u2026","rel":"","context":"In &quot;API Programming&quot;","img":{"alt_text":"Trend for Python, Perl, PHP","src":"https:\/\/i0.wp.com\/intelligentonlinetools.com\/blog\/wp-content\/uploads\/2016\/07\/trend_for_python_perl_php-300x144.png?resize=350%2C200","width":350,"height":200},"classes":[]},{"id":510,"url":"http:\/\/intelligentonlinetools.com\/blog\/2016\/08\/19\/getting-data-from-wikipedia-using-python\/","url_meta":{"origin":1410,"position":3},"title":"Getting Data From Wikipedia Using Python","date":"August 19, 2016","format":false,"excerpt":"Recently I come across python package Wikipedia which is a Python library that makes it easy to access and parse data from Wikipedia. Using this library you can search Wikipedia, get article summaries, get data like links and images from a page, and more. Wikipedia wraps the MediaWiki API so\u2026","rel":"","context":"In &quot;API Programming&quot;","img":{"alt_text":"","src":"","width":0,"height":0},"classes":[]},{"id":766,"url":"http:\/\/intelligentonlinetools.com\/blog\/2016\/12\/31\/retrieving-post-data-using-the-wordpress-api-with-python-script\/","url_meta":{"origin":1410,"position":4},"title":"Retrieving Post Data Using the WordPress API with Python Script","date":"December 31, 2016","format":false,"excerpt":"In this post we will create python script that is able to get data from WordPress (WP) blog using WP API. This script will save downloaded data into csv file for further analysis or other purposes. WP API is returning data in json format and is accessible through link http:\/\/hostname.com\/wp-json\/wp\/v2\/posts.\u2026","rel":"","context":"In &quot;API Programming&quot;","img":{"alt_text":"","src":"","width":0,"height":0},"classes":[]},{"id":1178,"url":"http:\/\/intelligentonlinetools.com\/blog\/2017\/05\/14\/time-series-prediction-with-convolutional-neural-networks\/","url_meta":{"origin":1410,"position":5},"title":"Forecasting Time Series Data with Convolutional Neural Networks","date":"May 14, 2017","format":false,"excerpt":"Convolutional neural networks(CNN) is increasingly important concept in computer science and finds more and more applications in different fields. Many posts on the web are about applying convolutional neural networks for image classification as CNN is very useful type of neural networks for image classification. But convolutional neural networks can\u2026","rel":"","context":"In &quot;Artificial Intelligence&quot;","img":{"alt_text":"","src":"https:\/\/i0.wp.com\/intelligentonlinetools.com\/blog\/wp-content\/uploads\/2017\/05\/time-series-LSTM-300x164.png?resize=350%2C200","width":350,"height":200},"classes":[]}],"_links":{"self":[{"href":"http:\/\/intelligentonlinetools.com\/blog\/wp-json\/wp\/v2\/posts\/1410"}],"collection":[{"href":"http:\/\/intelligentonlinetools.com\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"http:\/\/intelligentonlinetools.com\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"http:\/\/intelligentonlinetools.com\/blog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"http:\/\/intelligentonlinetools.com\/blog\/wp-json\/wp\/v2\/comments?post=1410"}],"version-history":[{"count":8,"href":"http:\/\/intelligentonlinetools.com\/blog\/wp-json\/wp\/v2\/posts\/1410\/revisions"}],"predecessor-version":[{"id":1418,"href":"http:\/\/intelligentonlinetools.com\/blog\/wp-json\/wp\/v2\/posts\/1410\/revisions\/1418"}],"wp:attachment":[{"href":"http:\/\/intelligentonlinetools.com\/blog\/wp-json\/wp\/v2\/media?parent=1410"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"http:\/\/intelligentonlinetools.com\/blog\/wp-json\/wp\/v2\/categories?post=1410"},{"taxonomy":"post_tag","embeddable":true,"href":"http:\/\/intelligentonlinetools.com\/blog\/wp-json\/wp\/v2\/tags?post=1410"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}