def punch(pm, x00, y00, x03, y03): step = x03 - x00 if step < 3: # stop recursion if square < 3 x 3 return step = step // 3 # define short names for square corner coordinates x01 = x00 + step x02 = x01 + step y01 = y00 + step y02 = y01 + step # we clear the middle square and recurse for the other 8 ir = fitz.IRect(x01, y01, x02, y02) # rectangle of middle square punch(pm, x00, y00, x01, y01) # top left punch(pm, x01, y00, x02, y01) # top middle punch(pm, x02, y00, x03, y01) # top right punch(pm, x00, y01, x01, y02) # middle left pm.clearWith(0, ir) # clear center to black punch(pm, x02, y01, x03, y02) # middle right punch(pm, x00, y02, x01, y02) # bottom left punch(pm, x01, y02, x02, y03) # bottom middle punch(pm, x02, y02, x03, y03) # bottom right return
@author: Jorj X. McKie =============================================================================== PyMuPDF demo program -------------------- Demonstrates some of MuPDF's non-PDF graphic capabilities. Read an image and create a new one consisting of 3 * 4 tiles of it. =============================================================================== """ assert len(sys.argv) == 2, "Usage: %s <input file>" % sys.argv[0] # create pixmap from file pix0 = fitz.Pixmap(sys.argv[1]) # calculate target pixmap colorspace and dimensions and then create it tar_cs = pix0.colorspace tar_width = pix0.width * 3 tar_height = pix0.height * 4 tar_irect = fitz.IRect(0, 0, tar_width, tar_height) tar_pix = fitz.Pixmap(tar_cs, tar_irect, pix0.alpha) tar_pix.clearWith(90) # clear target with a "very lively stone gray" (Loriot) # now fill target with 3 * 4 tiles of input picture for i in list(range(4)): pix0.y = i * pix0.height # modify input's y coord for j in list(range(3)): pix0.x = j * pix0.width # modify input's x coord tar_pix.copyPixmap(pix0, pix0.irect) # copy input to new loc # save all intermediate images to show what is happening fn = "target-" + str(i) + str(j) + ".png" tar_pix.writePNG(fn)
punch(pm, x00, y01, x01, y02) # middle left pm.clearWith(0, ir) # clear center to black punch(pm, x02, y01, x03, y02) # middle right punch(pm, x00, y02, x01, y02) # bottom left punch(pm, x01, y02, x02, y03) # bottom middle punch(pm, x02, y02, x03, y03) # bottom right return #============================================================================== # main program #============================================================================== d = 3**6 # = 729, picture dimension in pixels # create a quadratic pixmap with origin (0,0), where width = height = d should # be a power of 3 t0 = time.clock() cs = fitz.Colorspace(fitz.CS_RGB) ir = fitz.IRect(0, 0, d, d) pm = fitz.Pixmap(cs, ir) # fill image area with "white" and then optionally tint and gamma it pm.clearWith(255) #pm.tintWith(10, 20, 100) # tint it with some sort of blue #pm.gammaWith(0.5) # lighten it up # now punch holes into it, down to 1 pixel granularity punch(pm, 0, 0, d, d) t1 = time.clock() pm.writePNG("sierpinski.png") t2 = time.clock() print("%f sec to create img" % (t1 - t0)) print("%f sec to save img" % (t2 - t1))
def extra_rest_fig(page, page1, source_rects_1, rest_name_figrect, page_length, page_width, out_path, broker_arg): ''' 提取拼接跨页图片后保存 0:图表分开命名0 图表统一命名1 1:图表名位置 靠左0 居中1 靠右2 2:来源位置 靠左0 居中1 靠右2 3:页边距(右侧) 4:页边距(下侧) 5:页边距(上侧) 6:图表左侧坐标偏移(分情况) 7:一行多个图片时图片间的间距(分情况) 8:图表名字号高度 ''' mat = fitz.Matrix(3, 3) # 保存图片相关信息 result_position = [] width = page.rect.y1 rest_name_figrect = sorted(rest_name_figrect, key=take_x0) if len(rest_name_figrect) > len(source_rects_1): for i in range(len(rest_name_figrect) - len(source_rects_1)): source_rects_1.append( fitz.Rect(0, 0, page_length - broker_arg[3] * 28, page_width - broker_arg[4] * 28)) for i in range(len(rest_name_figrect)): if i < len(rest_name_figrect) - 1: if broker_arg[1] == 0: rect1 = fitz.Rect( rest_name_figrect[i][1].x0 - broker_arg[6] * 28, rest_name_figrect[i][1].y0, rest_name_figrect[i + 1][1].x0 - broker_arg[7] * 28, page_width - broker_arg[4] * 28) rect2 = fitz.Rect( rest_name_figrect[i][1].x0 - broker_arg[6] * 28, broker_arg[5] * 28, rest_name_figrect[i + 1][1].x0 - broker_arg[7] * 28, source_rects_1[i].y1) else: rect1 = fitz.Rect( source_rects_1[i].x0 - broker_arg[6] * 28, rest_name_figrect[i][1].y0, source_rects_1[i + 1].x0 - broker_arg[7] * 28, page_width - broker_arg[4] * 28) rect2 = fitz.Rect( source_rects_1[i].x0 - broker_arg[6] * 28, broker_arg[5] * 28, source_rects_1[i + 1].x0 - broker_arg[7] * 28, source_rects_1[i].y1) pix = page.getPixmap(matrix=mat, clip=rect1, alpha=False) pix1 = page1.getPixmap(matrix=mat, clip=rect2, alpha=False) tar_irect = fitz.IRect(0, 0, pix.width, pix.height + pix1.height) tar_pix = fitz.Pixmap(fitz.csRGB, tar_irect, pix.alpha) tar_pix.clearWith(90) pix.x = 0 pix.y = 0 pix1.x = 0 pix1.y = pix.height tar_pix.copyPixmap(pix, pix.irect) tar_pix.copyPixmap(pix1, pix1.irect) rect1.y1 = rect1.y0 + broker_arg[8] * 28 figname = get_figname(page, rect1) figname = validateTitle(figname) fig_name = figname + ".png" tar_pix.writePNG(os.path.join(out_path, fig_name)) top = width - rect1.y0 result_str = fig_name + ' ||page:' + str( page.number + 1) + ' ||point:(' + str(round( rect1.x0, 2)) + ',' + str(round( rect1.y0, 2)) + ')' + ' ||top: ' + str(round(top, 2)) else: if broker_arg[1] == 0: rect1 = fitz.Rect( rest_name_figrect[i][1].x0 - broker_arg[6] * 28, rest_name_figrect[i][1].y0, page_length - broker_arg[3] * 28, page_width - broker_arg[4] * 28) rect2 = fitz.Rect( rest_name_figrect[i][1].x0 - broker_arg[6] * 28, broker_arg[5] * 28, page_length - broker_arg[3] * 28, source_rects_1[i].y1) else: rect1 = fitz.Rect(source_rects_1[i].x0 - broker_arg[6] * 28, rest_name_figrect[i][1].y0, page_length - broker_arg[3] * 28, page_width - broker_arg[4] * 28) rect2 = fitz.Rect(source_rects_1[i].x0 - broker_arg[6] * 28, broker_arg[5] * 28, page_length - broker_arg[3] * 28, source_rects_1[i].y1) pix = page.getPixmap(matrix=mat, clip=rect1, alpha=False) pix1 = page1.getPixmap(matrix=mat, clip=rect2, alpha=False) tar_irect = fitz.IRect(0, 0, pix.width, pix.height + pix1.height) tar_pix = fitz.Pixmap(fitz.csRGB, tar_irect, pix.alpha) tar_pix.clearWith(90) pix.x = 0 pix.y = 0 pix1.x = 0 pix1.y = pix.height tar_pix.copyPixmap(pix, pix.irect) tar_pix.copyPixmap(pix1, pix1.irect) rect1.y1 = rect1.y0 + broker_arg[8] * 28 figname = get_figname(page, rect1) figname = validateTitle(figname) fig_name = figname + ".png" tar_pix.writePNG(os.path.join(out_path, fig_name)) top = width - rect1.y0 result_str = fig_name + ' ||page:' + str( page.number + 1) + ' ||point:(' + str(round( rect1.x0, 2)) + ',' + str(round( rect1.y0, 2)) + ')' + ' ||top: ' + str(round(top, 2)) result_position.append(result_str + '\n') return result_position
def test_irect(): p1 = fitz.Point(10, 20) p2 = fitz.Point(100, 200) p3 = fitz.Point(150, 250) r = fitz.IRect(10, 20, 100, 200) r_tuple = tuple(r) assert tuple(fitz.IRect(p1, p2)) == r_tuple assert tuple(fitz.IRect(p1, 100, 200)) == r_tuple assert tuple(fitz.IRect(10, 20, p2)) == r_tuple assert tuple(r.include_point(p3)) == (10, 20, 150, 250) r = fitz.IRect(10, 20, 100, 200) assert tuple(r.include_rect((100, 200, 110, 220))) == (10, 20, 110, 220) r = fitz.IRect(10, 20, 100, 200) # include empty rect makes no change assert tuple(r.include_rect((0, 0, 0, 0))) == r_tuple r = fitz.IRect() for i in range(4): r[i] = i + 1 assert r == fitz.IRect(1, 2, 3, 4) failed = False try: r = fitz.IRect(1) except: failed = True assert failed failed = False try: r = fitz.IRect(1, 2, 3, 4, 5) except: failed = True assert failed failed = False try: r = fitz.IRect((1, 2, 3, 4, 5)) except: failed = True assert failed failed = False try: r = fitz.IRect(1, 2, 3, "x") except: failed = True assert failed failed = False try: r = fitz.IRect() r[5] = 1 except: failed = True assert failed
=============================================================================== PyMuPDF demo program - updated to PyMuPDF 1.9 --------------------------------------------- Demonstrates some of MuPDF's non-PDF graphic capabilities. Read an image and create a new one consisting of 3 * 4 tiles of it. =============================================================================== ''' # create a pixel map from any supported image file: BMP, JPEG, PNG, GIF, TIFF, JXR pix0 = fitz.Pixmap("supported_img.xxx") # create a pixel map from file # calculate target pixmap colorspace and dimensions, then create it tar_csp = pix0.getColorspace() # copy input's colorspace tar_width = pix0.width * 3 # 3 columns tar_height = pix0.height * 4 # 4 rows tar_irect = fitz.IRect(0, 0, tar_width, tar_height) # we need to define a target rectangle tar_pix = fitz.Pixmap(tar_csp, tar_irect) # now create target pixel map tar_pix.clearWith( 90) # clear pixmap with a lively gray: (R, G, B) = (90, 90, 90) # now fill target with 3 * 4 tiles of input picture for i in list(range(4)): pix0.y = i * pix0.height # modify input's y coord for j in list(range(3)): pix0.x = j * pix0.width # modify input's x coord tar_pix.copyPixmap(pix0, pix0.getIRect()) # copy input to new loc # save intermediate images too, to display what is happening fn = "target-" + str(i) + str(j) + ".png" tar_pix.writePNG(fn)