python imaging library save function - python

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")

Related

How can I get image from this python code?

from PIL import Image
new_im = Image.new('RGB', (100,200), (255,255,255))
new_im.save("MonImage.png", "PNG")
i use this code in visual studio to create an image but i cant show it
In addition to saving the file to disk (via new_im.save) you can also display the image with
new_im.show()
I think this will help you (your picture will be opened in a new window)
new_im.show()

Convert mp3 song image from png to jpg

I have a set of many songs, some of which have png images in metadata, and I need to convert these to jpg.
I know how to convert png images to jpg in general, but I am currently accessing metadata using eyed3, which returns ImageFrame objects, and I don't know how to manipulate these. I can, for instance, access the image type with
print(img.mime_type)
which returns
image/png
but I don't know how to progress from here. Very naively I tried loading the image with OpenCV, but it is either not a compatible format or I didn't do it properly. And anyway I wouldn't know how to update the old image with the new one either!
Note: While I am currently working with eyed3, it is perfectly fine if I can solve this any other way.
I was finally able to solve this, although in a not very elegant way.
The first step is to load the image. For some reason I could not make this work with eyed3, but TinyTag does the job:
from PIL import Image
from tinytag import TinyTag
tag = TinyTag.get(mp3_path, image=True)
image_data = tag.get_image()
img_bites = io.BytesIO(image_data)
photo = Image.open(im)
Then I manipulate it. For example we may resize it and save it as jpg. Because we are using Pillow (PIL) for these operations, we actually need to save the image and finally load it back to get the binary data (this detail is probably what should be improved in the process).
photo = photo.resize((500, 500)) # suppose we want 500 x 500 pixels
rgb_photo = photo.convert("RGB")
rgb_photo.save(temp_file_path, format="JPEG")
The last step is thus to load the image and set it as metadata. You have more details about this step in this answer.:
audio_file = eyed3.load(mp3_path) # this has been loaded before
audio_file.tag.images.set(
3, open(temp_file_path, "rb").read(), "image/jpeg"
)
audio_file.tag.save()

Converting multipage PDF to TIFF does not work with Python library Wand

Given the short, 5 page PDF file (attached at the bottom), and the following python code to convert to a multi-page TIFF:
from wand.image import Image
with Image(filename='5-page-pdf.pdf', resolution=200) as img:
img.type = "grayscale"
img.format = "tiff"
img.compression = "lzw"
img.save(filename="test.tiff")
results in a TIFF file that has pages 2-4 as what appears to be black text on a dark-grey (or maybe transparent) background. Other image processing libraries cannot open the file or render it.
Converting the same PDF with ImageMagick, which Wand uses, works just fine
convert -density 200 5-page-pdf.pdf -type grayscale -compress lzw 5-page-pdf.tiff
this produces a file that does work with other imaging libraries and looks correct in a TIFF viewer.
I've tried removing the alpha channel, I've tried setting the background color to 'White', and a few other things, to no avail. The TIFF that comes out of Wand is always garbled. If it's doable in ImageMagick it should be doable in Wand, right? What parameter or setting am I missing?
Original PDF
Wand Produced TIFF
Looks like setting the img.alpha_channel property is not propagating across the pages.
Try this workaround
from wand.api import library
from wand.image import Image
with Image(filename="5-page-pdf.pdf", resolution=200) as img:
img.type = 'grayscale'
img.compression = "lzw"
# Manually iterate over all page, and turn off alpha channel.
library.MagickResetIterator(img.wand)
for idx in range(library.MagickGetNumberImages(img.wand)):
library.MagickSetIteratorIndex(img.wand, idx)
img.alpha_channel = 'off'
img.save(filename="test.tiff")

Create, Modify, and Save an image in python 3.x

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

Transparent PNG in PIL turns out not to be transparent

I have been hitting my head against the wall for a while with this, so maybe someone out there can help.
I'm using PIL to open a PNG with transparent background and some random black scribbles, and trying to put it on top of another PNG (with no transparency), then save it to a third file.
It comes out all black at the end, which is irritating, because I didn't tell it to be black.
I've tested this with multiple proposed fixes from other posts. The image opens in RGBA format, and it's still messed up.
Also, this program is supposed to deal with all sorts of file formats, which is why I'm using PIL. Ironic that the first format I tried is all screwy.
Any help would be appreciated. Here's the code:
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)) # xoff and yoff are 0 in my tests
img.save(outfile)
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)

Categories

Resources