[DTW] Week06

This week’s assignment is about manipulating images scrapped from the web.
I scrapped the image results of ‘Chris Van Allsburg’, who is my favourite illustrator, from Pinterest, made a tiled image with those scrapped jpegs and added a text title to it.
Since I only wanted his illustration images, I had to delete other images manually from the automated scrap results.
I had 131 images in total after the deleting job and Selenium, Glob, Pillow was used for web browser scrolling/scrapping, making image tiles and adding text, respectively.

The source code for this process is like below:

[pinterest.py] – for image download

import requests
from selenium import webdriver
import os
import time


def download_file(url, local_filename=None):

    #from: https://stackoverflow.com/questions/16694907/how-to-download-large-file-in-python-with-requests-py

    if local_filename is None:
        local_filename = url.split('/')[-1]

    if os.path.exists(local_filename):
        return local_filename

    # NOTE the stream=True parameter
    r = requests.get(url, stream=True)
    with open(local_filename, 'wb') as f:
        for chunk in r.iter_content(chunk_size=1024): 
            if chunk: # filter out keep-alive new chunks
                f.write(chunk)
                #f.flush() commented by recommendation from J.F.Sebastian
    return local_filename


driver = webdriver.Chrome()
driver.get('https://www.pinterest.co.kr/search/pins/?q=chris%20van%20allsburg&rs=typed&term_meta[]=chris%7Ctyped&term_meta[]=van%7Ctyped&term_meta[]=allsburg%7Ctyped')

for x in range(0, 10):
    time.sleep(3)
    driver.execute_script('window.scrollTo(0, document.body.scrollHeight);')

images = driver.find_elements_by_css_selector('img')

for i, image in enumerate(images):
  #print image.get_attribute('src')
  url = image.get_attribute('src')
  savedname = 'pinterest/' + str(i) + '.jpg'
  download_file(url, savedname)

[christile.py]

import glob
from PIL import Image
import random

blank_image = Image.new('RGB', (2200, 1900), (255, 255, 255))
blank_image.save('white.jpg')

# grap all the *.jpg files in 'babies' folder
jpegs = glob.glob('pinterest/*.jpg')

x=0
y=0

for jpg in jpegs:
  # print jpg
  im = Image.open(jpg)
  im.thumbnail((200, 200))
  w = im.size[0]
  h = im.size[1]
  blank_image.paste(im, (x, y))

  x += w
  if x>2000:
    x=0
    y+=h


blank_image.save('chrisvanallsburg3.jpg')

[christext.py]

import requests
import os
from PIL import Image, ImageFilter, ImageDraw, ImageFont



def write_text(imgfilename, text):
  im = Image.open(imgfilename)
  canvas = ImageDraw.Draw(im)
#	canvas.rectangle([0, 125, im.size[0], 166], fill=(0,0,0))
  fnt = ImageFont.truetype('/Library/Fonts/Butler_Bold.otf', 170)
  canvas.text((200, 800), text, font=fnt, fill=(255, 255, 0))
  im.save(imgfilename + '.modified.jpg')


write_text('chrisvanallsburg3.jpg', 'Chris Van Allsburg')

Following is the result:

 

 

Leave a Reply

Your email address will not be published. Required fields are marked *