Example #1
0
def split():    
    image = pystacia.read(sys.args[1])
    rawdata = image.get_blob('png')
    r.set("image_dimensions", json.dumps([image.width, image.height]))
    x = 0
    y = 0     
    while x < image.width:        
        y = 0    
        while y < image.height:
            tmpimg = pystacia.read_blob(rawdata)
            x1 = x
            y1 = y
            x2 = x + 80
            if x2 > image.width:
                x2 = image.width
            y2 = y + 80
            if y2 > image.height:
                y2 = image.height
            print "r:", x1, y1, x2, y2
            # crop is resize ohhh ?
            # http://liquibits.bitbucket.org/image.html#resizing
            # params are: width, height, x, y
            data = tmpimg.resize(x2-x1, y2-y1, x1, y1)
            key = "img_%s_%s_%s_%s" % (x1, y1, x2, y2)
            r.set(key, pickle.dumps(data.get_blob('png')))
            r.lpush("image_parts", key)            
            y += 80
        x += 80
    return r.execute_command("LGETALL", 'image_parts')
Example #2
0
    def check_image(png_filename, svg_filename):
        """Check that the pixels match between ``svg`` and ``png``."""
        image1 = pystacia.read(png_filename).get_raw('RGBA')
        pixels1, width1, height1 = (
            image1['raw'], image1['width'], image1['height'])
        png_filename = os.path.join(
            OUTPUT_FOLDER, os.path.basename(png_filename))
        cairosvg.svg2png(url=svg_filename, write_to=png_filename, dpi=72)
        image2 = pystacia.read(png_filename).get_raw('RGBA')
        pixels2, width2, height2 = (
            image2['raw'], image2['width'], image2['height'])

        # Test size
        assert abs(width1 - width2) <= SIZE_TOLERANCE, \
            "Bad width (%s != %s)" % (width1, width2)
        assert abs(height1 - height2) <= SIZE_TOLERANCE, \
            "Bad height (%s != %s)" % (height1, height2)

        # Test pixels
        if pixels1 == pixels2:
            return
        width = min(width1, width2)
        height = min(height1, height2)
        if PYTHON_3:  # Iterating on bytes gives ints on Python 3
            pixels1 = list(pixels1)
            pixels2 = list(pixels2)
        else:  # Iterating on bytes gives bytes on Python 2. Get ints.
            pixels1 = map(ord, pixels1)
            pixels2 = map(ord, pixels2)
        stride = 4 * width
        for j in range(0, stride * height, stride):
            if pixels1[j:j + stride] == pixels2[j:j + stride]:
                continue
            for i in range(j, j + stride, 4):
                pixel1 = pixels1[i:i + 3]
                pixel2 = pixels2[i:i + 3]
                alpha1 = pixels1[i + 3]
                alpha2 = pixels2[i + 3]
                pixel1 = [value * alpha1 for value in pixel1] + [alpha1 * 255]
                pixel2 = [value * alpha2 for value in pixel2] + [alpha2 * 255]
                assert pixel1 == pixel2 or all(
                    abs(value1 - value2) <= PIXEL_TOLERANCE
                    for value1, value2 in zip(pixel1, pixel2)
                ), "Bad pixel %i, %i (%s != %s)" % (
                    (i // 4) % width, (i // 4) // width, pixel1, pixel2)
Example #3
0
 def get_image_data(self, room_name, page_no):
     image_url = os.path.join("files", room_name, str(page_no) + "_image.png")
     image_path = os.path.join(config.ROOT_DIR, image_url)
     try:
         image = read(image_path)
     except IOError as e:
         self.logger.error("Error %s while reading image at location %s" % (e,
                                                                            image_path))
         return '', -1, -1
     width, height = image.size
     return image_url, width, height
Example #4
0
    def test(self):
        import pystacia
        from pystacia import image

        with catch_warnings(record=True) as w:
            simplefilter('always')

            self.assertTrue(
                image.blank(30, 30).is_same(pystacia.blank(30, 30)))

            self.assertTrue('blank' in w[-1].message.args[0])

            if lena_available():
                self.assertTrue(image.lena().is_same(pystacia.lena()))
                self.assertTrue('lena' in w[-1].message.args[0])

            tmpname = mkstemp()[1] + '.bmp'
            img = sample()
            img.write(tmpname)

            self.assertTrue(
                pystacia.read(tmpname).is_same(image.read(tmpname)))
            self.assertTrue('read' in w[-1].message.args[0])

            self.assertTrue(
                pystacia.read_blob(img.get_blob('bmp')).is_same(
                    image.read_blob(img.get_blob('bmp'))))

            self.assertTrue(
                pystacia.read_raw(**img.get_raw('rgb')).is_same(
                    image.read_raw(**img.get_raw('rgb'))))

            img.close()

            for symbol in [
                    'magick_logo', 'wizard', 'netscape', 'granite', 'rose'
            ]:
                self.assertTrue(
                    getattr(image,
                            symbol)().is_same(getattr(pystacia, symbol)()))
                self.assertTrue(symbol in w[-1].message.args[0])

            self.assertIsInstance(pystacia.Image(), image.Image)

            names = [
                'composites', 'types', 'filters', 'colorspaces',
                'compressions', 'axes'
            ]
            for name in names:
                self.assertEqual(
                    getattr(pystacia, name).x,
                    getattr(image, name).x)
                self.assertTrue(name in w[-1].message.args[0])
Example #5
0
 def render(name):
     raw = rendered.get(name)
     if raw is None:
         # name is sometimes "support/something.htm"
         basename = os.path.basename(name)
         png_filename = os.path.join(RESULTS_DIRECTORY, basename + '.png')
         HTML(BASE_URL + name).write_png(
             png_filename, stylesheets=[PAGE_SIZE_STYLESHEET])
         with closing(pystacia.read(png_filename)) as image:
             raw = image.get_raw('rgba')
         rendered[name] = raw
     return raw
Example #6
0
 def get_image_data(self, room_name, page_no):
     image_url = os.path.join("files", room_name,
                              str(page_no) + "_image.png")
     image_path = os.path.join(config.ROOT_DIR, image_url)
     try:
         image = read(image_path)
     except IOError as e:
         self.logger.error("Error %s while reading image at location %s" %
                           (e, image_path))
         return '', -1, -1
     width, height = image.size
     return image_url, width, height
    def setUp(self):
        class Image(ep.Object):
            image = ep.f.Image()
            thumb = ep.f.Thumb(origin=image, size=(100, 100))

        self.Image = Image
        self.conn = get_test_conn()
        ep.set_context(self.conn)
        ep.start_session()
        img = pystacia.read(os.path.join(HERE, 'pythons.yotpeg'))
        img.filename = 'pythons.jpeg'
        pythons = Image(image=img)
        ep.add(pythons)
        ep.commit()
Example #8
0
    def check_image(png_filename, svg_filename):
        """Check that the pixels match between ``svg`` and ``png``."""
        image1 = pystacia.read(png_filename).get_raw('RGBA')
        pixels1, width1, height1 = (
            image1['raw'], image1['width'], image1['height'])
        png_filename = os.path.join(
            OUTPUT_FOLDER, os.path.basename(png_filename))
        cairosvg.svg2png(url=svg_filename, write_to=png_filename, dpi=72)
        image2 = pystacia.read(png_filename).get_raw('RGBA')
        pixels2, width2, height2 = (
            image2['raw'], image2['width'], image2['height'])

        # Test size
        assert abs(width1 - width2) <= SIZE_TOLERANCE, \
            "Bad width (%s != %s)" % (width1, width2)
        assert abs(height1 - height2) <= SIZE_TOLERANCE, \
            "Bad height (%s != %s)" % (height1, height2)

        # Test pixels
        width = min(width1, width2)
        height = min(height1, height2)
        pixels1 = list(pixels1)
        pixels2 = list(pixels2)
        for i in range(0, 4 * width * height, 4):
            pixel1 = get_pixel(pixels1, i)
            alpha_pixel1 = (
                [pixel1[3] * value for value in pixel1[:3]] +
                [255 * pixel1[3]])
            pixel2 = get_pixel(pixels2, i)
            alpha_pixel2 = (
                [pixel2[3] * value for value in pixel2[:3]] +
                [255 * pixel2[3]])
            for value1, value2 in zip(alpha_pixel1, alpha_pixel2):
                assert abs(value1 - value2) <= PIXEL_TOLERANCE, \
                    "Bad pixel %i, %i (%s != %s)" % (
                        (i // 4) % width, (i // 4) // width, pixel1, pixel2)
Example #9
0
    def test(self):
        import pystacia
        from pystacia import image

        with catch_warnings(record=True) as w:
            simplefilter('always')

            self.assertTrue(image.blank(30, 30).
                            is_same(pystacia.blank(30, 30)))

            self.assertTrue('blank' in w[-1].message.args[0])

            if lena_available():
                self.assertTrue(image.lena().is_same(pystacia.lena()))
                self.assertTrue('lena' in w[-1].message.args[0])

            tmpname = mkstemp()[1] + '.bmp'
            img = sample()
            img.write(tmpname)

            self.assertTrue(pystacia.read(tmpname).
                            is_same(image.read(tmpname)))
            self.assertTrue('read' in w[-1].message.args[0])

            self.assertTrue(pystacia.read_blob(img.get_blob('bmp')).
                            is_same(image.read_blob(img.get_blob('bmp'))))

            self.assertTrue(pystacia.read_raw(**img.get_raw('rgb')).
                            is_same(image.read_raw(**img.get_raw('rgb'))))

            img.close()

            for symbol in ['magick_logo', 'wizard',
                           'netscape', 'granite', 'rose']:
                self.assertTrue(getattr(image, symbol)().
                                is_same(getattr(pystacia, symbol)()))
                self.assertTrue(symbol in w[-1].message.args[0])

            self.assertIsInstance(pystacia.Image(), image.Image)

            names = ['composites', 'types', 'filters', 'colorspaces',
                     'compressions', 'axes']
            for name in names:
                self.assertEqual(getattr(pystacia, name).x,
                                 getattr(image, name).x)
                self.assertTrue(name in w[-1].message.args[0])
Example #10
0
import pystacia

noise = '''iso=32; rone=rand(); rtwo=rand();
myn=sqrt(-2*ln(rone))*cos(2*Pi*rtwo); myntwo=sqrt(-2*ln(rtwo))*
cos(2*Pi*rone); pnoise=sqrt(p)*myn*sqrt(iso)* 
channel(4.28,3.86,6.68,0)/255; max(0,p+pnoise)''' 


if __name__ == "__main__":
    image = pystacia.read('dont_panic.jpg')
    image.fx('''iso=32; rone=rand(); rtwo=rand();
myn=sqrt(-2*ln(rone))*cos(2*Pi*rtwo); myntwo=sqrt(-2*ln(rtwo))*
cos(2*Pi*rone); pnoise=sqrt(p)*myn*sqrt(iso)* 
channel(4.28,3.86,6.68,0)/255; max(0,p+pnoise)''')
    image.write("cloned.jpg")