Exemplo n.º 1
0
def wiper(pth, bound, original_size, wipev, wipeh):
    useful.write_message('wiper', pth, bound, original_size, wipev, wipeh)
    img = Image.open(pth)
    img = img.convert('RGB')
    xos, yos = original_size
    xl, yt, xr, yb = bound
    if xl > xr:
        x = xl
        xl = xr
        xr = x
    if yt > yb:
        y = yt
        yt = yb
        yb = y
    xr -= 1
    yb -= 1
    x1, y1, x2, y2 = xl, yt, xr, yb

    def wiper_copy(img, xf1, yf1, xf2, yf2, xt1, yt1, xt2, yt2):
	#useful.write_message(('%3d ' * 8) % (xf1, yf1, xf2 + 1, yf2 + 1, xt1, yt1, xt2 + 1, yt2 + 1), nonl=True)
	cp = img.crop((xf1, yf1, xf2 + 1, yf2 + 1))
	img.paste(cp, (xt1, yt1, xt2 + 1, yt2 + 1))

    while 1:
        if x1 > x2 or y1 > y2:
            break
        if wipev:
            if y1 != 0:
                wiper_copy(img, x1, yt, x2, yt, x1, y1, x2, y1)
                y1 += 1
            if x1 > x2 or y1 > y2:
                break
            if y2 != yos - 1:
                wiper_copy(img, x1, yb, x2, yb, x1, y2, x2, y2)
                y2 -= 1
        elif wipeh:
            if x1 != 0:
                wiper_copy(img, xl, y1, xl, y2, x1, y1, x1, y2)
                x1 += 1
            if x1 > x2 or y1 > y2:
                break
            if x2 != xos - 1:
                wiper_copy(img, xr, y1, xr, y2, x2, y1, x2, y2)
                x2 -= 1
        else:
            if y1 != 0:
                wiper_copy(img, x1, yt, x2, yt, x1, y1, x2, y1)
                y1 += 1
            if x1 != 0:
                wiper_copy(img, xl, y1, xl, y2, x1, y1, x1, y2)
                x1 += 1
            if x1 > x2 or y1 > y2:
                break
            if y2 != yos - 1:
                wiper_copy(img, x1, yb, x2, yb, x1, y2, x2, y2)
                y2 -= 1
            if x2 != xos - 1:
                wiper_copy(img, xr, y1, xr, y2, x2, y1, x2, y2)
                x2 -= 1
    return img
Exemplo n.º 2
0
def wiper(pth, bound, original_size, wipev, wipeh):
    useful.write_message('wiper', pth, bound, original_size, wipev, wipeh)
    img = Image.open(pth)
    img = img.convert('RGB')
    xos, yos = original_size
    xl, yt, xr, yb = bound
    if xl > xr:
        x = xl
        xl = xr
        xr = x
    if yt > yb:
        y = yt
        yt = yb
        yb = y
    xr -= 1
    yb -= 1
    x1, y1, x2, y2 = xl, yt, xr, yb

    def wiper_copy(img, xf1, yf1, xf2, yf2, xt1, yt1, xt2, yt2):
        #useful.write_message(('%3d ' * 8) % (xf1, yf1, xf2 + 1, yf2 + 1, xt1, yt1, xt2 + 1, yt2 + 1), nonl=True)
        cp = img.crop((xf1, yf1, xf2 + 1, yf2 + 1))
        img.paste(cp, (xt1, yt1, xt2 + 1, yt2 + 1))

    while 1:
        if x1 > x2 or y1 > y2:
            break
        if wipev:
            if y1 != 0:
                wiper_copy(img, x1, yt, x2, yt, x1, y1, x2, y1)
                y1 += 1
            if x1 > x2 or y1 > y2:
                break
            if y2 != yos - 1:
                wiper_copy(img, x1, yb, x2, yb, x1, y2, x2, y2)
                y2 -= 1
        elif wipeh:
            if x1 != 0:
                wiper_copy(img, xl, y1, xl, y2, x1, y1, x1, y2)
                x1 += 1
            if x1 > x2 or y1 > y2:
                break
            if x2 != xos - 1:
                wiper_copy(img, xr, y1, xr, y2, x2, y1, x2, y2)
                x2 -= 1
        else:
            if y1 != 0:
                wiper_copy(img, x1, yt, x2, yt, x1, y1, x2, y1)
                y1 += 1
            if x1 != 0:
                wiper_copy(img, xl, y1, xl, y2, x1, y1, x1, y2)
                x1 += 1
            if x1 > x2 or y1 > y2:
                break
            if y2 != yos - 1:
                wiper_copy(img, x1, yb, x2, yb, x1, y2, x2, y2)
                y2 -= 1
            if x2 != xos - 1:
                wiper_copy(img, xr, y1, xr, y2, x2, y1, x2, y2)
                x2 -= 1
    return img
Exemplo n.º 3
0
def submit_comment(pif):
    pif.render.print_html()
    print pif.render.format_head()
    useful.write_message(pif.form)
    ostr = "I am sending this comment for you. "

    fname = pif.form.get_str('pic.name')
    fimage = pif.form.get_str('pic')
    pif.form.delete('pic')
    pif.form.change_key('page', 'page_id')
    fn = "../../comments/comment." + datetime.datetime.now().strftime(
        '%Y%m%d.%H%M%S')

    mysubject = pif.form.get_str('mysubject')
    mycomment = pif.form.get_str('mycomment')
    if ('http://' in mysubject or 'http://' in mycomment
            or 'https://' in mysubject or 'https://' in mycomment):
        ostr += "<dl><dt>ERROR</dt><dd>Whoa there.  This isn't for submitting links.  Please use the SUGGEST A LINK feature from the link list.</dd></dl>"
        return ostr

    ostr += "<dl><dt>My Subject</dt><dd>" + mysubject + "</dd>\n"
    ostr += "<dl><dt>My Comment</dt><dd>" + mycomment + "</dd>\n"
    ostr += "<dt>My Name</dt><dd>" + pif.form.get_str('myname') + "</dd>\n"
    ostr += "<dt>My Email</dt><dd>" + pif.form.get_str(
        'myemail') + "</dd></dl>\n"

    if fimage:
        ostr += "<dt>Relevant File</dt><dd>" + fname + "<br>\n"
        direc = config.INC_DIR
        descriptions_file = config.LOG_ROOT + '/descr.log'
        dest_filename = images.get_next_upload_filename()
        dest_filename = useful.file_save(direc, dest_filename, fimage)
        images.file_log(direc + '/' + dest_filename, direc)

        cred = who = comment = '-'
        if pif.form.get_str('mycomment'):
            comment = squish_re.sub(' ', pif.form.get_str('mycomment'))
        if pif.form.get_str('credit'):
            cred = squish_re.sub(' ', pif.form.get_str('credit'))
        if pif.form.get_str('myname'):
            who = squish_re.sub(' ', pif.form.get_str('myname'))
        open(descriptions_file, 'a+').write(
            '\t'.join([dest_filename, '-', '-', '-', comment, cred, who]) +
            '\n')
        ostr = '<div class="warning">Thank you for submitting that file.</div><br>\n'

        ostr += "</dd></dl>\n"

    fh = open(fn, "wt")
    fh.write("_POST\n\n" + pprint.pformat(pif.form, indent=2, width=132) +
             "\n\n")
    fh.write("REMOTE_ADDR=" + os.getenv('REMOTE_ADDR') + "\n")
    ostr += "Thanks for sending that.  Now please use the BACK button on your browser to return to where you were."
    return ostr
Exemplo n.º 4
0
    def execute(self, query, args=None, verbose=None, tag=''):
        if verbose is None:
            verbose = self.verbose
        if tag:
            query = query.split(None, 1) + ['']
            query = query[0] + ' /* ' + tag + ' */ ' + query[1]
        if verbose:
            useful.write_comment('DB.execute q : "%s"' % query)
            if args:
                useful.write_comment('     args :', args)
            sys.stdout.flush()
        if self.logger:
#            log_name = os.path.join(config.LOG_ROOT, config.ENV + datetime.datetime.now().strftime('.dbq%Y%m.log'))
#            try:
#                open(log_name, 'a').write('%s %s%s %s %s\n' %
#                                          (datetime.datetime.now().strftime('%Y%m%d.%H%M%S'), '/*mock*/' if self.nowrites else '',
#                                           self.user_id, os.environ.get('REMOTE_ADDR', ''), query))
#            except:
#                pass
	    self.logger.info('q %s%s %s' %
			      ('/*mock*/ ' if self.nowrites else '',
			       os.environ.get('REMOTE_ADDR', ''), query))
        cu = self.db.cursor()
	if tag == 'UpdateLineupModel':
	    useful.write_message(query, '<br>')
        try:
            if args:
                nrows = cu.execute(query, args)
            else:
                nrows = cu.execute(query)
        except:
	    self.logger.info('x %s%s %s' %
			      ('/*mock*/ ' if self.nowrites else '',
			       os.environ.get('REMOTE_ADDR', ''), traceback.format_exc(0)))
            if verbose:
                traceback.print_exc()
            return ([], cu.description, -1)
        resp = cu.fetchall()
        if verbose:
            useful.write_comment("DB.execute a :", nrows, cu.lastrowid, resp)
            sys.stdout.flush()
        self.lastrowid = cu.lastrowid
        self.lastdescription = cu.description
        self.lastresp = resp
        cu.close()
	self.logger.info('a %s%s %s' %
			  ('/*mock*/ ' if self.nowrites else '',
			   os.environ.get('REMOTE_ADDR', ''), "%s rows %s id" % (len(resp), self.lastrowid)))
        return (resp, self.lastdescription, self.lastrowid)
Exemplo n.º 5
0
def pipe_chain(inp, pipes, stderr=None, verbose=True):
    ch = '%'
    for pipe in pipes:
        if verbose:
            useful.write_message(ch, ' '.join(pipe), nonl=True)
        ch = '|'
        proc = subprocess.Popen(pipe,
                                stdin=inp,
                                stdout=subprocess.PIPE,
                                stderr=stderr,
                                close_fds=True)
        inp = proc.stdout
    if verbose:
        useful.write_message()
    return proc.communicate()[0]
Exemplo n.º 6
0
def padder(pth, target_size):
    useful.write_message('padder', pth, target_size)
    img = Image.open(pth)
    img = img.convert('RGB')

    xos, yos = img.size
    xts = target_size[0] if target_size[0] else xos
    yts = target_size[1] if target_size[1] else yos
    if img.size == (xts, yts):
        return img
    if xos > xts or yos > yts:
        useful.write_message("original larger than new", nonl=True)
        return img
    img = img.convert('RGB')

    nc = (0, 0, 0)
    bc = None

    sx = (xts - xos) / 2
    sy = (yts - yos) / 2
    ex = sx + xos - 1
    ey = sy + yos - 1

    # |0   |s       e|    |n
    # |    |<-- o -->|    |
    # |<------- n ------->|

    nimg = Image.new(img.mode, (xts, yts), nc)
    for x in range(0, xos):
        for y in range(0, yos):
            nimg.putpixel((sx + x, sy + y), img.getpixel((x, y)))
    for x in range(0, sx):
        for y in range(0, yts):
            nimg.putpixel((x, y), nimg.getpixel((sx, y)))
    for x in range(ex + 1, xts):
        for y in range(0, yts):
            nimg.putpixel((x, y), nimg.getpixel((ex, y)))
    for y in range(0, sy):
        for x in range(0, xts):
            nimg.putpixel((x, y), nimg.getpixel((x, sy)))
    for y in range(ey + 1, yts):
        for x in range(0, xts):
            nimg.putpixel((x, y), nimg.getpixel((x, ey)))

    return nimg
Exemplo n.º 7
0
def padder(pth, target_size):
    useful.write_message('padder', pth, target_size)
    img = Image.open(pth)
    img = img.convert('RGB')

    xos, yos = img.size
    xts = target_size[0] if target_size[0] else xos
    yts = target_size[1] if target_size[1] else yos
    if img.size == (xts, yts):
	return img
    if xos > xts or yos > yts:
	useful.write_message("original larger than new", nonl=True)
	return img
    img = img.convert('RGB')

    nc = (0,0,0)
    bc = None

    sx = (xts - xos) /2
    sy = (yts - yos) /2
    ex = sx + xos - 1
    ey = sy + yos - 1

    # |0   |s       e|    |n
    # |    |<-- o -->|    |
    # |<------- n ------->|

    nimg = Image.new(img.mode, (xts, yts), nc)
    for x in range(0, xos):
	for y in range(0, yos):
	    nimg.putpixel((sx + x, sy + y), img.getpixel((x, y)))
    for x in range(0, sx):
	for y in range(0, yts):
	    nimg.putpixel((x, y), nimg.getpixel((sx, y)))
    for x in range(ex + 1, xts):
	for y in range(0, yts):
	    nimg.putpixel((x, y), nimg.getpixel((ex, y)))
    for y in range(0, sy):
	for x in range(0, xts):
	    nimg.putpixel((x, y), nimg.getpixel((x, sy)))
    for y in range(ey + 1, yts):
	for x in range(0, xts):
	    nimg.putpixel((x, y), nimg.getpixel((x, ey)))

    return nimg
Exemplo n.º 8
0
def do_prod_masses(pif, tform):
    ddir = tform.tdir.replace('lib', 'pic')
    print pif.form.get_str('credit'), ddir, '<br>'
    siz = pif.form.get('tysz')
    for fn, nam in pif.form.get_list(start='nam.'):
	print '<hr>'
	print fn, ddir, siz, nam, '<br>'

	rf = [False, False, False, False, False]
	pth = tform.tdir + '/' + fn
	q = (0, 0,) + imglib.get_size(pth)
	nname = ddir + '/' + siz + '_' + nam + '.jpg'
	ts = (400, 0)
	ofi = imglib.shrinker(pth, nname, q, ts, rf)
	imglib.simple_save(ofi, nname)
	images.file_log(nname, tform.tdir)
	url = 'http://www.bamca.org/' + nname
	link = 'http://www.bamca.org/'
	useful.write_message('Post to Tumblr: ', tumblr.tumblr(pif).create_photo(caption=nam, source=url, link=link))
	pif.dbh.write_photo_credit(pif.form.get_str('credit'), ddir, nam)
Exemplo n.º 9
0
    def redo(self, post):
	useful.write_message('removing from spool')
	self.pif.dbh.delete_tumblr(post.id)
	ty_post = post.post_type
	kwargs = eval(post.payload)
	print kwargs
	if ty_post == TUMBLR_PHOTO:
	    response = self.create_photo(**kwargs)
	elif ty_post == TUMBLR_TEXT:
	    response = self.create_text(**kwargs)
	elif ty_post == TUMBLR_QUOTE:
	    response = self.create_quote(**kwargs)
	elif ty_post == TUMBLR_LINK:
	    response = self.create_link(**kwargs)
	elif ty_post == TUMBLR_CHAT:
	    response = self.create_chat(**kwargs)
	elif ty_post == TUMBLR_AUDIO:
	    response = self.create_audio(**kwargs)
	elif ty_post == TUMBLR_VIDEO:
	    response = self.create_video(**kwargs)
	else:
	    response = {'state': 'published'}
	return response
Exemplo n.º 10
0
def iconner(in_path, name, logo=None, isizex=100, isizey=100):
    if not os.path.exists(in_path):
        useful.write_message('no original file', in_path, nonl=True)
        return

    thumb = Image.open(in_path)
    if thumb.size[1] != 120:
        useful.write_message('bad original size', thumb.size, nonl=True)
        return
    thumb = thumb.resize((isizex, isizex * thumb.size[1] / thumb.size[0]),
                         Image.NEAREST)
    banner = Image.open(logo)

    text = imicon.Icon(isizex, isizey)
    top = banner.size[1] + thumb.size[1]
    texttop = isizey - 6 - (6 * len(name)) / 2
    for n in name:
        if len(n) > isizex / 6:
            text.charset("3x5.font")
            left = 50 - len(n) * 2
        else:
            text.charset("5x5.font")
            left = 50 - len(n) * 3
        text.string(left, texttop, n.upper())
        texttop = texttop + 6
    iconimage = text.getimage()

    iconimage.paste(banner, ((isizex - banner.size[0]) / 2, 0))
    iconimage.paste(thumb, ((isizex - thumb.size[0]) / 2, banner.size[1]))

    # write out as png and job off the final conversion to netpbm.
    # doing this because I don't like how PIL disses GIFs.
    tmp_pth = '/tmp/iconner.png'
    iconimage.save(tmp_pth)
    ofi = pipe_convert(tmp_pth, '.gif', verbose=True)
    os.unlink(tmp_pth)
    return ofi
Exemplo n.º 11
0
def iconner(in_path, name, logo=None, isizex=100, isizey=100):
    if not os.path.exists(in_path):
        useful.write_message('no original file', in_path, nonl=True)
        return

    thumb = Image.open(in_path)
    if thumb.size[1] != 120:
        useful.write_message('bad original size', thumb.size, nonl=True)
        return
    thumb = thumb.resize((isizex, isizex * thumb.size[1] / thumb.size[0]), Image.NEAREST)
    banner = Image.open(logo)

    text = imicon.Icon(isizex, isizey)
    top = banner.size[1] + thumb.size[1]
    texttop = isizey - 6 - (6 * len(name)) / 2
    for n in name:
        if len(n) > isizex / 6:
            text.charset("3x5.font")
            left = 50 - len(n) * 2
        else:
            text.charset("5x5.font")
            left = 50 - len(n) * 3
        text.string(left, texttop, n.upper())
        texttop = texttop + 6
    iconimage = text.getimage()

    iconimage.paste(banner, ((isizex - banner.size[0]) / 2, 0))
    iconimage.paste(thumb, ((isizex - thumb.size[0]) / 2, banner.size[1]))

    # write out as png and job off the final conversion to netpbm.
    # doing this because I don't like how PIL disses GIFs.
    tmp_pth = '/tmp/iconner.png'
    iconimage.save(tmp_pth)
    ofi = pipe_convert(tmp_pth, '.gif', verbose=True)
    os.unlink(tmp_pth)
    return ofi
Exemplo n.º 12
0
 def redo(self, post):
     useful.write_message('removing from spool')
     self.pif.dbh.delete_tumblr(post.id)
     ty_post = post.post_type
     kwargs = eval(post.payload)
     print(kwargs)
     if ty_post == TUMBLR_PHOTO:
         response = self.create_photo(**kwargs)
     # elif ty_post == TUMBLR_TEXT:
     #     response = self.create_text(**kwargs)
     # elif ty_post == TUMBLR_QUOTE:
     #     response = self.create_quote(**kwargs)
     # elif ty_post == TUMBLR_LINK:
     #     response = self.create_link(**kwargs)
     # elif ty_post == TUMBLR_CHAT:
     #     response = self.create_chat(**kwargs)
     # elif ty_post == TUMBLR_AUDIO:
     #     response = self.create_audio(**kwargs)
     # elif ty_post == TUMBLR_VIDEO:
     #     response = self.create_video(**kwargs)
     else:
         response = {'state': 'published'}
     print(response)
     return response
Exemplo n.º 13
0
def cropper(pth, nname, bound, rf):
    x1, y1, x2, y2 = bound
    useful.write_message('crop', x1, y1, x2, y2, ':', x2 - x1, y2 - y1, ':',
                         rf)
    useful.write_message(pth)
    useful.write_message("cutting")
    ofi = pipe_chain(open(pth),
                     import_file(pth) + cut(x1, y1, x2, y2) + rot_flip(rf) +
                     export_file(nname, pth),
                     stderr=open('/dev/null', 'w'))
    return ofi
Exemplo n.º 14
0
def cropper(pth, nname, bound, rf):
    x1, y1, x2, y2 = bound
    useful.write_message('crop', x1, y1, x2, y2, ':', x2 - x1, y2 - y1, ':',
                         rf)
    useful.write_message(pth)
    useful.write_message("cutting")
    ofi = useful.pipe_chain(open_input_file(pth),
                            import_file(pth) + cut(x1, y1, x2, y2) +
                            rot_flip(rf) + export_file(nname, pth),
                            stderr=open_write_dev_null(),
                            verbose=True)
    return ofi
Exemplo n.º 15
0
def cropper(pth, nname, bound, rf):
    x1, y1, x2, y2 = bound
    useful.write_message('crop', x1, y1, x2, y2, ':', x2-x1, y2-y1, ':', rf)
    useful.write_message(pth)
    useful.write_message("cutting")
    ofi = useful.pipe_chain(open(pth),
            import_file(pth) +
            cut(x1, y1, x2, y2) +
            rot_flip(rf) +
            export_file(nname, pth),
		    stderr=open('/dev/null', 'w'))
    return ofi
Exemplo n.º 16
0
def stitcher(ofn, fa, is_horiz, minx, miny, limit_x, limit_y, verbose=False):
    if is_horiz:
        useful.write_message('horizontal', nonl=True)

        if limit_y:
            miny = min(miny, limit_y)
        resize_x = 0
        resize_y = miny
        cat = ['pnmcat', '-lr']

    else:
        useful.write_message('vertical', nonl=True)

        if limit_x:
            minx = min(minx, limit_x)
        resize_x = minx
        resize_y = 0
        cat = ['pnmcat', '-tb']

    for f in fa:
        pipes = import_file(f[0]) + \
         cut(f[3], f[4], f[5], f[6]) + \
         resize(x=resize_x, y=resize_y)
        outf = pipe_chain(open(f[0]),
                          pipes,
                          verbose=verbose,
                          stderr=open('/dev/null', 'w'))
        if verbose:
            useful.write_message('>', f[0] + '.pnm')
        open(f[0] + '.pnm', 'w').write(outf)
        cat.append(f[0] + '.pnm')
    outf = pipe_chain(open('/dev/null'), [cat] + export_file(ofn),
                      verbose=verbose,
                      stderr=open('/dev/null', 'w'))

    if verbose:
        useful.write_message('>', ofn)
    open(ofn, 'w').write(outf)

    if not verbose:
        for f in fa:
            os.unlink(f[0] + '.pnm')
Exemplo n.º 17
0
def stitcher(ofn, fa, is_horiz, minx, miny, limit_x, limit_y, verbose=False):
    if is_horiz:
        useful.write_message('horizontal', nonl=True)

	if limit_y:
	    miny = min(miny, limit_y)
	resize_x = 0
	resize_y = miny
	cat = ['pnmcat', '-lr']

    else:
        useful.write_message('vertical', nonl=True)

	if limit_x:
	    minx = min(minx, limit_x)
	resize_x = minx
	resize_y = 0
	cat = ['pnmcat', '-tb']

    for f in fa:
	pipes = import_file(f[0]) + \
		cut(f[3], f[4], f[5], f[6]) + \
		resize(x=resize_x, y=resize_y)
	outf = useful.pipe_chain(open(f[0]), pipes, verbose=verbose,
		    stderr=open('/dev/null', 'w'))
	if verbose:
	    useful.write_message('>', f[0] + '.pnm')
	open(f[0] + '.pnm', 'w').write(outf)
	cat.append(f[0] + '.pnm')
    outf = useful.pipe_chain(open('/dev/null'), [cat] + export_file(ofn), verbose=verbose,
		    stderr=open('/dev/null', 'w'))

    if verbose:
	useful.write_message('>', ofn)
    open(ofn, 'w').write(outf)

    if not verbose:
	for f in fa:
	    os.unlink(f[0] + '.pnm')
Exemplo n.º 18
0
    def post(self, ty_post, response, **kwargs):
	if response.get('state') != 'published':
	    useful.write_message('spooling for later')
	    self.pif.dbh.insert_tumblr(ty_post, str(response), str(kwargs))
	return response
Exemplo n.º 19
0
def shrinker(pth, nname, bound, maxsize, rf):
    useful.write_message('shrinker', pth, nname, bound, maxsize)
    x1, y1, x2, y2 = bound
    xcs = x2 - x1
    ycs = y2 - y1
    xts, yts = maxsize
    if not xts:
        xts = xcs
    if not yts:
        yts = ycs
    useful.write_message(x1, y1, x2, y2, ':', xcs, ycs, ':', xts, yts)
    useful.write_message(pth)
    if xcs == xts and ycs == yts:
        useful.write_message("cutting")
        xts, yts = fix_axes(rf, xts, yts)
        ofi = pipe_chain(open(pth),
                         import_file(pth) + cut(x1, y1, x1 + xcs, y1 + ycs) +
                         rot_flip(rf) + export_file(nname, pth),
                         stderr=open('/dev/null', 'w'))
    elif xts / xcs < yts / ycs:
        useful.write_message("shrinking x")
        xts, yts = fix_axes(rf, xts, yts)
        ofi = pipe_chain(open(pth),
                         import_file(pth) + cut(x1, y1, x1 + xcs, y1 + ycs) +
                         rot_flip(rf) + resize(x=xts) +
                         export_file(nname, pth),
                         stderr=open('/dev/null', 'w'))
    else:
        useful.write_message("shrinking y")
        xts, yts = fix_axes(rf, xts, yts)
        ofi = pipe_chain(open(pth),
                         import_file(pth) + cut(x1, y1, x1 + xcs, y1 + ycs) +
                         rot_flip(rf) + resize(y=yts) +
                         export_file(nname, pth),
                         stderr=open('/dev/null', 'w'))
    return ofi
Exemplo n.º 20
0
def shaper(pth, nname, bound, target_size, original_size, rf):
    xts, yts = target_size
    x1, y1, x2, y2 = bound
    xcs = x2 - x1
    ycs = y2 - y1
    xos, yos = original_size

    useful.write_message('Shape :', pth, ': bounds', x1, y1, x2, y2,
                         'bound size', xcs, ycs, 'target size', xts, yts)
    if xts and yts:
        x1, x2, y1, y2 = set_shape_sizes(x1, x2, y1, y2, xts, yts, xos, yos)
        xcs = x2 - x1
        ycs = y2 - y1

        if xcs > xts:
            useful.write_message("shrinking", nonl=True)
            xts, yts = fix_axes(rf, xts, yts)
            ofi = pipe_chain(open(pth),
                             import_file(pth) + cut(x1, y1, x2, y2) +
                             rot_flip(rf) + resize(x=xts) +
                             export_file(nname, pth),
                             stderr=open('/dev/null', 'w'))
        elif xcs < xts:
            dx = xts - xcs
            dy = yts - ycs
            x1, x2, y1, y2 = normalize(x1, x2, y1, y2, xts, yts)
            useful.write_message("expanding", x1, x2, y1, y2, nonl=True)
            xts, yts = fix_axes(rf, xts, yts)
            ofi = pipe_chain(open(pth),
                             import_file(pth) + cut(x1, y1, x2, y2) +
                             rot_flip(rf) + export_file(nname, pth),
                             stderr=open('/dev/null', 'w'))
        elif xos == xts and yos == yts and xos == xcs and yos == ycs:
            useful.write_message("copying", nonl=True)
            ofi = open(pth).read()
        else:
            useful.write_message("cutting", nonl=True)
            xts, yts = fix_axes(rf, xts, yts)
            ofi = pipe_chain(open(pth),
                             import_file(pth) + cut(x1, y1, x2, y2) +
                             rot_flip(rf) + export_file(nname, pth),
                             stderr=open('/dev/null', 'w'))

    else:

        if xts < x2 - x1:
            useful.write_message("trim shrinking x", nonl=True)
            xts, yts = fix_axes(rf, xts, yts)
            ofi = pipe_chain(open(pth),
                             import_file(pth) + cut(x1, y1, x2, y2) +
                             rot_flip(rf) + resize(x=xts) +
                             export_file(nname, pth),
                             stderr=open('/dev/null', 'w'))
        elif yts < y2 - y1:
            useful.write_message("trim shrinking y", nonl=True)
            xts, yts = fix_axes(rf, xts, yts)
            ofi = pipe_chain(open(pth),
                             import_file(pth) + cut(x1, y1, x2, y2) +
                             rot_flip(rf) + resize(y=yts) +
                             export_file(nname, pth),
                             stderr=open('/dev/null', 'w'))
        else:
            useful.write_message("trim cutting", nonl=True)
            xts, yts = fix_axes(rf, xts, yts)
            ofi = pipe_chain(open(pth),
                             import_file(pth) + cut(x1, y1, x2, y2) +
                             rot_flip(rf) + export_file(nname, pth),
                             stderr=open('/dev/null', 'w'))

    useful.write_message('')
    return ofi
Exemplo n.º 21
0
def normalize(x1, x2, y1, y2, xm, ym):
    useful.write_message('normalize',
                         x1,
                         y1,
                         "/",
                         x2,
                         y2,
                         "orig",
                         xm,
                         ym,
                         nonl=True)
    if x1 < 0:
        x2 = x2 - x1
        x1 = 0
        useful.write_message("x1", nonl=True)
    if y1 < 0:
        y2 = y2 - y1
        y1 = 0
        useful.write_message("y1", nonl=True)
    if x2 >= xm:
        x1 = x1 - (x2 - xm)
        x2 = xm
        useful.write_message("x2", nonl=True)
    if y2 >= ym:
        y1 = y1 - (y2 - ym)
        y2 = ym
        useful.write_message("y2", nonl=True)
    useful.write_message('returns', x1, y1, "/", x2, y2)
    return x1, x2, y1, y2
Exemplo n.º 22
0
def set_shape_sizes(x1, x2, y1, y2, xts, yts, xos, yos):
    #xos, yos = get_size(tdir + '/' + fil)
    xcs = x2 - x1
    ycs = y2 - y1
    ratio = float(xts) / float(yts)
    useful.write_message("set_shape_sizes", x1, y1, "/", x2, y2, ';', xts, yts,
                         ';', xcs, ycs, ';', xos, yos, ';', ratio)
    if xcs < xts:
        if xos < xts:  # this one doesn't work right
            useful.write_message("fix x to orig /", nonl=True)
            x1 = 0
            x2 = xos - 1
        else:
            useful.write_message("fix x to target /", nonl=True)
            x1 = max(0, x1 - (xts - xcs) / 2)
            x2 = x1 + xts
    if ycs < yts:
        if yos < yts:
            useful.write_message("fix y to orig /", nonl=True)
            y1 = 0
            y2 = yos - 1
        else:
            useful.write_message("fix y to target /", nonl=True)
            y1 = max(0, y1 - (yts - ycs) / 2)
            y2 = y1 + yts
    useful.write_message("(", x1, y1, "/", x2, y2, ")", nonl=True)
    xcs = x2 - x1
    ycs = y2 - y1
    # might revisit this, for images that are off center this might not be right
    if xts <= xos and yts <= yos and xcs <= xts and ycs <= yts:
        useful.write_message("shape expanding x and y")
        x1 = x1 - (xts - xcs) / 2
        x2 = x1 + xts
        y1 = y1 - (yts - ycs) / 2
        y2 = y1 + yts
        useful.write_message("(", x1, y1, ") (", x2, y2, ")", nonl=True)
        x1, x2, y1, y2 = normalize(x1, x2, y1, y2, xos, yos)
    elif xcs < int(float(ycs) * ratio):
        # too tall - expand x
        useful.write_message("shape expanding x", nonl=True)
        nxs = int(float(ycs) * ratio)
        nx1 = x1 - (nxs - xcs) / 2
        nx2 = nx1 + nxs
        if nxs > xos:
            useful.write_message("off both edges")
            nx1 = 0
            nx2 = xos
        elif nx1 < 0:
            useful.write_message("off the left")
            nx2 = nx2 - nx1
            nx1 = 0
        elif nx2 > xos:
            useful.write_message("off the right")
            nx1 = nx1 - (nx2 - xos)
            nx2 = xos
        else:
            pass
            useful.write_message("on")
        x1 = nx1
        x2 = nx2
    elif xcs > int(float(ycs) * ratio):
        # too wide - expand y
        useful.write_message("shape expanding y", nonl=True)
        nys = int(float(xcs) / ratio)
        ny1 = y1 - (nys - ycs) / 2
        ny2 = ny1 + nys
        if nys > yos:
            useful.write_message("off both edges")
            ny1 = 0
            ny2 = yos
        elif ny1 < 0:
            useful.write_message("off the top")
            ny2 = nys
            ny1 = 0
        elif ny2 > yos:
            useful.write_message("off the bottom")
            #ny1 = ny1 - (ny2 - yos)
            ny1 = yos - nys
            ny2 = yos
        else:
            pass
            useful.write_message("on")
        y1 = ny1
        y2 = ny2
    else:
        useful.write_message("shape as is")
        # hit the jackpot, Mel!
        pass
    useful.write_message('set_shape_sizes returned', x1, y1, '/', x2, y2, '->',
                         x2 - x1, y2 - y1)
    return x1, x2, y1, y2
Exemplo n.º 23
0
def normalize(x1, x2, y1, y2, xm, ym):
    useful.write_message('normalize', x1, y1, "/", x2, y2, "orig", xm, ym, nonl=True)
    if x1 < 0:
        x2 = x2 - x1
        x1 = 0
        useful.write_message("x1", nonl=True)
    if y1 < 0:
        y2 = y2 - y1
        y1 = 0
        useful.write_message("y1", nonl=True)
    if x2 >= xm:
        x1 = x1 - (x2 - xm)
        x2 = xm
        useful.write_message("x2", nonl=True)
    if y2 >= ym:
        y1 = y1 - (y2 - ym)
        y2 = ym
        useful.write_message("y2", nonl=True)
    useful.write_message('returns', x1, y1, "/", x2, y2)
    return x1, x2, y1, y2
Exemplo n.º 24
0
def shrinker(pth, nname, bound, maxsize, rf):
    useful.write_message('shrinker', pth, nname, bound, maxsize)
    x1, y1, x2, y2 = bound
    xcs = x2 - x1
    ycs = y2 - y1
    xts, yts = maxsize
    if not xts:
        xts = xcs
    if not yts:
        yts = ycs
    useful.write_message(x1, y1, x2, y2, ':', xcs, ycs, ':', xts, yts)
    useful.write_message(pth)
    if xcs == xts and ycs == yts:
        useful.write_message("cutting")
	xts, yts = fix_axes(rf, xts, yts)
        ofi = useful.pipe_chain(open(pth),
                import_file(pth) +
                cut(x1, y1, x1 + xcs, y1 + ycs) +
                rot_flip(rf) +
                export_file(nname, pth),
		    stderr=open('/dev/null', 'w'))
    elif xts/xcs < yts/ycs:
        useful.write_message("shrinking x")
	xts, yts = fix_axes(rf, xts, yts)
        ofi = useful.pipe_chain(open(pth),
                import_file(pth) +
                cut(x1, y1, x1 + xcs, y1 + ycs) +
                rot_flip(rf) +
                resize(x=xts) +
                export_file(nname, pth),
		    stderr=open('/dev/null', 'w'))
    else:
        useful.write_message("shrinking y")
	xts, yts = fix_axes(rf, xts, yts)
        ofi = useful.pipe_chain(open(pth),
                import_file(pth) +
                cut(x1, y1, x1 + xcs, y1 + ycs) +
                rot_flip(rf) +
                resize(y=yts) +
                export_file(nname, pth),
		    stderr=open('/dev/null', 'w'))
    return ofi
Exemplo n.º 25
0
def shaper(pth, nname, bound, target_size, original_size, rf):
    xts, yts = target_size
    x1, y1, x2, y2 = bound
    xcs = x2 - x1
    ycs = y2 - y1
    xos, yos = original_size

    useful.write_message('Shape :', pth, ': bounds', x1, y1, x2, y2, 'bound size', xcs, ycs, 'target size', xts, yts)
    if xts and yts:
        x1, x2, y1, y2 = set_shape_sizes(x1, x2, y1, y2, xts, yts, xos, yos)
        xcs = x2 - x1
        ycs = y2 - y1

        if xcs > xts:
            useful.write_message("shrinking", nonl=True)
	    xts, yts = fix_axes(rf, xts, yts)
            ofi = useful.pipe_chain(open(pth),
                    import_file(pth) +
                    cut(x1, y1, x2, y2) +
                    rot_flip(rf) +
                    resize(x=xts) +
                    export_file(nname, pth),
		    stderr=open('/dev/null', 'w'))
        elif xcs < xts:
            dx = xts - xcs
            dy = yts - ycs
            x1, x2, y1, y2 = normalize(x1, x2, y1, y2, xts, yts)
            useful.write_message("expanding", x1, x2, y1, y2, nonl=True)
	    xts, yts = fix_axes(rf, xts, yts)
            ofi = useful.pipe_chain(open(pth),
                    import_file(pth) +
                    cut(x1, y1, x2, y2) +
                    rot_flip(rf) +
                    export_file(nname, pth),
		    stderr=open('/dev/null', 'w'))
        elif xos == xts and yos == yts and xos == xcs and yos == ycs:
            useful.write_message("copying", nonl=True)
            ofi = open(pth).read()
        else:
            useful.write_message("cutting", nonl=True)
	    xts, yts = fix_axes(rf, xts, yts)
            ofi = useful.pipe_chain(open(pth),
                    import_file(pth) +
                    cut(x1, y1, x2, y2) +
                    rot_flip(rf) +
                    export_file(nname, pth),
		    stderr=open('/dev/null', 'w'))

    else:

        if xts < x2 - x1:
            useful.write_message("trim shrinking x", nonl=True)
	    xts, yts = fix_axes(rf, xts, yts)
            ofi = useful.pipe_chain(open(pth),
                    import_file(pth) +
                    cut(x1, y1, x2, y2) +
                    rot_flip(rf) +
                    resize(x=xts) +
                    export_file(nname, pth),
		    stderr=open('/dev/null', 'w'))
        elif yts < y2 - y1:
            useful.write_message("trim shrinking y", nonl=True)
	    xts, yts = fix_axes(rf, xts, yts)
            ofi = useful.pipe_chain(open(pth),
                    import_file(pth) +
                    cut(x1, y1, x2, y2) +
                    rot_flip(rf) +
                    resize(y=yts) +
                    export_file(nname, pth),
		    stderr=open('/dev/null', 'w'))
        else:
            useful.write_message("trim cutting", nonl=True)
	    xts, yts = fix_axes(rf, xts, yts)
            ofi = useful.pipe_chain(open(pth),
                    import_file(pth) +
                    cut(x1, y1, x2, y2) +
                    rot_flip(rf) +
                    export_file(nname, pth),
		    stderr=open('/dev/null', 'w'))

    useful.write_message('')
    return ofi
Exemplo n.º 26
0
    def message(self, *args):
        if self.dump_file:  # pragma: no cover
            self.dump_file.write(' '.join([str(x) for x in args]) + '\n')
	useful.write_message(*args)
Exemplo n.º 27
0
 def post(self, ty_post, response, **kwargs):
     if response.get('state') != 'published':
         useful.write_message('spooling for later')
         self.pif.dbh.insert_tumblr(ty_post, str(response), str(kwargs))
     return response
Exemplo n.º 28
0
def set_shape_sizes(x1, x2, y1, y2, xts, yts, xos, yos):
    #xos, yos = get_size(tdir + '/' + fil)
    xcs = x2 - x1
    ycs = y2 - y1
    ratio = float(xts) / float(yts)
    useful.write_message("set_shape_sizes", x1, y1, "/", x2, y2, ';', xts, yts, ';', xcs, ycs, ';', xos, yos, ';', ratio)
    if xcs < xts:
        if xos < xts: # this one doesn't work right
            useful.write_message("fix x to orig /", nonl=True)
            x1 = 0
            x2 = xos - 1
        else:
            useful.write_message("fix x to target /", nonl=True)
            x1 = max(0, x1 - (xts - xcs) / 2)
            x2 = x1 + xts
    if ycs < yts:
        if yos < yts:
            useful.write_message("fix y to orig /", nonl=True)
            y1 = 0
            y2 = yos - 1
        else:
            useful.write_message("fix y to target /", nonl=True)
            y1 = max(0, y1 - (yts - ycs) / 2)
            y2 = y1 + yts
    useful.write_message("(", x1, y1, "/", x2, y2, ")", nonl=True)
    xcs = x2 - x1
    ycs = y2 - y1
    # might revisit this, for images that are off center this might not be right
    if xts <= xos and yts <= yos and xcs <= xts and ycs <= yts:
        useful.write_message("shape expanding x and y")
        x1 = x1 - (xts - xcs) / 2
        x2 = x1 + xts
        y1 = y1 - (yts - ycs) / 2
        y2 = y1 + yts
        useful.write_message("(", x1, y1, ") (", x2, y2, ")", nonl=True)
        x1, x2, y1, y2 = normalize(x1, x2, y1, y2, xos, yos)
    elif xcs < int(float(ycs) * ratio):
        # too tall - expand x
        useful.write_message("shape expanding x", nonl=True)
        nxs = int(float(ycs) * ratio)
        nx1 = x1 - (nxs - xcs) / 2
        nx2 = nx1 + nxs
        if nxs > xos:
            useful.write_message("off both edges")
            nx1 = 0
            nx2 = xos
        elif nx1 < 0:
            useful.write_message("off the left")
            nx2 = nx2 - nx1
            nx1 = 0
        elif nx2 > xos:
            useful.write_message("off the right")
            nx1 = nx1 - (nx2 - xos)
            nx2 = xos
        else:
            pass
            useful.write_message("on")
        x1 = nx1
        x2 = nx2
    elif xcs > int(float(ycs) * ratio):
        # too wide - expand y
        useful.write_message("shape expanding y", nonl=True)
        nys = int(float(xcs) / ratio)
        ny1 = y1 - (nys - ycs) / 2
        ny2 = ny1 + nys
        if nys > yos:
            useful.write_message("off both edges")
            ny1 = 0
            ny2 = yos
        elif ny1 < 0:
            useful.write_message("off the top")
            ny2 = nys
            ny1 = 0
        elif ny2 > yos:
            useful.write_message("off the bottom")
            #ny1 = ny1 - (ny2 - yos)
            ny1 = yos - nys
            ny2 = yos
        else:
            pass
            useful.write_message("on")
        y1 = ny1
        y2 = ny2
    else:
        useful.write_message("shape as is")
        # hit the jackpot, Mel!
        pass
    useful.write_message('set_shape_sizes returned', x1, y1, '/', x2, y2, '->', x2 - x1, y2 - y1)
    return x1, x2, y1, y2
Exemplo n.º 29
0
    def action(self, pif, tdir=None, fn=None):
        log_action = False
        if not fn:
            fn = self.fn
        if not tdir:
            tdir = self.tdir
        ret = {'fn': fn, 'dir': tdir, 'act': True}
        from_path = os.path.join(tdir, fn)
        to_dir = to_name = ''
        if self.delete:
            useful.file_delete(from_path)
        elif self.archive:
            useful.file_mover(from_path,
                              os.path.join(tdir, 'archive', fn),
                              mv=True)
            ret['fn'] = ''
        elif self.fixed:
            useful.file_mover(from_path,
                              os.path.join(tdir, 'fixed', fn),
                              mv=True)
            ret['fn'] = ''
        elif self.rename:
            if not self.nname:
                useful.warn('What? (rename, no name)')
            else:
                to_dir = tdir
                to_name = self.nname
        elif self.lib:
            if not self.man:
                useful.warn('What? (lib, no man)')
            elif not os.path.exists(
                    useful.relpath('.' + config.LIB_MAN_DIR, self.man)):
                #		man2 = pif.dbh.fetch_alias(self.man)
                #		if not man2:
                useful.warn('bad destination')


#		else:
#		    useful.file_mover(from_path, os.path.join('.' + config.LIB_MAN_DIR, man2['ref_id'].lower(), fn), mv=self.mv, ov=self.ov)
            else:
                to_dir = useful.relpath('.' + config.LIB_MAN_DIR, self.man)
                to_name = self.nname
        elif self.mvbin:
            if not os.path.exists(os.path.join('lib', self.cat)):
                useful.warn('bad destination')
            else:
                to_dir = os.path.join('lib', self.cat)
                to_name = self.nname
        elif self.selcat:
            if not (self.man or self.nname) or not self.dest:
                useful.warn('What? (selcat, no name or dest)')
            else:
                ddir = self.dest
                dnam = self.man if self.man else self.nname
                if self.pref:
                    dnam = self.pref + '_' + dnam
                    if self.ptype == 't':
                        ddir = '.' + config.IMG_DIR_ADD
                        if self.suff:
                            dnam += '-' + self.suff
                if dnam:
                    to_name = dnam.lower() + '.jpg'
                    to_dir = ddir
                log_action = True
        elif self.select:
            inc = self.inc
            if not self.man:
                #self.man = tdir[tdir.rfind('/') + 1:]
                useful.warn('Huh? (select, no man)')
            else:
                ddir = '.' + config.IMG_DIR_MAN
                dnam = self.man
                if self.var:
                    ddir = '.' + config.IMG_DIR_VAR
                    dnam = dnam + '-' + self.var
                    if self.pref:
                        dnam = self.pref + '_' + dnam
                elif self.pref:
                    if self.pref != 'n':
                        dnam = self.pref + '_' + dnam
                    if self.ptype == 't':
                        ddir = '.' + config.IMG_DIR_ADD
                        if self.suff:
                            dnam += '-' + self.suff
                        self.inc = True
                else:
                    useful.warn("What? (select, no var or pref)")
                    dnam = ''
                if dnam:
                    to_name = dnam.lower() + '.jpg'
                    to_dir = ddir
                log_action = True
        else:
            ret['act'] = False
        if to_dir:
            useful.file_mover(from_path,
                              os.path.join(to_dir, to_name),
                              mv=self.mv,
                              ov=self.ov,
                              inc=self.inc)
            ret['fn'] = to_name
            ret['dir'] = to_dir
            if log_action:
                title = pif.form.get_str('title', to_name)
                url = 'http://www.bamca.org/' + os.path.join(to_dir, to_name)
                link = 'http://www.bamca.org/' + self.link
                useful.write_message(
                    'Post to Tumblr: ',
                    tumblr.tumblr(pif).create_photo(caption=to_name,
                                                    source=url,
                                                    link=link))
            pif.dbh.write_photo_credit(pif.form.get_str('credit'), to_dir,
                                       to_name)
        return ret
Exemplo n.º 30
0
def shrinker(pth, nname, bound, maxsize, rf):
    useful.write_message('shrinker', pth, nname, bound, maxsize)
    x1, y1, x2, y2 = bound
    xcs = x2 - x1
    ycs = y2 - y1
    xts, yts = maxsize
    if not xts:
        xts = xcs
    if not yts:
        yts = ycs
    useful.write_message(x1, y1, x2, y2, ':', xcs, ycs, ':', xts, yts)
    useful.write_message(pth)
    if xcs == xts and ycs == yts:
        useful.write_message("cutting")
        xts, yts = fix_axes(rf, xts, yts)
        ofi = useful.pipe_chain(
            open_input_file(pth),
            import_file(pth) + cut(x1, y1, x1 + xcs, y1 + ycs) + rot_flip(rf) +
            export_file(nname, pth),
            stderr=open_write_dev_null(),
            verbose=True)
    elif xts // xcs < yts // ycs:
        useful.write_message("shrinking x")
        xts, yts = fix_axes(rf, xts, yts)
        #        ofi = useful.pipe_chain(open_input_file(pth),
        #                                import_file(pth) + cut(x1, y1, x1 + xcs, y1 + ycs) + rot_flip(rf) +
        #                                resize(x=xts) + export_file(nname, pth),
        #                                stderr=open_write_dev_null(), verbose=True)
        ofi = mangler(
            pth,
            cut(x1, y1, x1 + xcs, y1 + ycs) + rot_flip(rf) + resize(x=xts),
            nname)
    else:
        useful.write_message("shrinking y")
        xts, yts = fix_axes(rf, xts, yts)
        #        ofi = useful.pipe_chain(open_input_file(pth),
        #                                import_file(pth) + cut(x1, y1, x1 + xcs, y1 + ycs) + rot_flip(rf) +
        #                                resize(y=yts) + export_file(nname, pth),
        #                                stderr=open_write_dev_null(), verbose=True)
        ofi = mangler(
            pth,
            cut(x1, y1, x1 + xcs, y1 + ycs) + rot_flip(rf) + resize(y=yts),
            nname)
    return ofi