def show(server, msg, pic): if not pic: pic = server.lasturl elif not pic.startswith("http"): params = { "safe": "off", "v": "1.0", "rsz": 1, "q": pic } pic = requests.get( "https://ajax.googleapis.com/ajax/services/search/images", params=params ).json()["responseData"]["results"][0]["url"] server.lasturl = pic if msg.prefix == "!": k = 16 else: k = 6 data = requests.get(pic).content data = BytesIO(data) img = Image.open(data) if img.size[0] > 4096 or img.size[1] > 4096: return "│ Image too large." scalefactor = min(img.size[0]*3/k, img.size[1]/k) img = img.resize((int(img.size[0]*3/scalefactor) * 2, int(img.size[1]/scalefactor)*2), Image.ANTIALIAS) if img.size[0] > 110: scalefactor = 110 / img.size[0] img = img.resize((int(scalefactor * img.size[0]), int(scalefactor * img.size[1])), Image.ANTIALIAS) img = img.convert("RGBA") img.load() # needed for split() background = Image.new('RGB', img.size, (255,255,255)) background.paste(img, mask=img.split()[3]) # 3 is the alpha channel return irc_render(background)
def show(server, msg, pic): if not pic: pic = server.lasturl elif not pic.startswith("http"): params = { "safe": "off", "v": "1.0", "rsz": 1, "q": pic } pic = requests.get( "https://ajax.googleapis.com/ajax/services/search/images", params=params ).json()["responseData"]["results"][0]["url"] server.lasturl = pic if msg.prefix == "!": h_max = 16 else: h_max = 6 w_max = 18 w_res, h_res = 6, 2 data = requests.get(pic).content data = BytesIO(data) img = Image.open(data) if img.size[0] > 4096 or img.size[1] > 4096: return "│ Image too large." scalefactor = max(img.size[0]/w_max, img.size[1]/h_max) x, y = img.size[0]/scalefactor, img.size[1]/scalefactor img = img.resize((int(x) * w_res, int(y)*h_res), Image.ANTIALIAS) background = flatten(img) return irc_render(background)
def show(server, msg, pic): if not pic: pic = server.lasturl elif not pic.startswith("http"): params = {"safe": "off", "v": "1.0", "rsz": 1, "q": pic} pic = requests.get( "https://ajax.googleapis.com/ajax/services/search/images", params=params).json()["responseData"]["results"][0]["url"] server.lasturl = pic if msg.prefix == "!": h_max = 16 else: h_max = 6 w_max = 18 w_res, h_res = 6, 2 data = requests.get(pic).content data = BytesIO(data) img = Image.open(data) if img.size[0] > 4096 or img.size[1] > 4096: return "│ Image too large." scalefactor = max(img.size[0] / w_max, img.size[1] / h_max) x, y = img.size[0] / scalefactor, img.size[1] / scalefactor img = img.resize((int(x) * w_res, int(y) * h_res), Image.ANTIALIAS) background = flatten(img) return irc_render(background)