I have a source file image.ico of any size and want to create a thumbnail. This is the code i am using right now:
converted_file = cStringIO.StringIO()
thumb = ImageOps.fit(image, (width, height), Image.ANTIALIAS)
thumb.save(converted_file, format='png')
I chose png as extension because PIL does not support ico files which could be the culprit. It works beside the fact that transparency is not applied. Parts with alpha=0 are rendered black instead of being transparent. How can i fix this behavior?
/edit
I also tried (see this answer):
converted_file = cStringIO.StringIO()
thumb = ImageOps.fit(image, (width, height), Image.ANTIALIAS)
background = Image.new('RGBA', (width, height), (255, 255, 255, 0))
background.paste(thumb, box = (0, 0, width, height))
background.save(converted_file, format='png')
Same effect.
The Problem is indeed that PIL does not know how to exactly read ICO files. There are two possibilities how to fix this:
Add a plugin to PIL that registers the ICO format
Use Pillow, a fork of PIL that is more frequently updated
I chose to use Pillow which is also Python 3 compatible and has some more goodies.
1. PIL Plugin
Save the Win32IconImagePlugin somewhere in your project. After importing the PIL Image class, import the plugin to register ICO support:
from PIL import Image
import Win32IconImagePlugin
There you go, now you can use the right format:
thumb.save(converted_file, format='ico')
2. Pillow
Pillow has builtin support for ICO images.
Just remove pil and install pillow:
pip uninstall pil
pip install pillow
Be sure to change all global pil imports:
import Image, ImageOps
to
from PIL import Image, ImageOps
There you go, now you can use the right format:
thumb.save(converted_file, format='ico')
Related
I am trying to add some text on my image using PIL, see the code below,
from PIL import Image
from PIL import ImageFont
from PIL import ImageDraw
import sys
image = Image.open('image.png')
draw = ImageDraw.Draw(image)
font = ImageFont.truetype('arial',40)
draw.text((700, 470),'Text',(0,0,0),font=font)
img.save('out-image.png','PNG')
But I lost the original colors of the image, see below images,
Original Image
After adding text
How I can preserve the original colors.
Thank You
That looks like a bug in PIL to me. I think it is because your image is palettised and the draw.text() is messing up the palette.
For a work-around, you can convert to an RGB image when you open it to avoid palette issues. Change to this:
image = Image.open('image.png').convert('RGB')
I need convert/load jpg2000 images (24 BIT RGBI True Color) to png/jpg for further usage and struggled quite a bit to get them loaded at all on windows. Succeeded with a pgmagic installation on windows, but the RGBI images come out as grayscale.
Output image generated with:
from pgmagick import Image
img = Image("path")
img.write('output.jpg')
What am I missing here?
Found the answer myself:
I had to use osgeo:
from osgeo import gdal
from PIL import Image
dataset = gdal.open("path", gdal.GA_ReadOnly)
img = dataset.ReadAsArray()
img_trnsp = img.transpose()
final_image = Image.fromarray(img_trnsp)
I have a bunch of images I need to put a text-overlay on top of. I created the overlay with GIMP (PNG with transparency) and tried pasting it on top of the other image:
from PIL import Image
background = Image.open("hahn_echo_1.png")
foreground = Image.open("overlay_step_3.png")
background.paste(foreground, (0, 0), foreground)
background.save("abc.png")
However, instead of displaying a nice black text on top, I get this:
overlay.png looks like this in Gimp:
So I would expect some nice and black text instead of this colorful mess.
Any ideas? Some PIL option I am missing?
As vrs pointed out above, using alpha_composite like this answer: How to merge a transparent png image with another image using PIL
does the trick. Make sure to have the images in the correct mode (RGBA).
Complete solution:
from PIL import Image
background = Image.open("hahn_echo_1.png").convert("RGBA")
foreground = Image.open("overlay_step_3.png").convert("RGBA")
print(background.mode)
print(foreground.mode)
Image.alpha_composite(background, foreground).save("abc.png")
Result:
I'm currently making a program that renders information to a buffer, and I want to save the information as an image file of some sort to my working directory. I've seen some examples using PIL, but that library isn't supported for python 3.x. Are there better alternatives?
First uninstall PIL than install Pillow
its a PIL's clone which works on python 3.x.
from PIL import Image
img = Image.open("test1.jpg") #jpg, png, etc.
pix = img.load()
print img.size #Get the width and height of the image for iterating over
print pix[15,15] #Get the RGBA Value of the a pixel of an image
pix[15, 15] = value # Set the RGBA Value of the image (tuple)
img.save("out.jpg") # Saves the modified pixels to image
I have just done some image processing using the python image library (PIL) and I can't get the save function to work. the whole code works fine but it just wont save the resulting image. The code is below:
im=Image.new("rgb",(200,10),"#ddd")
draw=Image.draw.draw(im)
draw.text((10,10),"run away",fill="red")
im.save("g.jpeg")
Saving gives an error as unknown extension and even removing the dot doesn't help.
Use .jpg:
im.save("g.jpg")
The image library determines what encoder to use by the extension, but in certain versions of PIL the JPEG encoder do not register the .jpeg extension, only .jpg.
Another possibility is that your PIL installation doesn't support JPEG at all; try saving the image as a PNG, for example.
Replace
draw=Image.draw.draw(im)
with
draw = ImageDraw.Draw(im)
and make sure the height of the new image is tall enough to accomodate the text.
import Image
import ImageDraw
im = Image.new("RGB", (200, 30), "#ddd")
draw = ImageDraw.Draw(im)
draw.text((10, 10), "run away", fill="red")
im.save("g.jpeg")
yields
please save with .jpg extention eg:
im.save("g.jpg")