def test_fontpointsize(self): img = Image((300, 200), 'red') img.font_pointsize(60) self.assertEqual(60, img.font_pointsize()) self.assertEqual(float, type(img.font_pointsize())) img.annotate("hello", (100, 100)) img.write('t.png')
def test_size_property(self): im = Image((500, 300), 'red') self.assertEqual(im.width, 500) self.assertEqual(im.height, 300) im.scale(0.5) self.assertEqual(im.width, 250) self.assertEqual(im.height, 150)
def test_size_property(self): img = Image((500, 300), 'red') self.assertEqual(img.width, 500) self.assertEqual(img.height, 300) img.scale(0.5) self.assertEqual(img.width, 250) self.assertEqual(img.height, 150)
def test_composite_arg_gravity(self): base = Image((300, 200), 'green') layer = Image((150, 100), 'transparent') drawer = Draw() drawer.circle(50, 50, 20, 20) layer.draw(drawer) base.composite(layer, 'center', 'over') base.write('t.png')
def test_fill_opacity(self): self.im = Image((600, 400), 'transparent') self.d.fill_color('red') self.d.fill_opacity(0.5) self.d.circle(150, 150, 50, 180) self.d.fill_color('green') self.d.fill_opacity(0.8) self.d.circle(160, 160, 50, 180) self.im.draw(self.d.drawer) self.im.write('t.png')
def test_unicodefilename(self): self.img.write('unicode.png') if sys.version_info >= (3, ): img = Image('unicode.png') else: img = Image(unicode('unicode.png')) img.scale(0.5) if sys.version_info >= (3, ): img.write('unicode.jpg') else: img.write(unicode('unicode.jpg'))
def image2norm(image_path, norm_path): print('image2norm(python) ' + image_path + ' ' + norm_path) sys.stdout.flush() ok = False try: img = Image(image_path) img.write(norm_path) ok = True except Exception as e: print(e) ok = False return ok
def _ensure(id_, folderer, getter): folder = folderer(id_) # Create folder if it does not exists if not os.path.exists(folder): os.makedirs(folder) # Download image original = os.path.join(folder, ORIGINAL_FILENAME) if not os.path.exists(original): getter(original) # Create thumbnail thumb = os.path.join(folder, THUMBNAIL_FILENAME) if os.path.exists(original) and not os.path.exists(thumb): img = Image(original) img.scale((THUMBNAIL_SIZE, THUMBNAIL_SIZE)) img.write(thumb)
def __init__( self, filename = None, color = None, *args, **kargs ): """ Init a adapter. :Args: - filename - This param is a file name or Image of pgmagick. - Color - This is color of pgmagick.api. """ if isinstance( filename, OrginallyImage ): self.__apiImage = ApiImage() self.__apiImage.img = filename else: self.__apiImage = ApiImage( filename, color, args, kargs )
def write(self, filename): try: return Image.write(self, filename) except Exception as e: if 'Warning' not in e.message: raise warnings.warn(e.message, RuntimeWarning)
def read_ipfc(request, ip_sub_file_path): # only allow reading from session working directory (ip_sub_file_path must begin with uuid) file_path = os.path.join(config_path_work, ip_sub_file_path) if not os.path.exists(file_path): return HttpResponseNotFound("File not found %s vs %s" % (ip_sub_file_path, file_path)) elif not os.path.isfile(file_path): return HttpResponseBadRequest("Not a file") else: file_size = fsize(file_path) if file_size <= config_max_filesize_viewer: mime = get_mime_type(file_path) print "MIME" + mime file_content = None if get_mime_type(file_path) == "image/png" or get_mime_type( file_path) == "image/jpg": file_content = read_file_content(file_path) file_content = "data:" + mime + ";base64," + base64.b64encode( file_content) elif get_mime_type(file_path) == "image/tiff" or get_mime_type( file_path) == "image/gif": from pgmagick.api import Image img = Image(file_path) uuid = randomutils.getUniqueID() img.write('/tmp/%s.png' % uuid) print '/tmp/%s.png' % uuid file_content = "data:" + mime + ";base64," + base64.b64encode( read_file_content('/tmp/%s.png' % uuid)) elif get_mime_type(file_path) == "application/pdf": uuid = randomutils.getUniqueID() html_file = ('/tmp/%s.html' % uuid) pdftohtml_cmd = CliCommand.get("pdftohtml", { 'pdf_file': file_path, 'html_file': html_file }) out = check_output(pdftohtml_cmd) file_content = read_file_content(html_file) else: file_content = read_file_content(file_path) return HttpResponse(file_content) else: return HttpResponseForbidden( "Size of requested file exceeds limit (file size %d > %d)" % (file_size, config_max_filesize_viewer))
def pdf_to_images(self, ext="jpg"): ''' PDF to images ''' assert os.path.isfile(self.data.path) i = 0 medias = [] while Image and True: try: image = Image("%s[%d]" % (self.data.path, i)) image_file = os.path.join( UPLOAD_TMP_DIR, 'pdf.%d.%d.%s' % (self.id, i, ext)) image.write(image_file) media = MediaFile.create( image_file, name=image_file.split('/')[-1:][0]) medias.append(media) except Exception: #:TODO Exception type check break i = i + 1 return medias
def arrow( self, background_color = "#FFFFFF", border_color = "#BBBBBB" ): """ Draw a arrow at icon bottom. """ img = self.get_api_image() width = img.width height = img.height center_x = width / 2 arrow_half_width = int( center_x / 3 ) arrow_height = int( height / 7 ) bg_img = ApiImage(( width, height + arrow_height + 1 ),'transparent') # arrow_start_x = center_x - arrow_half_width arrow_end_x = center_x + arrow_half_width center_y = height + arrow_height arrow_bg_start_point = ( arrow_start_x, height -1 ) arrow_bg_end_point = ( arrow_end_x, height - 1 ) arrow_bg_center_point = ( center_x, height + arrow_height ) draw = Draw() draw.composite( 0, 0, width, height, img.img ) bg_img.draw( draw ) points = ( arrow_bg_start_point, arrow_bg_end_point, arrow_bg_center_point, arrow_bg_start_point ) bg_img.draw( self.__create_arrow_bg( points, background_color ) ) bg_img.draw( self.__create_arrow_border( arrow_start_x, arrow_end_x, height, arrow_height, border_color ) ) self.__apiImage = bg_img
def test_scale_with_filtertype(self): img = Image((600, 400), 'gradient:#ffffff-#000000') img.scale(0.6, 'Catrom') img.write('t.jpg') m = hashlib.md5() with open('t.jpg', 'rb') as fp: m.update(fp.read()) scale_with_filtertype_catrom_digest = m.hexdigest() img = Image((600, 400), 'gradient:#ffffff-#000000') img.scale(0.6, 'Cubic') img.write('t.jpg') m = hashlib.md5() with open('t.jpg', 'rb') as fp: m.update(fp.read()) scale_with_filtertype_cubic_digest = m.hexdigest() img = Image((600, 400), 'gradient:#ffffff-#000000') img.scale(0.6) img.write('t.jpg') m = hashlib.md5() with open('t.jpg', 'rb') as fp: m.update(fp.read()) scale_digest = m.hexdigest() self.assertNotEqual(scale_with_filtertype_catrom_digest, scale_digest) self.assertNotEqual(scale_with_filtertype_catrom_digest, scale_with_filtertype_cubic_digest)
def test_arg_float(self): Image((600.5, 400.4), 'red')
def test_nonarg(self): Image()
def test_composite(self): img1 = Image((20, 20), 'plasma:blue') self.d.composite(10, 10, 0, 0, img1) self.img.draw(self.d) self.img.write('t.png')
class ImageTestCase(unittest.TestCase): def setUp(self): self.img = Image((600, 400), 'red') def test_unicodefilename(self): self.img.write('unicode.png') img = Image(u'unicode.png') img.scale(0.5) img.write(u'unicode.jpg') def test_nonarg(self): Image() def test_arg(self): Image((600, 400), 'red') def test_arg_float(self): Image((600.5, 400.4), 'red') def test_blur(self): self.img.blur(2, 2.3) self.img.write('t.jpg') def test_blur_channel(self): self.img.blur_channel('cyan') self.img.write('t.jpg') def test_scale(self): img = Image((600, 400), 'gradient:#ffffff-#000000') img.scale(0.6) img.write('t.jpg') def test_scale_with_filtertype(self): img = Image((600, 400), 'gradient:#ffffff-#000000') img.scale(0.6, 'Catrom') img.write('t.jpg') m = hashlib.md5() with open('t.jpg', 'rb') as fp: m.update(fp.read()) scale_with_filtertype_catrom_digest = m.hexdigest() img = Image((600, 400), 'gradient:#ffffff-#000000') img.scale(0.6, 'Cubic') img.write('t.jpg') m = hashlib.md5() with open('t.jpg', 'rb') as fp: m.update(fp.read()) scale_with_filtertype_cubic_digest = m.hexdigest() img = Image((600, 400), 'gradient:#ffffff-#000000') img.scale(0.6) img.write('t.jpg') m = hashlib.md5() with open('t.jpg', 'rb') as fp: m.update(fp.read()) scale_digest = m.hexdigest() self.assertNotEqual(scale_with_filtertype_catrom_digest, scale_digest) self.assertNotEqual(scale_with_filtertype_catrom_digest, scale_with_filtertype_cubic_digest) def test_composite_arg_list(self): base = Image((300, 200), 'green') layer = Image((300, 200), 'transparent') drawer = Draw() drawer.circle(50, 50, 50, 100) layer.draw(drawer) base.composite(layer, (10, 10), 'over') base.write('t.png') def test_composite_arg_gravity(self): base = Image((300, 200), 'green') layer = Image((150, 100), 'transparent') drawer = Draw() drawer.circle(50, 50, 20, 20) layer.draw(drawer) base.composite(layer, 'center', 'over') base.write('t.png') def test_crop(self): img = Image((300, 200), 'gradient:#ffff00-#00ffff') img.crop(20, 20, 50, 100) img.write('t.png') def test_fontpointsize(self): img = Image((300, 200), 'red') img.font_pointsize(60) self.assertEqual(60, img.font_pointsize()) self.assertEqual(float, type(img.font_pointsize())) if sys.platform.lower() == 'darwin': img.font("/Library/Fonts/Arial.ttf") img.annotate("hello", (100, 100)) img.write('t.png') def test_size_property(self): img = Image((500, 300), 'red') self.assertEqual(img.width, 500) self.assertEqual(img.height, 300) img.scale(0.5) self.assertEqual(img.width, 250) self.assertEqual(img.height, 150)
def test_scale(self): img = Image((600, 400), 'gradient:#ffffff-#000000') img.scale(0.6) img.write('t.jpg')
def test_scale_with_filtertype(self): testset = {"Catrom": None, "Cubic": None, "None": None} for k, v in testset.items(): img = Image((600, 400), 'gradient:#ffffff-#000000') if sys.platform.lower() == 'darwin': img.font("/Library/Fonts/Arial.ttf") img.annotate("hello", (100, 100)) if k != "None": img.scale(0.6, k) else: img.scale(0.6) img.write('t.jpg') m = hashlib.md5() with open('t.jpg', 'rb') as fp: m.update(fp.read()) testset[k] = m.hexdigest() self.assertNotEqual(testset["Catrom"], testset["None"]) self.assertNotEqual(testset["Cubic"], testset["None"]) self.assertNotEqual(testset["Catrom"], testset["Cubic"])
def test_crop(self): img = Image((300, 200), 'gradient:#ffff00-#00ffff') img.crop(20, 20, 50, 100) img.write('t.png')
class Image( object ): """ This is pgmagick.api.Image adapter.And add some util tools. """ def __init__( self, filename = None, color = None, *args, **kargs ): """ Init a adapter. :Args: - filename - This param is a file name or Image of pgmagick. - Color - This is color of pgmagick.api. """ if isinstance( filename, OrginallyImage ): self.__apiImage = ApiImage() self.__apiImage.img = filename else: self.__apiImage = ApiImage( filename, color, args, kargs ) def border( self, size, color_str = "white" ): """ Set a border and color. :Args: - size - This is dict. (example: {"width":2,"height":2} ) - color_str - The default value is white. ( example: "#ffeeff" ) """ img = self.get_pgmagick_image() img.borderColor( Color( color_str ) ) img.border( Geometry( size.get("width"), size.get("height") ) ); def crop( self, bounds ): """ Crop this image. :Args: - bounds - This is bounds dict. ( example:{"width":40,"height":40,"x":0,"y":0} ) """ img = self.get_pgmagick_image() img.crop( Geometry( bounds.get("width"), bounds.get("height"), bounds.get("x"), bounds.get("y") ) ) def resize( self, size, is_force = False ): """ Reset a size. :Args: - size - This is dict. (example: {"width":2,"height":2} ) - isForce - This is boolean.The image width or height will be possible less to set size,when isForce is true. """ if is_force: self.__apiImage.scale( ( size.get("width"), size.get("height") ) ) else: height = self.__apiImage.height width = self.__apiImage.width ratio_height = float( size.get("height") ) / float( height ) ratio_width = float( size.get("width") ) / float( width ) ratio = 1 if ratio_height < ratio_width: ratio = ratio_width else: ratio = ratio_height self.__apiImage.scale( ( ratio * width, ratio * height ) ) def get_blob( self ): """ Get a blob object. """ blob = Blob() img = self.get_pgmagick_image() img.write( blob, "png" ) return blob def get_pgmagick_image( self ): """ Get a pgmagick image. """ return self.__apiImage.img def get_api_image( self ): """ Get a pgmagick.api image. """ return self.__apiImage def __create_arrow_bg( self, points, color ): """ Create a draw of arrow background. """ draw = Draw() draw.stroke_antialias( False ) draw.polygon( points ) draw.fill_color( color ) return draw def __create_arrow_border( self, start_x, end_x , y, arrow_height, color ): """ Create a draw of arrow border. """ center_x = ( start_x + end_x ) /2 center_y = arrow_height + y arrow_border_draw = Draw() arrow_border_draw.stroke_color( color ) arrow_border_draw.stroke_antialias( False ) arrow_border_draw.line( start_x, y, center_x, center_y ) arrow_border_draw.line( center_x, center_y, end_x, y ) arrow_border_draw.stroke_width( 2 ) return arrow_border_draw def arrow( self, background_color = "#FFFFFF", border_color = "#BBBBBB" ): """ Draw a arrow at icon bottom. """ img = self.get_api_image() width = img.width height = img.height center_x = width / 2 arrow_half_width = int( center_x / 3 ) arrow_height = int( height / 7 ) bg_img = ApiImage(( width, height + arrow_height + 1 ),'transparent') # arrow_start_x = center_x - arrow_half_width arrow_end_x = center_x + arrow_half_width center_y = height + arrow_height arrow_bg_start_point = ( arrow_start_x, height -1 ) arrow_bg_end_point = ( arrow_end_x, height - 1 ) arrow_bg_center_point = ( center_x, height + arrow_height ) draw = Draw() draw.composite( 0, 0, width, height, img.img ) bg_img.draw( draw ) points = ( arrow_bg_start_point, arrow_bg_end_point, arrow_bg_center_point, arrow_bg_start_point ) bg_img.draw( self.__create_arrow_bg( points, background_color ) ) bg_img.draw( self.__create_arrow_border( arrow_start_x, arrow_end_x, height, arrow_height, border_color ) ) self.__apiImage = bg_img
def setUp(self): self.im = Image((600, 400), 'red') self.d = Draw()
def print_all_property(): origin_img = Image('test.jpg') print "%dx%d" % (origin_img.width, origin_img.height)
def test_scale(self): im = Image((600, 400), 'red') im.scale(0.6) im.write('t.jpg')
def main(): parser = argparse.ArgumentParser(description="unzip an epub archive to a " "tmp directory, scale images by 50%, and re-archive as epub.") parser.add_argument('-f', '--file', dest='file', action='store', default="", help='epub file to shrink images in. (REQUIRED argument)') parser.add_argument('-d', '--directory', dest='directory', action='store', default="/tmp/smaller_epub/", help='temporary directory: %(default)s') parser.add_argument('-p', '--prefix', dest='prefix', action='store', default="smallscale_", help='prefix to add to output files: %(default)s') parser.add_argument('-w', '--max-width', dest='max_width', action='store', type=int, default=640, help='Max width: %(default)s') parser.add_argument('-m', '--max-height', dest='max_height', action='store', type=int, default=480, help='Max height: %(default)s') parser.add_argument('-s', '--no-recursive-size', dest='no_recursive_size', action='store_true', default=False, help='Only half the size of large images once. Default: keep shrinking ' 'the image by half until it is less than the max limits') parser.add_argument('-v', '--verbose', dest='verbose', action='store', default=0, type=int, metavar = 'N', help='Verbosity level. Anything other than 0 for debug info.') parser.add_argument('-V', '--verbose_on', dest='verbose_on', action='store_true', default=False, help='Set Verbosity level N = 1.') args = parser.parse_args() if args.verbose_on: args.verbose = max(1, args.verbose) if not args.file: parser.print_help() sys.exit() fnoext = os.path.splitext( args.file) print "fnoext = " +fnoext[0] print "fext = " +fnoext[1] tmpdir = os.path.join(args.directory, fnoext[0]) print "tmpdir = " +tmpdir if os.path.isdir(tmpdir): shutil.rmtree(tmpdir) os.makedirs(tmpdir) with zipfile.ZipFile(args.file, 'r') as zip_ref: zip_ref.extractall(tmpdir) for root, dirs, files in os.walk(tmpdir): for f in files: fpath = os.path.join(root, f) #print "fpath = " +fpath flower = f.lower() if flower.endswith(".png"): print("-----" +str(fpath)) img = Image(filename=fpath) elif flower.endswith(".gif"): print("-----" +str(fpath)) img = Image(filename=fpath) elif flower.endswith(".jpg"): print("-----" +str(fpath)) img = Image(filename=fpath) else: img = False if img: print img.width, img.height if not f.startswith(args.prefix): small_file = False fprefix = os.path.join(root, args.prefix +f) while img.width > args.max_width or img.height > args.max_height: width = int(img.width)/2 height = int(img.height)/2 #img.scale(0.5) # pgmagick #img.transform('50%') # wand.image img.resize( width,height ) # wand.image if args.verbose: #print "shrunk " +f +" image to " +str(img.width) +"x" +str(img.height) print "shrunk " +f +" image to " +str(img.size) small_file = True if args.no_recursive_size: break if small_file: if args.verbose: print "writing " +str(img.width) +"x" +str(img.height) +" image to " +fprefix #img.write( fprefix ) #img.write(fpath) #pgmagick img.save(filename=fpath) #wand fbase = os.path.basename( os.path.abspath(args.file) ) fdir = os.path.dirname( os.path.abspath(args.file) ) outfile = os.path.join(fdir, args.prefix +args.file) outbase = os.path.join(fdir, args.prefix +fnoext[0]) print "outbase = " +outbase print "outfile = " +outfile shutil.make_archive(outbase, 'zip', tmpdir) shutil.move(outbase +'.zip', outfile)
def test_unicodefilename(self): self.img.write('unicode.png') img = Image(u'unicode.png') img.scale(0.5) img.write(u'unicode.jpg')
def setUp(self): self.img = Image((600, 400), 'red')
# coding: utf-8 from pgmagick.api import Image img = Image((300, 200)) img.font("/etc/alternatives/fonts-japanese-gothic.ttf") img.annotate('Hello World') img.annotate('ようこそpgmagickへ!!') img.write('japanese-text.png')
class DrawTestCase(unittest.TestCase): def setUp(self): self.img = Image((600, 400), 'red') self.d = Draw() def test_affine(self): self.d.affine(10, 10, 20, 20, 40, 40) self.img.draw(self.d.drawer) self.img.write('t.jpg') def test_arc(self): self.d.arc(30, 30, 40, 40, 40, 40) self.img.draw(self.d.drawer) self.img.write('t.jpg') def test_bezier(self): points = ((30, 30), (50, 75), (200, 100)) self.d.bezier(points) self.img.draw(self.d.drawer) self.img.write('t.png') def test_circle(self): self.d.circle(40, 40, 50, 100) self.img.draw(self.d.drawer) self.img.write('t.png') def test_color(self): self.d.color(40, 40, 'point') self.img.draw(self.d.drawer) self.img.write('t.png') def test_composite(self): img1 = Image((20, 20), 'plasma:blue') self.d.composite(10, 10, 0, 0, img1) self.img.draw(self.d) self.img.write('t.png') def test_draw_for_draw_class(self): self.d.color(40, 40, 'point') self.d.circle(100, 100, 50, 100) self.img.draw(self.d) self.img.write('t.png') def test_ellipse(self): self.d.ellipse(150, 150, 120, 120, 0, 120) self.img.draw(self.d) self.img.write('t.png') def test_fill_color(self): self.d.fill_color('#f09060') self.d.ellipse(150, 150, 120, 120, 0, 120) self.img.draw(self.d) self.img.write('t.png') def test_fill_rule(self): self.d.fill_rule('evenodd') self.d.circle(150, 150, 50, 180) self.d.fill_rule('nonzero') self.d.circle(350, 150, 250, 180) self.img.draw(self.d.drawer) self.img.write('t.png') def test_fill_opacity(self): self.im = Image((600, 400), 'transparent') self.d.fill_color('red') self.d.fill_opacity(0.5) self.d.circle(150, 150, 50, 180) self.d.fill_color('green') self.d.fill_opacity(0.8) self.d.circle(160, 160, 50, 180) self.img.draw(self.d.drawer) self.img.write('t.png') def test_font_style_italic(self): if sys.platform.lower() == 'darwin': self.skipTest("DrawableFont() is broken") self.d.font('vera.ttf', 'italic') self.d.text(30, 30, "hello pgmagick") self.img.draw(self.d) self.img.write('t.png') def test_font_style_oblique(self): if sys.platform.lower() == 'darwin': self.skipTest("DrawableFont() is broken") self.d.font('vera.ttf', 'oblique') self.d.text(30, 30, "hello pgmagick") self.img.draw(self.d) self.img.write('t.png') def test_font_stretch_ultracondensed(self): if sys.platform.lower() == 'darwin': self.skipTest("DrawableFont() is broken") self.d.font('vera.ttf', 'oblique', stretch='ultracondensed') self.d.text(30, 30, "hello pgmagick") self.img.draw(self.d) self.img.write('t.png') def test_font_stretch_extraexpanded(self): if sys.platform.lower() == 'darwin': self.skipTest("DrawableFont() is broken") self.d.font('vera.ttf', 'oblique', stretch='extraexpanded') self.d.text(30, 30, "hello pgmagick") self.img.draw(self.d) self.img.write('t.png') def test_font_weight100(self): if sys.platform.lower() == 'darwin': self.skipTest("DrawableFont() is broken") self.d.font('vera.ttf', weight=100) self.d.text(30, 30, "hello pgmagick") self.img.draw(self.d) self.img.write('t.png') def test_font_bold(self): if sys.platform.lower() == 'darwin': self.skipTest("DrawableFont() is broken") self.d.font('vera.ttf', weight='bold') self.d.text(30, 30, "hello pgmagick") self.img.draw(self.d) self.img.write('t.png') def test_gravity(self): if sys.platform.lower() == 'darwin': self.skipTest("DrawableFont() is broken") self.d.gravity('center') self.d.text(0, 0, "hello pgmagick") self.img.draw(self.d) self.img.write('t.png') def test_line(self): self.d.line(10, 10, 40, 200) self.img.draw(self.d) self.img.write('t.png') def test_matte(self): self.d.matte(30, 30, 'filltoborder') self.img.draw(self.d) self.img.write('t.png') def test_miterlimit(self): self.d.miterlimit(1) self.d.stroke_color('black') self.d.stroke_width(3) self.d.line(10, 200, 100, 10) self.d.line(100, 10, 210, 200) self.img.draw(self.d) self.d = Draw() self.d.miterlimit(18) self.d.stroke_color('black') self.d.stroke_width(3) self.d.stroke_opacity(0.5) self.d.fill_opacity(0.5) self.d.line(210, 200, 300, 10) self.d.line(300, 10, 410, 200) self.img.draw(self.d) self.img.write('t.png') def test_path(self): paths = ((40, 30), ) self.d.path(paths) self.img.draw(self.d.drawer) self.img.write('t.png') def test_point(self): for i in range(50): self.d.point(i, i + 10) for i in range(50, 200, 2): self.d.point(i, i + 10) self.img.draw(self.d.drawer) self.img.write('t.png') def test_pointsize(self): self.d.pointsize(10) self.d.pointsize(30.) self.d.circle(150, 150, 50, 180) self.img.draw(self.d.drawer) self.img.write('t.png') def test_polygon(self): coordinate = ((10, 10), (20, 30), (40, 50)) self.d.polygon(coordinate) coordinate = [(100, 100), (150, 100), [150, 150], (100, 150)] self.d.polygon(coordinate) self.img.draw(self.d) self.img.write('t.png') def test_polyline(self): coordinate = ((10, 10), (20, 30), (40, 50)) self.d.polyline(coordinate) coordinate = [(100, 100), (150, 100), [150, 150], (100, 150)] self.d.polyline(coordinate) self.img.draw(self.d) self.img.write('t.png') def test_rectangle(self): self.d.rectangle(50, 50, 100, 100) self.img.draw(self.d) self.img.write('t.png') def test_rotation(self): self.d.rectangle(150, 150, 200, 200) self.d.rotation(40) self.img.draw(self.d) self.d = Draw() self.d.fill_opacity(0.8) self.d.rectangle(150, 150, 200, 200) self.d.rotation(20) self.img.draw(self.d) self.d = Draw() self.d.fill_opacity(0.6) self.d.rectangle(150, 150, 200, 200) self.img.draw(self.d) self.img.write('t.png') def test_round_rectangle(self): self.d = Draw() self.d.round_rectangle(20, 20, 30, 30, 150, 150) self.img.draw(self.d) self.img.write('t.png') def test_scaling(self): self.d.scaling(0.4, 0.4) self.d.circle(150, 150, 150, 200) self.img.draw(self.d) self.d = Draw() self.d.fill_opacity(0.6) self.d.circle(150, 150, 150, 200) self.img.draw(self.d) self.img.write('t.png') def test_skew(self): self.d = Draw() self.d.fill_opacity(0.6) self.d.circle(50, 50, 50, 100) self.img.draw(self.d) self.d = Draw() self.d.fill_opacity(0.6) self.d.circle(50, 50, 50, 100) self.d.skewx(40) self.img.draw(self.d) self.d = Draw() self.d.fill_opacity(0.6) self.d.circle(50, 50, 50, 100) self.d.skewy(40) self.img.draw(self.d) self.img.write('t.png') def test_stroke_antialias(self): self.d.stroke_color('black') self.d.stroke_width(20) self.d.stroke_antialias(True) self.d.line(10, 10, 40, 200) self.d.stroke_antialias(False) self.d.line(50, 10, 80, 200) self.img.draw(self.d) self.img.write('t.png') def test_stroke_linecap(self): self.d.stroke_color('lime') self.d.stroke_linecap('butt') self.d.stroke_linecap('round') self.d.stroke_linecap('square') self.d.line(10, 10, 40, 200) self.img.draw(self.d) self.img.write('t.png') @unittest.skipIf(LIBGM_VERSION <= [1, 3, 18], "bug in gm version: %s" % str(LIBGM_VERSION)) def test_stroke_linejoin(self): self.d.stroke_color('lime') self.d.stroke_linejoin('round') self.d.stroke_linejoin('bevel') self.d.stroke_linejoin('miter') self.d.line(10, 10, 40, 200) self.img.draw(self.d) self.img.write('t.png') def test_stroke_color(self): self.d.stroke_color('lime') self.d.line(10, 10, 40, 200) self.img.draw(self.d) self.img.write('t.png') def test_stroke_width(self): self.d.stroke_width(20) self.d.line(20, 20, 50, 210) self.img.draw(self.d) self.img.write('t.png') def test_text(self): if sys.platform.lower() == 'darwin': self.skipTest("DrawableFont() is broken") self.d.text(30, 30, "hello pgmagick") self.img.draw(self.d) self.img.write('t.png') def test_text_antialias(self): if sys.platform.lower() == 'darwin': self.skipTest("DrawableFont() is broken") self.d.font('courier', weight='bold') self.d.pointsize(70) self.d.text_antialias(False) self.d.text(30, 100, "hello pgmagick") self.d.text_antialias(True) self.d.text(30, 200, "hello pgmagick") self.img.draw(self.d) self.img.write('t.png') def test_text_decoration(self): if sys.platform.lower() == 'darwin': self.skipTest("DrawableFont() is broken") self.d.pointsize(70) self.d.text_decoration('overline') self.d.text(30, 100, "hello pgmagick") self.d.text_decoration('linethrough') self.d.text(30, 200, "hello pgmagick") self.d.text_decoration('underline') self.d.text(30, 300, "hello pgmagick") self.img.draw(self.d) self.img.write('t.png') def test_text_undercolor(self): if sys.platform.lower() == 'darwin': self.skipTest("DrawableFont() is broken") self.d.pointsize(70) self.d.text_undercolor('lime') self.d.text(30, 100, "hello pgmagick") self.img.draw(self.d) self.img.write('t.png') def test_translate(self): self.d.translate(10, 200) self.img.draw(self.d) self.img.write('t.png')
def test_nonarg(self): im = Image()
class ImageTestCase(unittest.TestCase): def setUp(self): self.img = Image((600, 400), 'red') def test_unicodefilename(self): self.img.write('unicode.png') if sys.version_info >= (3, ): img = Image('unicode.png') else: img = Image(unicode('unicode.png')) img.scale(0.5) if sys.version_info >= (3, ): img.write('unicode.jpg') else: img.write(unicode('unicode.jpg')) def test_nonarg(self): im = Image() def test_arg(self): im = Image((600, 400), 'red') def test_arg_float(self): im = Image((600.5, 400.4), 'red') def test_blur(self): self.img.blur(2, 2.3) self.img.write('t.jpg') def test_blur_channel(self): self.img.blur_channel('cyan') self.img.write('t.jpg') def test_scale(self): im = Image((600, 400), 'red') im.scale(0.6) im.write('t.jpg') def test_composite_arg_list(self): base = Image((300, 200), 'green') layer = Image((300, 200), 'transparent') drawer = Draw() drawer.circle(50, 50, 50, 100) layer.draw(drawer) base.composite(layer, (10, 10), 'over') base.write('t.png') def test_composite_arg_gravity(self): base = Image((300, 200), 'green') layer = Image((150, 100), 'transparent') drawer = Draw() drawer.circle(50, 50, 20, 20) layer.draw(drawer) base.composite(layer, 'center', 'over') base.write('t.png') def test_crop(self): img = Image((300, 200), 'gradient:#ffff00-#00ffff') img.crop(20, 20, 50, 100) img.write('t.png') def test_fontpointsize(self): img = Image((300, 200), 'red') img.font_pointsize(60) self.assertEqual(60, img.font_pointsize()) self.assertEqual(float, type(img.font_pointsize())) img.annotate("hello", (100, 100)) img.write('t.png') def test_size_property(self): im = Image((500, 300), 'red') self.assertEqual(im.width, 500) self.assertEqual(im.height, 300) im.scale(0.5) self.assertEqual(im.width, 250) self.assertEqual(im.height, 150)
def test_fontpointsize(self): img = Image((300, 200), 'red') img.font_pointsize(60) self.assertEqual(60, img.font_pointsize()) self.assertEqual(float, type(img.font_pointsize())) if sys.platform.lower() == 'darwin': img.font("/Library/Fonts/Arial.ttf") img.annotate("hello", (100, 100)) img.write('t.png')
def test_arg(self): Image((600, 400), 'red')
class DrawTestCase(unittest.TestCase): def setUp(self): self.im = Image((600, 400), 'red') self.d = Draw() def test_affine(self): self.d.affine(10, 10, 20, 20, 40, 40) self.im.draw(self.d.drawer) self.im.write('t.jpg') def test_arc(self): self.d.arc(30, 30, 40, 40, 40, 40) self.im.draw(self.d.drawer) self.im.write('t.jpg') def test_bezier(self): points = ((30, 30), (50, 75), (200, 100)) self.d.bezier(points) self.im.draw(self.d.drawer) self.im.write('t.png') def test_circle(self): self.d.circle(40, 40, 50, 100) self.im.draw(self.d.drawer) self.im.write('t.png') def test_color(self): self.d.color(40, 40, 'point') self.im.draw(self.d.drawer) self.im.write('t.png') def test_composite(self): img1 = Image((20, 20), 'plasma:blue') self.d.composite(10, 10, 0, 0, img1) self.im.draw(self.d) self.im.write('t.png') def test_draw_for_draw_class(self): self.d.color(40, 40, 'point') self.d.circle(100, 100, 50, 100) self.im.draw(self.d) self.im.write('t.png') def test_ellipse(self): self.d.ellipse(150, 150, 120, 120, 0, 120) self.im.draw(self.d) self.im.write('t.png') def test_fill_color(self): self.d.fill_color('#f09060') self.d.ellipse(150, 150, 120, 120, 0, 120) self.im.draw(self.d) self.im.write('t.png') def test_fill_rule(self): self.d.fill_rule('evenodd') self.d.circle(150, 150, 50, 180) self.d.fill_rule('nonzero') self.d.circle(350, 150, 250, 180) self.im.draw(self.d.drawer) self.im.write('t.png') def test_fill_opacity(self): self.im = Image((600, 400), 'transparent') self.d.fill_color('red') self.d.fill_opacity(0.5) self.d.circle(150, 150, 50, 180) self.d.fill_color('green') self.d.fill_opacity(0.8) self.d.circle(160, 160, 50, 180) self.im.draw(self.d.drawer) self.im.write('t.png') def test_font_style_italic(self): self.d.font('vera.ttf', 'italic') self.d.text(30, 30, "hello pgmagick") self.im.draw(self.d) self.im.write('t.png') def test_font_style_oblique(self): self.d.font('vera.ttf', 'oblique') self.d.text(30, 30, "hello pgmagick") self.im.draw(self.d) self.im.write('t.png') def test_font_stretch_ultracondensed(self): self.d.font('vera.ttf', 'oblique', stretch='ultracondensed') self.d.text(30, 30, "hello pgmagick") self.im.draw(self.d) self.im.write('t.png') def test_font_stretch_extraexpanded(self): self.d.font('vera.ttf', 'oblique', stretch='extraexpanded') self.d.text(30, 30, "hello pgmagick") self.im.draw(self.d) self.im.write('t.png') def test_font_weight100(self): self.d.font('vera.ttf', weight=100) self.d.text(30, 30, "hello pgmagick") self.im.draw(self.d) self.im.write('t.png') def test_font_bold(self): self.d.font('vera.ttf', weight='bold') self.d.text(30, 30, "hello pgmagick") self.im.draw(self.d) self.im.write('t.png') def test_gravity(self): self.d.gravity('center') self.d.text(0, 0, "hello pgmagick") self.im.draw(self.d) self.im.write('t.png') def test_line(self): self.d.line(10, 10, 40, 200) self.im.draw(self.d) self.im.write('t.png') def test_matte(self): self.d.matte(30, 30, 'filltoborder') self.im.draw(self.d) self.im.write('t.png') def test_miterlimit(self): self.d.miterlimit(0) self.d.stroke_color('black') self.d.stroke_width(3) self.d.line(10, 200, 100, 10) self.d.line(100, 10, 210, 200) self.im.draw(self.d) self.d = Draw() self.d.miterlimit(18) self.d.stroke_color('black') self.d.stroke_width(3) self.d.stroke_opacity(0.5) self.d.fill_opacity(0.5) self.d.line(210, 200, 300, 10) self.d.line(300, 10, 410, 200) self.im.draw(self.d) self.im.write('t.png') def test_path(self): paths = ((40, 30),) self.d.path(paths) self.im.draw(self.d.drawer) self.im.write('t.png') def test_point(self): for i in range(50): self.d.point(i, i + 10) for i in range(50, 200, 2): self.d.point(i, i + 10) self.im.draw(self.d.drawer) self.im.write('t.png') def test_pointsize(self): self.d.pointsize(10) self.d.pointsize(30.) self.d.circle(150, 150, 50, 180) self.im.draw(self.d.drawer) self.im.write('t.png') def test_polygon(self): coordinate = ((10, 10), (20, 30), (40, 50)) self.d.polygon(coordinate) coordinate = [(100, 100), (150, 100), [150, 150], (100, 150)] self.d.polygon(coordinate) self.im.draw(self.d) self.im.write('t.png') def test_polyline(self): coordinate = ((10, 10), (20, 30), (40, 50)) self.d.polyline(coordinate) coordinate = [(100, 100), (150, 100), [150, 150], (100, 150)] self.d.polyline(coordinate) self.im.draw(self.d) self.im.write('t.png') def test_rectangle(self): self.d.rectangle(50, 50, 100, 100) self.im.draw(self.d) self.im.write('t.png') def test_rotation(self): self.d.rectangle(150, 150, 200, 200) self.d.rotation(40) self.im.draw(self.d) self.d = Draw() self.d.fill_opacity(0.8) self.d.rectangle(150, 150, 200, 200) self.d.rotation(20) self.im.draw(self.d) self.d = Draw() self.d.fill_opacity(0.6) self.d.rectangle(150, 150, 200, 200) self.im.draw(self.d) self.im.write('t.png') def test_round_rectangle(self): self.d.round_rectangle(200, 200, 30, 30, 15, 15) self.im.draw(self.d) self.im.write('t.png') def test_scaling(self): self.d.scaling(0.4, 0.4) self.d.circle(150, 150, 150, 200) self.im.draw(self.d) self.d = Draw() self.d.fill_opacity(0.6) self.d.circle(150, 150, 150, 200) self.im.draw(self.d) self.im.write('t.png') def test_skew(self): self.d = Draw() self.d.fill_opacity(0.6) self.d.circle(50, 50, 50, 100) self.im.draw(self.d) self.d = Draw() self.d.fill_opacity(0.6) self.d.circle(50, 50, 50, 100) self.d.skewx(40) self.im.draw(self.d) self.d = Draw() self.d.fill_opacity(0.6) self.d.circle(50, 50, 50, 100) self.d.skewy(40) self.im.draw(self.d) self.im.write('t.png') def test_stroke_antialias(self): self.d.stroke_color('black') self.d.stroke_width(20) self.d.stroke_antialias(True) self.d.line(10, 10, 40, 200) self.d.stroke_antialias(False) self.d.line(50, 10, 80, 200) self.im.draw(self.d) self.im.write('t.png') def test_stroke_linecap(self): self.d.stroke_color('lime') self.d.stroke_linecap('butt') self.d.stroke_linecap('round') self.d.stroke_linecap('square') self.d.line(10, 10, 40, 200) self.im.draw(self.d) self.im.write('t.png') def test_stroke_linejoin(self): self.d.stroke_color('lime') self.d.stroke_linejoin('round') self.d.stroke_linejoin('bevel') # FIXME: Unit Test Error self.d.stroke_linejoin('miter') self.d.line(10, 10, 40, 200) self.im.draw(self.d) self.im.write('t.png') def test_stroke_color(self): self.d.stroke_color('lime') self.d.line(10, 10, 40, 200) self.im.draw(self.d) self.im.write('t.png') def test_stroke_width(self): self.d.stroke_width(20) self.d.line(20, 20, 50, 210) self.im.draw(self.d) self.im.write('t.png') def test_text(self): self.d.text(30, 30, "hello pgmagick") self.im.draw(self.d) self.im.write('t.png') def test_text_antialias(self): self.d.font('courier', weight='bold') self.d.pointsize(70) self.d.text_antialias(False) self.d.text(30, 100, "hello pgmagick") self.d.text_antialias(True) self.d.text(30, 200, "hello pgmagick") self.im.draw(self.d) self.im.write('t.png') def test_text_decoration(self): self.d.pointsize(70) self.d.text_decoration('overline') self.d.text(30, 100, "hello pgmagick") self.d.text_decoration('linethrough') self.d.text(30, 200, "hello pgmagick") self.d.text_decoration('underline') self.d.text(30, 300, "hello pgmagick") self.im.draw(self.d) self.im.write('t.png') def test_text_undercolor(self): self.d.pointsize(70) self.d.text_undercolor('lime') self.d.text(30, 100, "hello pgmagick") self.im.draw(self.d) self.im.write('t.png') def test_translate(self): self.d.translate(10, 200) self.im.draw(self.d) self.im.write('t.png')
class ImageTestCase(unittest.TestCase): def setUp(self): self.img = Image((600, 400), 'red') def test_unicodefilename(self): self.img.write('unicode.png') if sys.version_info >= (3, ): img = Image('unicode.png') else: img = Image(unicode('unicode.png')) img.scale(0.5) if sys.version_info >= (3, ): img.write('unicode.jpg') else: img.write(unicode('unicode.jpg')) def test_nonarg(self): Image() def test_arg(self): Image((600, 400), 'red') def test_arg_float(self): Image((600.5, 400.4), 'red') def test_blur(self): self.img.blur(2, 2.3) self.img.write('t.jpg') def test_blur_channel(self): self.img.blur_channel('cyan') self.img.write('t.jpg') def test_scale(self): im = Image((600, 400), 'red') im.scale(0.6) im.write('t.jpg') def test_composite_arg_list(self): base = Image((300, 200), 'green') layer = Image((300, 200), 'transparent') drawer = Draw() drawer.circle(50, 50, 50, 100) layer.draw(drawer) base.composite(layer, (10, 10), 'over') base.write('t.png') def test_composite_arg_gravity(self): base = Image((300, 200), 'green') layer = Image((150, 100), 'transparent') drawer = Draw() drawer.circle(50, 50, 20, 20) layer.draw(drawer) base.composite(layer, 'center', 'over') base.write('t.png') def test_crop(self): img = Image((300, 200), 'gradient:#ffff00-#00ffff') img.crop(20, 20, 50, 100) img.write('t.png') def test_fontpointsize(self): img = Image((300, 200), 'red') img.font_pointsize(60) self.assertEqual(60, img.font_pointsize()) self.assertEqual(float, type(img.font_pointsize())) img.annotate("hello", (100, 100)) img.write('t.png') def test_size_property(self): im = Image((500, 300), 'red') self.assertEqual(im.width, 500) self.assertEqual(im.height, 300) im.scale(0.5) self.assertEqual(im.width, 250) self.assertEqual(im.height, 150)