python


Fill an image with pre-defined pattern with Wand


I have an image (like that: mask) and two integers, which represents a final image width & height. According to Wand's documentation Open empty image:
with Image(width=200, height=100) as img:
img.save(filename='200x100-transparent.png')
It will result in an empty image with transparent background.
Now, the question is: How to create a same empty image, but with mask image as background pattern?
The composite CLI command itself has a following operator:
-tile repeat composite operation across and down image
But how to achieve the same with Wand?
Well, after looking on ImageMagick's Composite source code itself, it became clear, that the Wand-driven solution should look like:
with Image(width=x, height=y) as img:
for x in xrange(0, img.width, crop_mask_path.width):
for y in xrange(0, img.height, crop_mask_path.height):
img.composite_channel('default_channels', crop_mask_path, 'over', x, y)
img.save(filename='patterned_image.png')
Building out the title iterator is the best solution in my opinion. However another hackish method would be to invoke the tile: protocol, and allow the internal ImageMagick methods to handle composites. You'll lose the control inherited by DIY, but gain some performance on optimized IM systems.
from wand.image import Image
from wand.api import library
with Image() as img:
# Same as `-size 400x400' needed by tile: protocol.
library.MagickSetOption(img.wand, 'size', '400x400')
# Prefix filename with `tile:' protocol.
img.read(filename='tile:rose.png')
img.save(filename='tile_rose.png')

Related Links

How to check if a date time is before midday
Merge rows based on values and key string in awk, sed or python
i find the files at the end empty ,python
How do I prevent my FOR loop from ending too early?
PyInstaller 2.1 import custom package
Python: Read File and append a lot of list of 28 lines
Numba Matrix Vector multiplication
How to improve very inefficient numpy code for calculating correlation
Flask-strange routing issue
Upgrade path for re-usable apps with South AND django 1.7 migrations
Making scikit-learn train on all training data after cross-validation
matplotlib does not display latex fonts correctly
How to use py2exe with third-party libraries
How to extract two numbers from input line and tab delimit them?
Copy selected lines of text from one file to another with command line argument with or without spaces
TypeError: can't use a string pattern on a bytes-like object

Categories

HOME
jsf
project-management
freepascal
jasmine
qpython3
thunderbird-addon
hana
apache2
rocketmq
orientation
laravel-4
hyperledger-fabric
user
nodemailer
pca
xul
public-key-encryption
postmessage
typeahead
spring-cloud-config
phpbb
riak-ts
apache-karaf
ip-camera
tortoisegit
header-files
jflex
python-textprocessing
geopandas
skmaps
kong
excel-2013
unpack
pcre
configure
jboss5.x
apache-cloudstack
smart-mobile-studio
precedence
remote-server
text-files
ksoap
vsts-package-management
deepstream.io
restful-url
nashorn
lftp
powermta
number-theory
gauss
istorage
dwarf
ticker
quintus
unity3d-editor
software-product-lines
windows-nt
gradle-eclipse
supportmapfragment
anti-cheat
mfc-feature-pack
web-essentials
except
sem
abas
stackframe
hiera
robocode
ctrlp
jquery-autocomplete
scala-2.11
sortable
suffix-array
sid
proxies
httpcontext
sanitization
facebook-sdk-3.1
goinstant
haskell-platform
pep8
usn
symfony-2.0
plone-funnelweb
mpmovieplayer
hinstance
emacs23
querystringparameter
throttling
hardware-acceleration
quick-search
pci-bus
code-design
lazy-c++
interface-design

Resources

Mobile Apps Dev
Database Users
javascript
java
csharp
php
android
MS Developer
developer works
python
ios
c
html
jquery
RDBMS discuss
Cloud Virtualization
Database Dev&Adm
javascript
java
csharp
php
python
android
jquery
ruby
ios
html
Mobile App
Mobile App
Mobile App