def _create_char(userid, x1, y1, x2, y2, charid, config=None, remove=True): x1, y1, x2, y2 = d.get_int(x1), d.get_int(y1), d.get_int(x2), d.get_int(y2) filename = d.url_make(charid, "char/.thumb", root=True) if not m.os.path.exists(filename): filename = d.url_make(charid, "char/cover", root=True) if not filename: return remove = False im = image.read(filename) size = im.size.width, im.size.height d.execute( """ UPDATE character SET settings = REGEXP_REPLACE(settings, '-.', '') || '-%s' WHERE charid = %i """, [image.image_setting(im), charid]) dest = '%s%i.thumb%s' % (d.get_hash_path( charid, "char"), charid, images.image_extension(im)) bounds = None if image.check_crop(size, x1, y1, x2, y2): bounds = geometry.Rectangle(x1, y1, x2, y2) thumb = images.make_thumbnail(im, bounds) thumb.write(dest, format=images.image_file_type(thumb)) if remove: m.os.remove(filename)
def create(userid, x1, y1, x2, y2, submitid=None, charid=None, config=None, remove=True): if charid: return _create_char(userid, x1, y1, x2, y2, charid, config, remove) db = d.connect() x1, y1, x2, y2 = d.get_int(x1), d.get_int(y1), d.get_int(x2), d.get_int(y2) source = thumbnail_source(submitid) im = db.query(orm.MediaItem).get(source['mediaid']).as_image() size = im.size.width, im.size.height bounds = None if image.check_crop(size, x1, y1, x2, y2): bounds = geometry.Rectangle(x1, y1, x2, y2) thumb = images.make_thumbnail(im, bounds) file_type = images.image_file_type(im) media_item = orm.fetch_or_create_media_item( thumb.to_buffer(format=file_type), file_type=file_type, im=thumb) orm.SubmissionMediaLink.make_or_replace_link(submitid, 'thumbnail-custom', media_item)
def _create_char(x1, y1, x2, y2, charid, remove=True): x1, y1, x2, y2 = d.get_int(x1), d.get_int(y1), d.get_int(x2), d.get_int(y2) filename = d.url_make(charid, "char/.thumb", root=True) if not m.os.path.exists(filename): filename = d.url_make(charid, "char/cover", root=True) if not filename: return remove = False im = image.read(filename) size = im.size.width, im.size.height d.engine.execute(""" UPDATE character SET settings = REGEXP_REPLACE(settings, '-.', '') || '-' || %(image_setting)s WHERE charid = %(char)s """, image_setting=image.image_setting(im), char=charid) dest = os.path.join(d.get_character_directory(charid), '%i.thumb%s' % (charid, images.image_extension(im))) bounds = None if image.check_crop(size, x1, y1, x2, y2): bounds = geometry.Rectangle(x1, y1, x2, y2) thumb = images.make_thumbnail(im, bounds) thumb.write(dest, format=images.image_file_type(thumb)) if remove: os.remove(filename)
def create(userid, x1, y1, x2, y2): x1, y1, x2, y2 = d.get_int(x1), d.get_int(y1), d.get_int(x2), d.get_int(y2) db = d.connect() im = db.query(orm.MediaItem).get( avatar_source(userid)['mediaid']).as_image() file_type = images.image_file_type(im) bounds = None size = im.size.width, im.size.height if image.check_crop(size, x1, y1, x2, y2): bounds = geometry.Rectangle(x1, y1, x2, y2) thumb = image.shrinkcrop(im, geometry.Size(100, 100), bounds) media_item = orm.MediaItem.fetch_or_create( thumb.to_buffer(format=file_type), file_type=file_type, im=thumb) orm.UserMediaLink.make_or_replace_link(userid, 'avatar', media_item) orm.UserMediaLink.clear_link(userid, 'avatar-source')
def _shrinkcrop(im, size, bounds=None): if bounds is not None: ret = im if bounds.position != geometry.origin or bounds.size != ret.size: ret = ret.cropped(bounds) if ret.size != size: ret = ret.resized(size) return ret elif im.size == size: return im shrunk_size = im.size.fit_around(size) shrunk = im if shrunk.size != shrunk_size: shrunk = shrunk.resized(shrunk_size) x1 = (shrunk.size.width - size.width) // 2 y1 = (shrunk.size.height - size.height) // 2 bounds = geometry.Rectangle(x1, y1, x1 + size.width, y1 + size.height) return shrunk.cropped(bounds)
def crop_image_to_height(image, height): # Crops from the bottom. crop_rect = geometry.Rectangle(0, 0, image.size.width, height) return image.cropped(crop_rect)
def crop_image_to_width(image, width): # Crops from both sides equally. overflow = image.size.width - width border = overflow / 2 crop_rect = geometry.Rectangle(border, 0, border + width, image.size.height) return image.cropped(crop_rect)
def test_rect_simple(): rect = geom.Rectangle(0, 0, 3, 4) assert rect.size == geom.Size(3, 4)
def test_rect_zero(): assert geom.Rectangle(0, 0, 10, 10) assert not geom.Rectangle(5, 5, 5, 10) assert not geom.Rectangle(5, 10, 5, 5) assert geom.Rectangle(5, 5, 10, 10)