Transparent PNG in PIL turns out not to be transparent

I think what you want to use is the paste mask argument. see the docs, (scroll down to paste) from PIL import Image img = Image.open(basefile) layer = Image.open(layerfile) # this file is the transparent one print layer.mode # RGBA img.paste(layer, (xoff, yoff), mask=layer) # the transparancy layer will be used as the mask img.save(outfile)

converting tiff to jpeg in python

I have successfully solved the issue. I posted the code to read the tiff files in a folder and convert into jpeg automatically. import os from PIL import Image yourpath = os.getcwd() for root, dirs, files in os.walk(yourpath, topdown=False): for name in files: print(os.path.join(root, name)) if os.path.splitext(os.path.join(root, name))[1].lower() == “.tiff”: if os.path.isfile(os.path.splitext(os.path.join(root, name))[0] + “.jpg”): … Read more

Saving an imshow-like image while preserving resolution

As you already guessed there is no need to create a figure. You basically need three steps. Normalize your data, apply the colormap, save the image. matplotlib provides all the necessary functionality: import numpy as np import matplotlib.pyplot as plt # some data (512×512) import scipy.misc data = scipy.misc.lena() # a colormap and a normalization … Read more

Variable scope and Try Catch in python

What’s wrong with the “else” clause ? for filename in files: try: im = Image.open(os.path.join(dirname,filename)) except IOError, e: print “error opening file :: %s : %s” % (os.path.join(dirname,filename), e) else: print im.size Now since you’re in a loop, you can also use a “continue” statement: for filename in files: try: im = Image.open(os.path.join(dirname,filename)) except IOError, … Read more

How to read a raw image using PIL?

The specific documentation is at http://effbot.org/imagingbook/concepts.htm: Mode The mode of an image defines the type and depth of a pixel in the image. The current release supports the following standard modes: 1 (1-bit pixels, black and white, stored with one pixel per byte) L (8-bit pixels, black and white) P (8-bit pixels, mapped to any … Read more

Changing image hue with Python PIL

There is Python code to convert RGB to HSV (and vice versa) in the colorsys module in the standard library. My first attempt used rgb_to_hsv=np.vectorize(colorsys.rgb_to_hsv) hsv_to_rgb=np.vectorize(colorsys.hsv_to_rgb) to vectorize those functions. Unfortunately, using np.vectorize results in rather slow code. I was able to obtain roughly a 5 times speed up by translating colorsys.rgb_to_hsv and colorsys.hsv_to_rgb into … Read more

How to convert Matplotlib figure to PIL Image object (without saving image)

EDIT # 2 PIL.Image.frombytes(‘RGB’, fig.canvas.get_width_height(),fig.canvas.tostring_rgb()) takes around 2ms compared to the 35/40ms of the below. This is the fastest way I can find so far. I’ve been looking at this also today. In the matplotlib docs the savefig function had this. pil_kwargsdict, optional Additional keyword arguments that are passed to PIL.Image.save when saving the figure. … Read more

Remove transparency/alpha from any image using PIL

This can be done by checking if the image is transparent def remove_transparency(im, bg_colour=(255, 255, 255)): # Only process if image has transparency (http://stackoverflow.com/a/1963146) if im.mode in (‘RGBA’, ‘LA’) or (im.mode == ‘P’ and ‘transparency’ in im.info): # Need to convert to RGBA if LA format due to a bug in PIL (http://stackoverflow.com/a/1963146) alpha = … Read more

Hata!: SQLSTATE[HY000] [1045] Access denied for user 'divattrend_liink'@'localhost' (using password: YES)