Visual

Images

In [1]:
import requests
from PIL import Image
from io import BytesIO
response = requests.get('https://data-science-workshop.solsort.com/image2.jpg')
src = Image.open(BytesIO(response.content))
src.resize([300,200])
Out[1]:

wordcloud

In [2]:
%matplotlib inline
!pip install wordcloud
Requirement already satisfied: wordcloud in /home/rasmuserik/anaconda3/lib/python3.6/site-packages
Requirement already satisfied: numpy>=1.6.1 in /home/rasmuserik/anaconda3/lib/python3.6/site-packages (from wordcloud)
Requirement already satisfied: pillow in /home/rasmuserik/anaconda3/lib/python3.6/site-packages (from wordcloud)
Requirement already satisfied: matplotlib in /home/rasmuserik/anaconda3/lib/python3.6/site-packages (from wordcloud)
Requirement already satisfied: olefile in /home/rasmuserik/anaconda3/lib/python3.6/site-packages (from pillow->wordcloud)
Requirement already satisfied: six>=1.10 in /home/rasmuserik/anaconda3/lib/python3.6/site-packages (from matplotlib->wordcloud)
Requirement already satisfied: python-dateutil>=2.0 in /home/rasmuserik/anaconda3/lib/python3.6/site-packages (from matplotlib->wordcloud)
Requirement already satisfied: pytz in /home/rasmuserik/anaconda3/lib/python3.6/site-packages (from matplotlib->wordcloud)
Requirement already satisfied: cycler>=0.10 in /home/rasmuserik/anaconda3/lib/python3.6/site-packages (from matplotlib->wordcloud)
Requirement already satisfied: pyparsing!=2.0.4,!=2.1.2,!=2.1.6,>=2.0.1 in /home/rasmuserik/anaconda3/lib/python3.6/site-packages (from matplotlib->wordcloud)
In [3]:
from wordcloud import WordCloud
wordcloud = WordCloud(
    width=400,
    height=300,
    max_words=4000,
    stopwords=["Project", "Gutenberg"],
    background_color= 'white', 
    max_font_size=200,
    random_state=123
).generate_from_frequencies({
    "hello": 100,
    "world": 50,
    "this": 30,
    "is": 30,
    "a": 40,
    "demo": 100,
    "of": 10,
    "wordcloud": 100
})
wordcloud.to_image()
Out[3]:

graphs

In [4]:
!pip install plotly
Requirement already satisfied: plotly in /home/rasmuserik/anaconda3/lib/python3.6/site-packages
Requirement already satisfied: six in /home/rasmuserik/anaconda3/lib/python3.6/site-packages (from plotly)
Requirement already satisfied: requests in /home/rasmuserik/anaconda3/lib/python3.6/site-packages (from plotly)
Requirement already satisfied: pytz in /home/rasmuserik/anaconda3/lib/python3.6/site-packages (from plotly)
Requirement already satisfied: decorator>=4.0.6 in /home/rasmuserik/anaconda3/lib/python3.6/site-packages (from plotly)
Requirement already satisfied: nbformat>=4.2 in /home/rasmuserik/anaconda3/lib/python3.6/site-packages (from plotly)
Requirement already satisfied: chardet<3.1.0,>=3.0.2 in /home/rasmuserik/anaconda3/lib/python3.6/site-packages (from requests->plotly)
Requirement already satisfied: idna<2.7,>=2.5 in /home/rasmuserik/anaconda3/lib/python3.6/site-packages (from requests->plotly)
Requirement already satisfied: urllib3<1.23,>=1.21.1 in /home/rasmuserik/anaconda3/lib/python3.6/site-packages (from requests->plotly)
Requirement already satisfied: certifi>=2017.4.17 in /home/rasmuserik/anaconda3/lib/python3.6/site-packages (from requests->plotly)
Requirement already satisfied: ipython_genutils in /home/rasmuserik/anaconda3/lib/python3.6/site-packages (from nbformat>=4.2->plotly)
Requirement already satisfied: traitlets>=4.1 in /home/rasmuserik/anaconda3/lib/python3.6/site-packages (from nbformat>=4.2->plotly)
Requirement already satisfied: jsonschema!=2.5.0,>=2.4 in /home/rasmuserik/anaconda3/lib/python3.6/site-packages (from nbformat>=4.2->plotly)
Requirement already satisfied: jupyter_core in /home/rasmuserik/anaconda3/lib/python3.6/site-packages (from nbformat>=4.2->plotly)
In [5]:
from plotly.offline import download_plotlyjs, init_notebook_mode, iplot
init_notebook_mode(connected=True)
from plotly import graph_objs 

data = [graph_objs.Bar(x=['a','b','c'], y=[3,1,2])]

iplot(data)

More images

In [6]:
from PIL import Image
from io import BytesIO
response = requests.get('https://data-science-workshop.solsort.com/image3.jpg')
src = Image.open(BytesIO(response.content))
In [7]:
src = src.resize([300,200], Image.ANTIALIAS)
display(src)
In [8]:
image = Image.new('RGB', (600, 200))
display(image)
In [9]:
image.paste(src, [300,0])
display(image)
In [10]:
from PIL import ImageOps
ImageOps.mirror(src)
Out[10]:
In [11]:
image.paste(ImageOps.mirror(src), [0,0])
display(image)

Image effects and filters

In [12]:
src.quantize(colors=30)
Out[12]:
In [13]:
src.effect_spread(5)
Out[13]:
In [14]:
src.resize([60,40]).resize([300,200])
Out[14]:
In [15]:
from PIL import ImageFilter
src.filter(ImageFilter.EDGE_ENHANCE)
Out[15]:

Image Pixels

In [16]:
response = requests.get('https://data-science-workshop.solsort.com/image1.jpg')
src = Image.open(BytesIO(response.content))
src = src.resize([150,100])
display(src)
r = [];
g = [];
b = [];
cols = [];
for x in range(0, src.size[0]):
    for y in range(0, src.size[1]):
        px = src.getpixel((x,y))
        r.append(px[0])
        g.append(px[1])
        b.append(px[2])
        cols.append("rgb" + str(px))
plot = graph_objs.Scatter3d(x=r,
                            y=g,
                            z=b,
                            mode="markers",
                            marker={
                                "size": 1,
                                "color": cols
                            })
iplot([plot])