def get_image_size(self, data): if isinstance(data, dict): img_file = data['name'] img_path = data['path'] if (exists("{}{}".format(img_path, img_file)) and isfile("{}{}".format(img_path, img_file))): if img_file[len(img_file) - 3:] == 'png' or 'jpg' or 'jpeg': try: img = Image.open("{}{}".format(img_path, img_file)) size = img.size width = int(re.search("\d+", str(size[0])).group()) height = int(re.search("\d+", str(size[1])).group()) return {'status': "OK", 'message': "Original file is found", 'width': width, 'height': height } except IOError: return {'status': "ERROR", 'message': "Original file is not found" } else: return {'status': "ERROR", 'message': "Original file is not supported: {}".format(img_file[len(img_file) - 3:]) }
def conv_image(self, data): if isinstance(data, dict): img_file = data['name'] img_path = data['path'] new_format = data['format'] new_width = data['width'] new_height = data['height'] size = new_width, new_height outfile = "{}{}{}".format(img_path, str(img_file[:len(img_file) - 3]), str(new_format.lower())) origfile = "{}{}".format(img_path, str(img_file)) if (isinstance(data, dict) and exists("{}{}".format(img_path, img_file)) is True and isfile("{}{}".format(img_path, img_file)) is True and outfile is not origfile): try: if img_file[len(img_file) - 3:] == 'png' or 'jpg' or 'jpeg': img = Image.open("{}{}".format(img_path, img_file)) img.thumbnail(size) img.save(outfile, new_format) return {'status': "OK", 'message': "Original file is converted to {}".format( outfile)} except IOError: return {'status': "ERROR", 'message': "Original file is not changed"}
def _make_thumbnail(self, filename, size): with closing(storage.get(self.filename)) as f: img = Image.open(f) if size != img.size: img = img.resize(size) buf = BytesIO() buf.name = filename img.save(buf) buf.seek(0) storage.put(filename, buf, overwrite=True)
def create(cls, commit=True, url=None, recipe=None, **kwargs): filename = storage.upload(recipe.name, url) with closing(storage.get(filename)) as f: img = Image.open(f) w, h = img.size return super().create(commit, filename=filename, recipe=recipe, _width=w, _height=h, **kwargs)
def add_folder_art(path): # need to check for valid art here (while loop?) # add command for global do not overwrite # check for JPGs art_file = os.path.join(path, 'folder.jpg') good_art = False if os.path.isfile(art_file): try: img = Image.open(art_file) good_art = True except IOError: good_art = False os.remove(art_file) if good_art is True: print art_file + ' found - ' + str(img.size) + 'px. Overwrite?' if yes_no() is False: print 'using existing art' else: os.remove(art_file) download_art(path, art_file) elif os.path.isdir(path): download_art(path, art_file)
def list_info(track, folder): # for FLACs we don't print the zero padding that is actually in the track # number print track.file_name print track.pprint print 'Artist: ' + track.tag['artist'] print 'Album Artist: ' + track.tag['album_artist'] print 'Track#: ' + track.tag['track_number'] print 'Title: ' + track.tag['title'] print 'Album: ' + track.tag['album'] print 'Year: ' + track.tag['year'] print 'Genre: ' + track.tag['genre'] art_file = os.path.join(folder, 'folder.jpg') if os.path.isfile(art_file): try: img = Image.open(art_file) print art_file + ' found - ' + str(img.size) + 'px.' + '\n\n' except IOError: print 'Unable to open cover art. This may or may not be a problem.\ Check cover art manually:' pwd = os.getcwd() os.chdir(folder) Handler = SimpleHTTPServer.SimpleHTTPRequestHandler httpd = SocketServer.TCPServer(("", 9999), Handler) interfaces = netifaces.interfaces() for i in interfaces: if i == 'lo': continue iface = netifaces.ifaddresses(i).get(netifaces.AF_INET) if iface is not None: for j in iface: print 'http://' + j['addr'] + ':9999/folder.jpg' print 'refresh page to continue...' httpd.handle_request() httpd.server_close() os.chdir(pwd)
import requests import time from selenium import webdriver from PilLite import Image from io import BytesIO url = "https://unsplash.com" driver = webdriver.Firefox(executable_path=r'./geckodriver') driver.get(url) driver.execute_script("window.scrollTo(0,1000);") time.sleep(5) image_elements = driver.find_elements_by_css_selector("#gridMulti img") i = 0 for image_element in image_elements: image_url = image_element.get_attribute("src") # Send an HTTP GET request, get and save the image from the response image_object = requests.get(image_url) image = Image.open(BytesIO(image_object.content)) image.save("./images/image" + str(i) + "." + image.format, image.format) i += 1
def create_thumb(filename): uploaded_img_thumb = Image.open("/".join((app.config["UPLOADED_IMG_DEST"], filename))) uploaded_img_thumb.thumbnail(THUMBS_SIZE) uploaded_img_thumb.save("/".join((app.config["UPLOADED_THUMB_DEST"], filename)))