本文整理汇总了Python中ImageUtils.ImageUtils类的典型用法代码示例。如果您正苦于以下问题:Python ImageUtils类的具体用法?Python ImageUtils怎么用?Python ImageUtils使用的例子?那么恭喜您, 这里精选的类代码示例或许可以为您提供帮助。
在下文中一共展示了ImageUtils类的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。
示例1: setup_loggers_for_user
def setup_loggers_for_user(self, user):
# Create directories if needed
user_dir = path.join(ImageUtils.get_root(), 'content', user)
ImageUtils.create_subdirectories(user_dir)
# Setup logger
self.logger = open(path.join(user_dir, 'history.log'), 'a')
self.db.logger = self.logger
ImageUtils.logger = self.logger
self.reddit.logger = self.logger
示例2: add_existing_image
def add_existing_image(self, user, oldimage, oldpath, subdir='', album_id=-1):
if 'tumblr' in oldpath:
# Can't properly handle tumblr links
self.debug('cannot properly handle tumblr links; trying anyway')
#return
if subdir == '' and album_id == -1:
self.debug('adding image: %s' % oldpath)
# Ensure image is an actual image
try:
dims = ImageUtils.get_dimensions(oldpath)
except:
self.debug('failed to load image: %s, skipping' % oldpath)
return
newimage = path.join(ImageUtils.get_root(), 'content', user, subdir, oldimage)
newimage = newimage.replace('.jpeg.jpg', '.jpg')
thumbnail = path.join(ImageUtils.get_root(), 'content', user, subdir, 'thumbs', oldimage)
thumbnail = thumbnail.replace('.jpeg.jpg', '.jpg')
if path.exists(newimage):
self.debug('new image already exists: %s' % newimage)
return
ImageUtils.create_subdirectories(path.join(ImageUtils.get_root(), 'content', user, subdir, 'thumbs'))
copy2(oldpath, newimage)
try:
ImageUtils.create_thumbnail(newimage, thumbnail)
except Exception, e:
self.debug('failed to create thumbnail: %s' % str(e))
thumbnail = path.join(ImageUtils.get_root(), 'images', 'nothumb.png')
示例3: setup_loggers_for_user
def setup_loggers_for_user(self, user):
# Create directories if needed
user_dir = path.join(ImageUtils.get_root(), 'content', user)
ImageUtils.create_subdirectories(user_dir)
# Setup logger
log_level = self.db.get_config('log_level', default='user')
if log_level == 'none': self.logger = open(devnull, 'w')
elif log_level == 'user': self.logger = open(path.join(user_dir, 'history.log'), 'a')
elif log_level == 'global': self.logger = self.root_log
self.db.logger = self.logger
ImageUtils.logger = self.logger
self.reddit.logger = self.logger
示例4: start
def start(self):
stale_count = self.db.count('urls', 'pending != 0')
if stale_count > 0:
print 'MAIN: found %d stale (interrupted) URLs, marking as non-pending...' % stale_count
self.db.update('urls', 'pending = 0')
self.db.commit()
print 'MAIN: starting infinite loop...'
already_printed_sleep_msg = False
while True:
sleep(0.1)
while len(self.results) > 0:
# self.results is the list of downloaded medias to be added to the DB
result = self.results.pop()
self.handle_result(result)
# Remove recently-completed rips
while len(self.to_remove) > 0:
(albumid, iindex) = self.to_remove.pop()
self.db.delete('urls', 'album_id = ? and i_index = ?', [ albumid, iindex ] )
self.db.commit()
try:
# Get next URL to retrieve
url = self.get_next_url()
except Exception, e:
if str(e) == 'no URLs found':
if not already_printed_sleep_msg:
already_printed_sleep_msg = True
print 'MAIN: no urls to get, sleeping 500ms'
sleep(0.5)
else:
print 'MAIN: get_next_url(): Exception: %s:\n%s' % (str(e), format_exc())
continue
# We have a URL to download & add to DB (url)
already_printed_sleep_msg = False
# Wait for thread count to drop
while len(self.current_threads) >= MAX_THREADS:
sleep(0.1)
self.current_threads.append(None)
# Create new thread to download the media, add to self.results
print 'MAIN: %s #%d: launching handler for: %s' % (url['path'], url['i_index'], url['url'])
# Create subdirs from main thread to avoid race condition
dirname = path.join(ImageUtils.get_root(), 'rips', url['path'], 'thumbs')
ImageUtils.create_subdirectories(dirname)
args = (url,)
t = Thread(target=self.retrieve_result_from_url, args=args)
t.start()
示例5: __init__
def __init__(self):
# Single file that all output is written to, to track usage
self.exit_if_already_started()
self.root_log = open(path.join(ImageUtils.get_root(), 'history.log'), 'a')
self.logger = self.root_log # Logger used by helper classes
self.db = DB() # Database instance
self.reddit = Reddit()
self.excluded_subs = self.db.get_excluded_subreddits()
示例6: delete_album
def delete_album(self, cursor, rowid, path):
# Delete images
cursor.execute('delete from medias where album_id = ?', [rowid])
# Delete pending URLs
cursor.execute('delete from urls where album_id = ?', [rowid])
# Delete album
cursor.execute('delete from albums where path = ?', [path])
# Delete directory + files
path = ospath.join(ImageUtils.get_root(), path)
rmtree(path)
示例7: __init__
class Ambilight:
def __init__(self, led_num, border):
self.NUMLED = led_num
self.BORDER = border
self.imageUtils = ImageUtils()
self.currentColors = collections.OrderedDict()
self.current_image_all = None
def run(self):
self.current_image_all = self.imageUtils.concatStripe(self.imageUtils.makeImagesOfCorners(self.BORDER))
self.currentColors = self.__calcColors()
def __save(self, im, name):
im.save(name, "PNG")
def __calcColors(self):
currentColors = collections.OrderedDict()
for i, chunk in enumerate(self.imageUtils.splitImageIntoChunks(self.current_image_all, self.NUMLED), 0):
currentColors[i] = ImageStat.Stat(chunk)._getmean()
return currentColors
示例8: remove_user
def remove_user(self, user):
userid = self.get_user_id(user)
user = self.select_one('username', 'users', where='id = ?', values=[userid])
self.delete('posts', 'userid = ?', [userid])
self.delete('comments', 'userid = ?', [userid])
self.delete('albums', 'userid = ?', [userid])
self.delete('users', 'username like ?', [user])
self.delete('newusers', 'username like ?', [user])
dirpath = path.join(ImageUtils.get_root(), 'content', user)
if path.exists(dirpath):
rmtree(dirpath)
示例9: process_url
def process_url(self, url, url_index, child):
self.debug('process_url: %s' % url)
userid = self.db.get_user_id(child.author)
if type(child) == Post:
base_fname = '%s-%d' % (child.id, url_index)
postid = child.id
commid = None
elif type(child) == Comment:
base_fname = '%s-%s-%d' % (child.post_id, child.id, url_index)
postid = child.post_id
commid = child.id
working_dir = path.join(ImageUtils.get_root(), 'content', child.author)
# A single URL can contain multiple medias (i.e. albums)
try:
(media_type, albumname, medias) = ImageUtils.get_urls(url)
except Exception, e:
self.debug('%s: process_url: unable to get URLs for %s: %s' % (child.author, url, str(e)))
return
示例10: start
def start(self):
print "MAIN: starting infinite loop..."
already_printed_sleep_msg = False
while True:
sleep(0.1)
if len(self.results) > 0:
# self.results is the list of downloaded medias to be added to the DB
result = self.results.pop()
self.handle_result(result)
try:
# Get next URL to retrieve
url = self.get_next_url()
except Exception, e:
if str(e) == "no URLs found":
if not already_printed_sleep_msg:
already_printed_sleep_msg = True
print "MAIN: no urls to get, sleeping 500ms"
sleep(0.5)
else:
print "MAIN: get_next_url(): Exception: %s:\n%s" % (str(e), format_exc())
continue
# We have a URL to download & add to DB (url)
already_printed_sleep_msg = False
# Wait for thread count to drop
while len(self.current_threads) >= MAX_THREADS:
sleep(0.1)
self.current_threads.append(None)
# Create new thread to download the media, add to self.results
print "MAIN: %s #%d: launching handler for: %s" % (url["path"], url["i_index"], url["url"])
# Create subdirs from main thread to avoid race condition
dirname = path.join(ImageUtils.get_root(), "rips", url["path"], "thumbs")
ImageUtils.create_subdirectories(dirname)
args = (url,)
t = Thread(target=self.retrieve_result_from_url, args=args)
t.start()
示例11: add_existing_album
def add_existing_album(self, user, oldalbum, oldpath):
newalbum = path.join(ImageUtils.get_root(), 'content', user, oldalbum)
if path.exists(newalbum):
self.debug('album already exists: %s' % newalbum)
return
(post, comment, imgid) = self.get_post_comment_id(oldalbum)
url = 'http://imgur.com/a/%s' % imgid
try:
album_id = self.add_album(newalbum, user, url, post, comment)
except Exception, e:
self.debug('add_existing_album: failed: %s' % str(e))
return
示例12: poll_user
def poll_user(self, user):
# Create directories if needed
user_dir = path.join(ImageUtils.get_root(), 'content', user)
ImageUtils.create_subdirectories(user_dir)
# Setup logger
self.logger = open(path.join(user_dir, 'history.log'), 'a')
self.db.logger = self.logger
ImageUtils.logger = self.logger
self.reddit.logger = self.logger
since_id = self.db.get_last_since_id(user)
# Get posts/comments for user
self.debug('%s: poll_user: since "%s"' % (user, since_id))
try:
children = self.reddit.get_user(user, since=since_id)
except Exception, e:
if '404: Not Found' in str(e):
# User is deleted, mark it as such
self.debug('%s: poll_user: user is 404, marking as deleted' % user)
self.db.mark_as_deleted(user)
return
self.debug('%s: poll_user: error %s' % (user, str(e)))
return
示例13: __init__
def __init__(self):
# Single file that all output is written to, to track usage
self.exit_if_already_started()
self.db = DB() # Database instance
log_level = self.db.get_config('log_level', default='user')
if log_level == 'none':
self.root_log = open(devnull, 'w')
else:
self.root_log = open(path.join(ImageUtils.get_root(), 'history.log'), 'a')
self.logger = self.root_log # Logger used by helper classes
self.reddit = Reddit()
self.excluded_subs = self.db.get_excluded_subreddits()
示例14: add_existing_album
def add_existing_album(self, user, oldalbum, oldpath):
newalbum = path.join(ImageUtils.get_root(), 'content', user, oldalbum)
if path.exists(newalbum):
self.debug('album already exists: %s' % newalbum)
return
(post, comment, imgid) = self.get_post_comment_id(oldalbum)
url = 'http://imgur.com/a/%s' % imgid
try:
album_id = self.add_album(newalbum, user, url, post, comment)
except Exception as e:
self.debug('add_existing_album: failed: %s' % str(e))
return
for image in listdir(oldpath):
self.debug('add_existing_album: image=%s' % path.join(oldpath, image))
fakeimage = post
if comment != None:
fakeimage = '%s-%s' % (fakeimage, comment)
fakeimage = '%s_%s' % (fakeimage, image.split('_')[-1])
self.add_existing_image(user, fakeimage, path.join(oldpath, image), subdir=oldalbum, album_id=album_id)
# Add post
p = Post()
p.id = post
p.author = user
if comment == None: p.url = url
p.created = path.getctime(oldpath)
p.subreddit = ''
p.title = ''
try:
self.add_post(p, legacy=1)
except Exception as e:
#self.debug('add_existing_image: %s' % str(e))
pass
# Add comment
if comment != None:
c = Comment()
c.id = comment
c.post_id = post
c.author = user
if comment != None: c.body = url
p.created = path.getctime(oldpath)
try:
self.add_comment(c, legacy=1)
except Exception as e:
#self.debug('add_existing_image: %s' % str(e))
pass
示例15: __init__
def __init__(self):
# Single file that all output is written to, to track usage
self.exit_if_already_started()
self.root_log = open(path.join(ImageUtils.get_root(), 'history.log'), 'a')
self.logger = self.root_log # Logger used by helper classes
self.db = DB() # Database instance
self.reddit = Reddit()
try:
(username, password) = self.db.get_credentials('reddit')
try:
self.reddit.login(username, password)
except Exception, e:
self.debug('__init__: failed to login to reddit: %s' % str(e))
except Exception, e:
self.debug('__init__: failed to get reddit credentials: %s' % str(e))