示例#1
0
def outputfilter_watermark(filter):

    s_in = None
    s_out = None
    try:
        s_in = StringIO(filter.read())
        im_in = Image.open(s_in)
        if im_in.size[0] <= 240 and im_in.size[1] <= 120:
            filter.write(s_in.getvalue())
            return

        # if this is a sub request, then don't process again
        req = filter.req
        if req.main:
            filter.write(s_in.getvalue())
            return

        cookies = Cookie.get_cookies(req)
        ticket = cookies['login_ticket'].value
        query = req.parsed_uri[apache.URI_QUERY]

        if query == "watermark=false":
            filter.write(s_in.getvalue())
            ticket_sobj = Ticket.get_by_valid_key(ticket)

            # if this is not a valid ticket, then just exit with no image
            if not ticket_sobj:
                return

            # TODO: need fancier algorithm here
            if ticket_sobj.get_value("login") == 'admin':
                filter.write(s_in.getvalue())
                return

        sizex = im_in.size[0]
        sizey = im_in.size[1]
        max_res = 240
        max_width = 640
        im_in = im_in.resize((max_res, int(sizey / (sizex / float(max_res)))))
        im_in = im_in.resize(
            (max_width, int(sizey / (sizex / float(max_width)))))

        # add the watermark
        watermark = Watermark()
        now = datetime.today().strftime("%Y/%m/%d, %H:%M")
        texts = ['Do Not Copy', ticket, now]
        sizes = [20, 10, 10, 20, 20]

        mark = watermark.generate(texts, sizes)
        im_out = watermark.execute(im_in, mark, 'tile', 0.5)

        s_out = StringIO()
        im_out.save(s_out, format='jpeg')
        filter.write(s_out.getvalue())

    finally:
        if s_in:
            s_in.close()
        if s_out:
            s_out.close()
示例#2
0
def test():
    watermark = Watermark()

    texts = ['Do Not Copy', 'Joe Smith', 'id=12345', 'Feb 12, 2011']
    #watermark_path = './sample-watermark.png'
    #mark = Image.open(watermark_path)

    path = '/home/apache/california_web.jpg'
    im = Image.open(path)

    for i in range(1, 10):
        mark = watermark.generate(texts, i*5)
        im2 = watermark.execute(im, mark, 'tile', 0.5)
        im2.save("./test/tile%s.jpg" % i)
示例#3
0
def test():
    watermark = Watermark()

    texts = ['Do Not Copy', 'Joe Smith', 'id=12345', 'Feb 12, 2011']
    #watermark_path = './sample-watermark.png'
    #mark = Image.open(watermark_path)

    path = '/home/apache/california_web.jpg'
    im = Image.open(path)

    for i in range(1, 10):
        mark = watermark.generate(texts, i * 5)
        im2 = watermark.execute(im, mark, 'tile', 0.5)
        im2.save("./test/tile%s.jpg" % i)
示例#4
0
def outputfilter_watermark(filter):

    s_in = None
    s_out = None
    try:
        s_in = StringIO(filter.read())
        im_in = Image.open(s_in)
        if im_in.size[0] <= 240 and im_in.size[1] <= 120:
            filter.write(s_in.getvalue())
            return

        # if this is a sub request, then don't process again
        req = filter.req
        if req.main:
            filter.write(s_in.getvalue())
            return

        cookies = Cookie.get_cookies(req)
        ticket = cookies['login_ticket'].value
        query = req.parsed_uri[apache.URI_QUERY]


        if query == "watermark=false":
            filter.write(s_in.getvalue())
            ticket_sobj = Ticket.get_by_valid_key(ticket)

            # if this is not a valid ticket, then just exit with no image
            if not ticket_sobj:
                return

            # TODO: need fancier algorithm here
            if ticket_sobj.get_value("login") == 'admin':
                filter.write(s_in.getvalue())
                return

        sizex = im_in.size[0]
        sizey = im_in.size[1]
        max_res = 240
        max_width = 640
        im_in = im_in.resize( (max_res, int(sizey/(sizex/float(max_res)))) )
        im_in = im_in.resize( (max_width, int(sizey/(sizex/float(max_width)))) )

        # add the watermark
        watermark = Watermark()
        now = datetime.today().strftime("%Y/%m/%d, %H:%M")
        texts = ['Do Not Copy', ticket, now]
        sizes = [20, 10, 10, 20, 20]

        mark = watermark.generate(texts, sizes)
        im_out = watermark.execute(im_in, mark, 'tile', 0.5)


        s_out = StringIO()
        im_out.save(s_out, format='jpeg')
        filter.write(s_out.getvalue())

    finally:
        if s_in:
            s_in.close()
        if s_out:
            s_out.close()