def img_zoom(jenni, msg): url = msg.groups()[0] if not url: imgs = load_db().get(msg.sender) if imgs and 'last_said' in imgs: url_regex = "(https?://\S+\.(?:jpg|png|jpeg|gif))" for img in reversed(imgs['last_said']): urls = re.findall(url_regex, img) if urls: url = random.choice(urls) break if not url: return filename = enhance.zoom(url) url = "http://brittbot.brittg.com/{}".format(filename) msgs = load_db() msgs[msg.sender]['last_said'].append(url) save_db(msgs) jenni.reply(url)
def img_zoom(jenni, msg): from modules.brittbot.pil import enhance from modules.find import load_db, save_db url = msg.groups()[0] if not url: imgs = load_db().get(msg.sender) if imgs and 'last_said' in imgs: url_regex = "(https?://\S+\.(?:jpg|png|jpeg|gif))" for img in reversed(imgs['last_said']): urls = re.findall(url_regex, img) if urls: url = random.choice(urls) break if not url: return filename = enhance.zoom(url) url = "http://brittbot.brittg.com/{}".format(filename) msgs = load_db() msgs[msg.sender]['last_said'].append(url) save_db(msgs) jenni.reply(url)
def img_enhance(jenni, msg): from modules.brittbot.pil import enhance from modules.find import load_db, save_db url = msg.groups()[0] if not url: imgs = load_db().get(msg.sender) if imgs and "last_said" in imgs: url_regex = "(https?://\S+\.(?:jpg|png|jpeg|gif))" for img in reversed(imgs["last_said"]): urls = re.findall(url_regex, img) if urls: url = random.choice(urls) break if not url: return filename = enhance.enhance(url) url = "http://brittbot.brittg.com/{}".format(filename) msgs = load_db() msgs[msg.sender]["last_said"].append(url) save_db(msgs) jenni.reply(url)
def deepdream(jenni, msg): import requests from requests_toolbelt import MultipartEncoder import urllib import uuid from modules.find import load_db, save_db if 'last_trip' in jenni.brain and time.time() - jenni.brain['last_trip'] < 30: jenni.reply('Tripping too hard right now, please try again in 30 seconds.') return in_url = msg.groups()[0] if not in_url: imgs = load_db().get(msg.sender) if imgs and 'last_said' in imgs: url_regex = "(https?://\S+\.(?:jpg|jpeg))" for img in reversed(imgs['last_said']): urls = re.findall(url_regex, img) if urls: in_url = random.choice(urls) break if not in_url: return f = open('/tmp/img.jpg', 'wb') f.write(urllib.urlopen(in_url).read()) f.close() data = MultipartEncoder({ 'title': 'wat', 'description': 'none', 'filter': 'trippy', 'submit': "Let's Dream", 'image': ('img0.jpg', open('/tmp/img.jpg', 'rb'), 'image/jpeg'), }) response = requests.post( 'https://dreamscopeapp.com/api/images', data=data, verify=False, headers={ 'Content-Type': data.content_type } ) if response.status_code >= 400: print response print response.content jenni.reply("An error has occurred.") image_id = re.findall(r'pageUrl\(\'(\S+)\'\)', response.text)[-1] for _ in range(15): time.sleep(1.5) r = requests.get('https://dreamscopeapp.com/api/images/{}'.format(image_id)) final_url = r.json()['filtered_url'] if final_url: break if not final_url: jenni.reply('Deep dream took too long to complete. Try a smaller image.') return jenni.brain['last_trip'] = time.time() img = urllib.urlopen(final_url).read() imagepath = '/var/www/htdocs/brittbot/' imagename = "%s.jpg" % str(uuid.uuid4()).replace('-', '')[0:8] f = open(imagepath + imagename, 'w') f.write(img) f.close() url = "http://brittbot.brittg.com/{}".format(imagename) msgs = load_db() msgs[msg.sender]['last_said'].append(url) save_db(msgs) jenni.reply(url)
def deepdream(jenni, msg): import requests from requests_toolbelt import MultipartEncoder import urllib import uuid from modules.find import load_db, save_db if 'last_trip' in jenni.brain and time.time() - jenni.brain['last_trip'] < 30: jenni.reply('Tripping too hard right now, please try again in 30 seconds.') return img_filter = msg.groups()[0] in_url = msg.groups()[1] if img_filter in ['lsd', 'deepdream']: img_filter = 'trippy' if not in_url: imgs = load_db().get(msg.sender) if imgs and 'last_said' in imgs: url_regex = "(https?://\S+\.(?:jpg|png|jpeg))" for img in reversed(imgs['last_said']): urls = re.findall(url_regex, img) if urls: in_url = random.choice(urls) break if not in_url: return f = open('/tmp/img.jpg', 'wb') f.write(urllib.urlopen(in_url).read()) f.close() data = MultipartEncoder({ 'title': 'wat', 'description': 'none', 'filter': img_filter, 'submit': "Let's Dream", 'image': ('img0.jpg', open('/tmp/img.jpg', 'rb'), 'image/jpeg'), }) response = requests.post( 'https://dreamscopeapp.com/api/images', data=data, verify=False, headers={ 'Content-Type': data.content_type } ) if response.status_code >= 400: print response print response.content jenni.reply("An error has occurred.") image_id = json.loads(response.text)['uuid'] for _ in range(15): time.sleep(1.5) r = requests.get('https://dreamscopeapp.com/api/images/{}'.format(image_id)) final_url = r.json()['filtered_url'] if final_url: break if not final_url: jenni.reply('Deep dream took too long to complete. Try a smaller image.') return jenni.brain['last_trip'] = time.time() img = urllib.urlopen(final_url).read() imagepath = '/var/www/htdocs/brittbot/' imagename = "%s.jpg" % str(uuid.uuid4()).replace('-', '')[0:8] f = open(imagepath + imagename, 'w') f.write(img) f.close() url = "http://brittbot.brittg.com/{}".format(imagename) msgs = load_db() msgs[msg.sender]['last_said'].append(url) save_db(msgs) jenni.reply(url)
def deepdream(jenni, msg): import requests from requests_toolbelt import MultipartEncoder import urllib import uuid from modules.find import load_db, save_db if "last_trip" in jenni.brain and time.time() - jenni.brain["last_trip"] < 30: jenni.reply("Tripping too hard right now, please try again in 30 seconds.") return img_filter = msg.groups()[0] in_url = msg.groups()[1] if img_filter in ["lsd", "deepdream"]: img_filter = "trippy" if not in_url: imgs = load_db().get(msg.sender) if imgs and "last_said" in imgs: url_regex = "(https?://\S+\.(?:jpg|png|jpeg))" for img in reversed(imgs["last_said"]): urls = re.findall(url_regex, img) if urls: in_url = random.choice(urls) break if not in_url: return f = open("/tmp/img.jpg", "wb") f.write(urllib.urlopen(in_url).read()) f.close() data = MultipartEncoder( { "title": "wat", "description": "none", "filter": img_filter, "submit": "Let's Dream", "image": ("img0.jpg", open("/tmp/img.jpg", "rb"), "image/jpeg"), } ) response = requests.post( "https://dreamscopeapp.com/api/images", data=data, verify=False, headers={"Content-Type": data.content_type} ) if response.status_code >= 400: print response print response.content jenni.reply("An error has occurred.") image_id = json.loads(response.text)["uuid"] for _ in range(15): time.sleep(1.5) r = requests.get("https://dreamscopeapp.com/api/images/{}".format(image_id)) final_url = r.json()["filtered_url"] if final_url: break if not final_url: jenni.reply("Deep dream took too long to complete. Try a smaller image.") return jenni.brain["last_trip"] = time.time() img = urllib.urlopen(final_url).read() imagepath = "/var/www/htdocs/brittbot/" imagename = "%s.jpg" % str(uuid.uuid4()).replace("-", "")[0:8] f = open(imagepath + imagename, "w") f.write(img) f.close() url = "http://brittbot.brittg.com/{}".format(imagename) msgs = load_db() msgs[msg.sender]["last_said"].append(url) save_db(msgs) jenni.reply(url)
def deepdream(jenni, msg): if 'last_trip' in jenni.brain and time.time() - jenni.brain['last_trip'] < 30: jenni.reply('Tripping too hard right now, please try again in 30 seconds.') return img_filter = msg.groups()[0] in_url = msg.groups()[1] if img_filter in ['lsd', 'deepdream']: img_filter = 'trippy' if not in_url: imgs = load_db().get(msg.sender) if imgs and 'last_said' in imgs: url_regex = "(https?://\S+\.(?:jpg|png|jpeg))" for img in reversed(imgs['last_said']): urls = re.findall(url_regex, img) if urls: in_url = random.choice(urls) break if not in_url: return f = open('/tmp/img.jpg', 'wb') f.write(urllib.urlopen(in_url).read()) f.close() if in_url.endswith('.gif'): from PIL import ( Image, ImageSequence, ) import io from modules.brittbot.pil.images2gif import writeGif img = Image.open('/tmp/img.jpg') if hasattr(img, 'format') and img.format.lower() == 'gif': if not msg.admin: return original_duration = img.info['duration'] frames = [frame.copy() for frame in ImageSequence.Iterator(img)] if len(frames) > 40: jenni.reply("Too many frames :(") return final_frames = [] for i, frame in enumerate(frames): im = frame.convert('RGBA') im.save('/tmp/frame{}.jpg'.format(i), 'jpeg', quality=100) print "Processing frame {}...".format(i) url = process_deepdream(open('/tmp/frame{}.jpg'.format(i), 'rb'), img_filter) im = Image.open(io.BytesIO(urllib.urlopen(url).read())) final_frames.append(im) time.sleep(2) imagepath = '/var/www/htdocs/brittbot/' imagename = "%s.gif" % str(uuid.uuid4()).replace('-', '')[0:8] writeGif(imagepath + imagename, final_frames, duration=original_duration/1000.0, dither=0) else: final_url = process_deepdream(open('/tmp/img.jpg', 'rb'), img_filter) jenni.brain['last_trip'] = time.time() img = urllib.urlopen(final_url).read() imagepath = '/var/www/htdocs/brittbot/' imagename = "%s.jpg" % str(uuid.uuid4()).replace('-', '')[0:8] f = open(imagepath + imagename, 'w') f.write(img) f.close() url = "http://brittbot.brittg.com/{}".format(imagename) msgs = load_db() msgs[msg.sender]['last_said'].append(url) save_db(msgs) jenni.reply(url)