title: calculate a grid layout in python for scribus
body: <p>I want to create a grid layout in <a href="https://www.scribus.net" rel="nofollow noreferrer">scribus</a> to create a photobook. The grid layout should take the image sizes in account and place the images accordingly. Like the pinterest layout / <a href="https://masonry.desandro.com/" rel="nofollow noreferrer">masonry-layout</a>. For example as shown here (taken from the startpage of <a href="https://masonry.desandro.com/" rel="nofollow noreferrer">masonry.desandro.com</a>)</p>
  
  <p><a href="https://i.stack.imgur.com/0CpBE.jpg" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/0CpBE.jpg" alt="Masonry layout take from masonry.desandro.com"></a></p>
  
  <p>Currently I try to find some information where I can start to dig in, so that I can build up such a programatically grid. But it's hard to find any good tutorial or explantion how to create such a calculation or I do not find the right keywords for my search. </p>
  
  <p>In other words, I want to develop a script where I calculate x and y positions on a single page, which takes the image ratio in account and create a nice looking page. The image dimensions/ratio will be calculated with the information from <a href="https://pypi.python.org/pypi/Pillow" rel="nofollow noreferrer">pillow</a>.</p>
  
  <p>Any hints and tips where I get more information to dig into this topic is appreciated. </p>
  
  <p>My current approach is not really usefull at the moment</p>
  
  <pre><code>#!/usr/bin/env python
  # run this script with 
  # /Applications/Scribus.app/Contents/MacOS/Scribus -ns -py foobar.py
  
  from scribus import *
  from PIL import Image # Info: Scribus is build with python 2.7
  
  # DIN A4 3508 x 2480 px = 29,7 x 21,0 cm; Ratio 3508/2480 = 1,41 = Portrait
  # Ratio &gt; 1 = Landscape 
  # Ratio &lt; 1 = Portrait 
  
  # build a manuel grid for testing
  # x,y,width, height 
  pic1 = ( 15, 20, 270, 360 )
  pic2 = ( 300, 20, 270, 360 )
  pic3 = ( 15, 430, 270, 360 )
  pic4 = ( 300, 430, 270, 360 )
  
  layout = []
  layout.append( pic1 )
  layout.append( pic2 )
  layout.append( pic3 )
  layout.append( pic4 )
  
  def doFrame( frame_layout ): 
      x,y,w,h = frame_layout
      createImage( x,y,w,h )
  
  if newDocument(PAPER_A4, (10,10,10,10),PORTRAIT, 1, UNIT_POINTS, NOFACINGPAGES, FIRSTPAGERIGHT, 1):
      for frame in layout: 
          doFrame( frame )
  
      setRedraw(1)
      redrawAll()
  
  else:
      result = messageBox ('Something is wrong',BUTTON_OK)
  </code></pre>