Resize an image with PythonMagick

Updated Gribouillis 4 Tallied Votes 3K Views Share

A simple way to resize an image programmatically, using the PythonMagick module. This module is a python port of the the magick++ library.

# AUTHOR: Gribouillis for the python forum at www.daniweb.com
import PythonMagick as Magick

in_name = 'scilab01.png'
out_name = 'scilab01_small.png'

img = Magick.Image(in_name)
geometry = img.size()
w, h = geometry.width(), geometry.height()
new_width = 800
factor = new_width/float(w)
new_height = int(h * factor)
img.resize("{}x{}".format(new_width, new_height))
img.write(out_name)
arronlee 0 Newbie Poster

Is this work on C sharp? I have only tried to customize the size of images <URL SNIPPED> using the code below:

using System.IO;
using System.Drawing.Printing;
using Yiigo.Imaging;
using Yiigo.Imaging.Processing;

using YiigoImage Image = new YiigoImage();
{
string ImageID = (@"C:\ yiigo_example.jpg ");
float ImageWidth = "300";
float ImageHeight = "500";
}
Image.YiigoImageProcessResize(@"C:\ yiigo_example.jpg", "300", "500");
Image.Save(@"C:\ yiigo_example.jpg");

Is there any difference between them?

Gribouillis 1,391 Programming Explorer Team Colleague

@arronlee: same remark as here . Avoid commercial links.

Nils_1 0 Newbie Poster

Dont like that there is no source code comment pointing out where to get it and that there is no info if it supports py3. Cant use it, downvote therefore.

Gribouillis 1,391 Programming Explorer Team Colleague

Dont like that there is no source code comment pointing out where to get it and that there is no info if it supports py3. Cant use it, downvote therefore.

I did not write this with python 3 in mind. For windows you can download pythonmagick binaries for python 3 in Christoph Gohlke's site.

I was not able to install pythonmagick for python 3 in linux mint (as of November 24th 2013). You can use other python wrappers around ImageMagick however. For example I tried a ctypes wrapper magickpy, installed using pip3 (package python3-pip in linux mint). Here is the code

import magickpy as Magick

in_name = 'scilab02.png'
out_name = 'scilab02_small.png'

img = Magick.Image.read(in_name)
w, h = img.width, img.height
new_width = 800
factor = new_width/float(w)
new_height = int(h * factor)
info = Magick.ExceptionInfo()
filter = Magick.FilterTypes.BesselFilter
blur = 1.0
img2 = img.makeResize(new_width, new_height, filter, blur, info)
img2.write(out_name)

Aso check this page for other wrappers.

Nils_1 0 Newbie Poster

Thanks, Gribouillis, my downvote was a bit harsh, have undone it :-)

Gribouillis 1,391 Programming Explorer Team Colleague

Here is similar code for another ctypes binding to the imagemagick wand API.

from wand.image import Image

in_name = 'scilab02.png'
out_name = 'scilab02_small.png'

with Image(filename = in_name) as image:
    w, h = image.size
    new_width = 800
    factor = new_width/float(w)
    new_height = int(h * factor)
    image.resize(new_width, new_height, 'lanczos', 1.0)
    image.save(filename = out_name)

In mint, install module wand with pip3 for python 3 or with package python-wand for python 2.

Nils_1 0 Newbie Poster

Thanks also for the link to the gohlke site

Gribouillis 1,391 Programming Explorer Team Colleague

You're welcome.

codehimn 14 Newbie Poster
import PythonMagick

img = PythonMagick.Image("elixir.png")
img.resize("20%")
img.write('out_name.JPG')

# this works for me :)
Gribouillis commented: Excellent! Conversion included! +14
Gribouillis 1,391 Programming Explorer Team Colleague

Not python, but related to the topic, the converseen GUI program converts images manually with imagemagick.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.