Skip to content

Instantly share code, notes, and snippets.

@halspg

halspg/hal.py Secret

Created February 27, 2015 14:34
Show Gist options
  • Save halspg/0a52cbf3d1d666e94cc2 to your computer and use it in GitHub Desktop.
Save halspg/0a52cbf3d1d666e94cc2 to your computer and use it in GitHub Desktop.
Pythonista用モジュール
# -*- coding: utf-8 -*-
from PIL import Image
import photos
import console
import requests
import bs4
import urllib
blog = 'http://halspg.wordpress.com/'
twitter = 'http://twitter.com/hals_pg'
def statusbarcrop(count=1):
s = []
for var in range(0, count):
temp = photos.pick_image()
w, h = temp.size
box = (0, 40, w, h)
temp = temp.crop(box)
s.append(temp)
return s
def imagesmerge(count=2, space=0, canvas='#ffffff', direction='h'):
w = []
h = []
s = []
width = 0
height = 0
for var in range(0, count):
temp = photos.pick_image()
size = list(temp.size)
w.append(size[0])
h.append(size[1])
s.append(temp)
if direction == 'h':
for i in range(0, len(w)):
width += w[i]
height = max(h)
bg = Image.new('RGB', (width, height), canvas)
bg.paste(s[0], (0, 0))
j = 0
for i in range(1, count):
j += w[i-1]
bg.paste(s[i], ((space * i) + j, 0))
elif direction == 'v':
for i in range(0, len(h)):
height += h[i]
width = max(w)
bg = Image.new('RGB', (width, height), canvas)
bg.paste(s[0], (0, 0))
j = 0
for i in range(1, count):
j += h[i-1]
bg.paste(s[i], (0, (space * i) + j))
return bg
def imagesresize(width=300):
height = 0
temp = photos.pick_image()
w, h = temp.size
height = int(h * (h * 1.0 / width))
temp.thumbnail((width, height), Image.ANTIALIAS)
return temp
def screenshotscropresizemerge(count=2, stb_crop=True, width=500, space=0, canvas='#ffffff'):
rwidth = int((width * 1.0 / count) - (space * (count - 1) / count))
height = []
s = []
w = []
h = []
for i in range(0, count):
temp = photos.pick_image()
size = list(temp.size)
if stb_crop is True:
box = (0, 40, size[0], size[1])
temp = temp.crop(box)
size = list(temp.size)
w.append(size[0])
h.append(size[1])
height.append(int(h[i] * (rwidth * 1.0 / w[i])))
temp.thumbnail((rwidth, height[i]), Image.ANTIALIAS)
s.append(temp)
bg = Image.new('RGB', (width, max(height)), canvas)
bg.paste(s[0], (0, 0))
for i in range(1, count):
bg.paste(s[i], ((space * i) + (rwidth * i), 0))
return bg
def gettitle(url):
headers = {'User-Agent': "Mozilla/5.0 (Macintosh; Intel Mac OS X 10_10) AppleWebKit/538.34.48 (KHTML, like Gecko) Version/8.0 Safari/538.35.8"}
conn = requests.get(url, headers = headers)
soup = bs4.BeautifulSoup(conn.text)
return soup.title.string.encode('utf-8')
def shortentinyurl(url):
req = 'http://tinyurl.com/api-create.php?url={}'.format(urllib.quote(url.encode('utf-8')))
return urllib.urlopen(req).read()
if __name__ == '__main__':
settings = {
'count': 2,
'stb_crop': True,
'width': 500,
'space': 0,
'canvas': '#ffffff'
}
import sys
arg = sys.argv
if len(arg) >= 3:
count = int(arg[2])
if len(arg) >= 2:
a = int(arg[1])
if a == 1:
photos.save_image(statusbarcrop(count))
elif a == 2:
photos.save_image(imagesmerge(count))
elif a == 3:
if len(arg) >= 3:
width = int(arg[2])
photos.save_image(imagesresize(width))
else: photos.save_image(imagesresize())
elif a == 4:
photos.save_image(screenshotscropresizemerge(count))
else:
photos.save_image(screenshotscropresizemerge(**settings))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment