Welcome, guest | Sign In | My Account | Store | Cart
#!/usr/bin/python
# -*- coding: utf-8 -*-
import fitz                    # this is PyMuPDF

'''
Created on Fri Jan 08 17:00:00 2016

@author: Jorj X. McKie

===============================================================================
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)

Diff to Previous Revision

--- revision 3 2016-03-19 12:40:17
+++ revision 4 2016-04-26 19:54:31
@@ -8,22 +8,22 @@
 @author: Jorj X. McKie
 
 ===============================================================================
-PyMuPDF demo program
---------------------
+PyMuPDF demo program - updated to PyMuPDF 1.9
+---------------------------------------------
 Demonstrates some of MuPDF's non-PDF graphic capabilities.
 
-Read a PNG image and create a new one consisting of 3 * 4 tiles of it.
+Read an image and create a new one consisting of 3 * 4 tiles of it.
 ===============================================================================
 '''
-# read in picture image as a pixmap
-pic = open("editra.png", "rb").read()            # read some RGB image into memory
-pix0 = fitz.Pixmap(pic, len(pic))                # create a pixel map out 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 dimensions and then create it
-tar_width  = pix0.width * 3                      
-tar_height = pix0.height * 4
+# 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(fitz.csRGB, tar_irect)       # now create target RGB pixel map
+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
@@ -32,6 +32,6 @@
     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 to show what is happening
+        # save intermediate images too, to display what is happening
         fn = "target-" + str(i) + str(j) + ".png"
         tar_pix.writePNG(fn)

History