Python PIL Paste - python

I want to paste a bunch of images together with PIL. For some reason, when I run the line blank.paste(img,(i*128,j*128)) I get the following error: ValueError: cannot determine region size; use 4-item box
I tried messing with it and using a tuple with 4 elements like it said (ex. (128,128,128,128)) but it gives me this error: SystemError: new style getargs format but argument is not a tuple
Each image is 128x and has a naming style of "x_y.png" where x and y are from 0 to 39. My code is below.
from PIL import Image
loc = 'top right/'
blank = Image.new("RGB", (6000,6000), "white")
for x in range(40):
for y in reversed(range(40)):
file = str(x)+'_'+str(y)+'.png'
img = open(loc+file)
blank.paste(img,(x*128,y*128))
blank.save('top right.png')
How can I get this to work?

This worked for me, I'm using Odoo v9 and I have pillow 4.0.
I did it steps in my server with ubuntu:
# pip uninstall pillow
# pip install Pillow==3.4.2
# /etc/init.d/odoo restart

You're not loading the image correctly. The built-in function open just opens a new file descriptor. To load an image with PIL, use Image.open instead:
from PIL import Image
im = Image.open("bride.jpg") # open the file and "load" the image in one statement
If you have a reason to use the built-in open, then do something like this:
fin = open("bride.jpg") # open the file
img = Image.open(fin) # "load" the image from the opened file
With PIL, "loading" an image means reading the image header. PIL is lazy, so it doesn't load the actual image data until it needs to.
Also, consider using os.path.join instead of string concatenation.

For me the methods above didn't work.
After checking image.py I found that image.paste(color) needs one more argument like image.paste(color, mask=original). It worked well for me by changing it to this:
image.paste(color, box=(0, 0) + original.size)

Related

Is there a way of attaching an image on a python code in such a way that it becomes part of the soure code?

I'm a beginner in python and I'm trying to send someone my small python program together with a picture that'll display when the code is run.
I tried to first convert the image to a binary file thinking that I'd be able to paste it in the source code but I'm not sure if that's even possible as I failed to successfully do it.
You can base64-encode your JPEG/PNG image which will make it into a regular (non-binary string) like this:
base64 -w0 IMAGE.JPG
Then you want to get the result into a Python variable, so repeat the command but copy the output to your clipboard:
base64 -w0 IMAGE.JPG | xclip -selection clipboard # Linux
base64 -w0 IMAGE.JPG | pbcopy # macOS
Now start Python and make a variable called img and paste the clipboard into it:
img = 'PASTE'
It will look like this:
img = '/9j/4AAQSk...' # if your image was JPEG
img = 'iVBORw0KGg...' # if your image was PNG
Now do some imports:
from PIL import Image
import base64
import io
# Make PIL Image from base64 string
pilImage = Image.open(io.BytesIO(base64.b64decode(img)))
Now you can do what you like with your image:
# Print its description and size
print(pilImage)
<PIL.JpegImagePlugin.JpegImageFile image mode=RGB size=200x100>
# Save it to local disk
pilImage.save('result.jpg')
You can save a picture in byte format inside a variable in your program. You can then convert the bytes back into a file-like object using the BytesIO function of the io module and plot that object using the Image module from the Pillow library.
import io
import PIL.Image
with open("filename.png", "rb") as file:
img_binary = file.read()
img = PIL.Image.open(io.BytesIO(img_binary))
img.show()
To save the binary data inside your program without having to read from the source file you need to encode it with something like base64, use print() and then simply copy the output into a new variable and remove the file reading operation from your code.
That would look like this:
img_encoded = base64.encodebytes(img_binary)
print(img_binary)
img_encoded = " " # paste the output from the console into the variable
the output will be very long, especially if you are using a big image. I only used a very small png for testing.
This is how the program should look like at the end:
import io
import base64
import PIL.Image
# with open("filename.png", "rb") as file:
# img_binary = file.read()
# img_encoded = base64.encodebytes(img_binary)
img_encoded = b'iVBORw0KGgoAAAANSUhEUgAAADAAAAAwCAYAAABX[...]'
img = PIL.Image.open(io.BytesIO(base64.decodebytes(img_encoded)))
img.show()
You could perhaps have your Python program download the image from a site where you upload files such as Google Drive, Mega, or Imgur. That way, you can always access and view the image easily without the need of running the program or for example converting the binary back into the image in the method you mentioned.
Otherwise, you could always store the image as bytes in a variable and have your program read this variable. I'm assuming that you really wish to do it this way as it would be easier to distribute as there is only one file that needs to be downloaded and run.
Or you could take a look at pyinstaller which is made for python programs to be easily distributed across machines without the need to install Python by packaging it as an executable (.exe) file! That way you can include the image file together by embedding it into the program. There are plenty of tutorials for pyinstaller you could google up. Note: Include the '--onefile' in your parameters when running pyinstaller as this will package the executable into a single file that the person you're sending it to can easily open whoever it may be-- granted the executable file can run on the user's operating system. :)

Opening an image with its original file name on display using Python?

I have a chart function that saves the end figure as a file. After I run the function, I also want it to display the figure at the end. So, I use this:
from PIL import Image
filepath = 'image.png'
img = Image.open(filepath)
img.show()
It works just fine, but when the file opens, it opens with a random file name, not the actual file name.
This can get troublesome as I have a lot of different chart functions that work in a similar fashion, so having logical names is a plus.
Is there a way I can open an image file with Python and have it display it's original file name?
EDIT
I'm using Windows, btw.
EDIT2
Updated the example with code that shows the same behaviour.
Instead of PIL you could use this:-
import os
filepath = "path"
os.startfile(filepath)
Using this method will open the file using system editor.
Or with PIL,
import Tkinter as tk
from PIL import Image, ImageTk # Place this at the end (to avoid any conflicts/errors)
window = tk.Tk()
#window.geometry("500x500") # (optional)
imagefile = {path_to_your_image_file}
img = ImageTk.PhotoImage(Image.open(imagefile))
lbl = tk.Label(window, image = img).pack()
window.mainloop()
The function img.show() opens a Windows utility to display the image. The image is first written to a temporary file before it is displayed. Here is the section from the PIL docs.
https://pillow.readthedocs.io/en/stable/reference/Image.html#PIL.Image.Image.show
Image.show(title=None, command=None)[source] Displays this image. This
method is mainly intended for debugging purposes.
This method calls PIL.ImageShow.show() internally. You can use
PIL.ImageShow.register() to override its default behaviour.
The image is first saved to a temporary file. By default, it will be
in PNG format.
On Unix, the image is then opened using the display, eog or xv
utility, depending on which one can be found.
On macOS, the image is opened with the native Preview application.
On Windows, the image is opened with the standard PNG display utility.
Parameters title – Optional title to use for the image window, where
possible.
"
The issue is that PIL uses a quick-and-dirty method for showing your image, and it's not intended for serious application use.

Appending images using python and imagemagick

Most people recommend "wand" when it comes to imagemagick for python but how can I append images using it in python ? I want to add lables to bottom of images using imagemagick in python : http://www.imagemagick.org/Usage/annotating/ but wand api seems to be very basic and doesn't have lots of imgemagick commands including labels and appending.
Is there any other way to use imagemagick in python ? my images are png type and are BytesIO streams inside the python code not files so I can not pass them to imagemagick using command line and can't save them on any temporary file either.
I'm not really sure what your asking for, but I'm guessing you want to write a label "below" an image. Here's an example with the wand library.
from wand.image import Image
from wand.compat import nested
from wand.color import Color
from wand.font import Font
with nested(Image(filename='logo:'),
Image(filename='null:')) as (source, text):
text.font = Font('Impact', 64)
text.read(filename='label:Hello world!')
largest_width = max(source.width, text.width)
offset = (largest_width - min(source.width, text.width)) / 2
with Image(width=largest_width,
height=source.height + text.height,
background=Color('WHITE')) as dst:
dst.composite(source, 0, 0)
dst.composite(text, int(offset), source.height)
dst.save(filename="output.png")
Overview
with nested(Image(filename='logo:'),
Image(filename='null:')) as (source, text):
Create two images. You would be responsible for replacing logo: image with your ByteIO buffer. The null: image is a placeholder for allocating a wand instance.
text.font = Font('Impact', 64)
text.read(filename='label:Hello world!')
This defines the typeface & text to draw. The label: protocol can be replaced with caption: for additional behavior(s?).
with Image(width=largest_width,
height=source.height + text.height,
background=Color('WHITE')) as dst:
Create a third "blank" image that is large enough to include both images.
dst.composite(source, 0, 0)
dst.composite(text, int(offset), source.height)
Copy the image data from the source & text to the new image.
Imagemagick is great but has steep learning curve. You need to have 3rd party ImageMagick installed (Would have to be 32 or 64 bit depending on your version of python). Installation alone is a pain considering you need to add it to path and the executable is necessary to run your script.
Consider something more portable and contained like Pillow which has number of features for achieving your goal.
pip install pillow
In pillow you can read images from BytesIO. And also draw or print on them. There is number of options available.
from PIL import Image, ImageDraw
import io
# Reading bytes from file but you can skip that and simply open your bytes like below.
with open('picture.jpg', 'rb') as f:
im = Image.open(io.BytesIO(f.read()))
draw = ImageDraw.Draw(im)
draw.text((10,10), "Hello World", fill=(255))
im.show() # do whatever you like with the image
Check the docs for more examples. https://pillow.readthedocs.io/en/latest/

Reading image using Pillow fails in Jupyter notebook

I'm trying to read a jpg file using Pillow (Version 3.2.0) in Jupyter notebook (Python 3.4), but it fails with the following error:
OSError: broken data stream when reading image file
I'm using the following code:
from PIL import Image
im = Image.open("/path/to/image.jpeg")
im.show()
It works fine both in the interactive Python shell and using Python 2.7 instead of 3.4.
I've followed these steps already: Using Pillow with Python 3
Anyone an idea what's going on?
Looks like you're not pointing to the directory where your photo is stored.
import os
defaultWd = os.getcwd()
defaultWd # Sets your curretn wd
os.chdir(defaultWd + '\\Desktop') # Points to your photo--e.g., on Desktop
os.getcwd() # Shows change in wd
from PIL import Image
im = Image.open("Mew.jpg")
im.show() # Will plot to your default image viewing software
And another way if you don't want to change current wd:
im = Image.open(os.getcwd() + "\\Desktop\\Mew.jpg")
im.show()
And if you want to plot inline:
from matplotlib.pyplot import imshow
%matplotlib inline
inlinePic = Image.open(os.getcwd() + "\\Desktop\\Mew.jpg")
imshow(inlinePic)
Note: You may also want to simply try typing 'jpg' instead of 'jpeg' as you did above, if your image is in your current working directory. Also, if PIC is not installed, you'll get this error NameError: name 'Image' is not defined.
The problem was related to another import: I was importing Tensorflow before PIL, which caused the problem. Same issue as this one: https://github.com/scikit-image/scikit-image/issues/2000. Changing the order of the imports solved it.

Getting error - 'could not find a writer'while giving imshow, imwrite command opencv

I am a beginner at opencv and python.
I have just installed opencv2.4.9 and enthought canopy-32bit. I am getting error for the following:
import cv2
image = cv2.imread('Lena.jpg')
cv2.imwrite('Mypic',image)
This is what I get:
c:\users\nam\appdata\local\temp\tmpokspbt.py in <module>()
3
4 image = cv2.imread('Lena.jpg')
----> 5 cv2.imwrite('Mypic',image)
error: ..\..\..\..\opencv\modules\highgui\src\loadsave.cpp:275: error: (-2) could not find a writer for the specified extension in function cv::imwrite_
you need to give an extension to imwrite(), so it knows, how to save(compress) it.
cv2.imwrite('Mypic.png',image)
# jpg,bmp,png,ppm,pgm,tiff supported 'out-of-the-box,
# webp,jp2 depending on if you compiled in the resp. 3rd party support
# no gif or tga.
You need to make sure you have the image type within the string you give to the imwrite().
imwrite() dose not have a default method to save, thus it is required within the name you give to it.
instead of :
cv2.imwrite('Mypic',image)
you need to write :
cv2.imwrite('Mypic.The_format_you_want_to_save',image)
As an example:
cv2.imwrite('Mypic.jpg',image)
Add an extension for the output file like .jpg, .png, etc based on the application.
For example in this case you could use,
import cv2
image = cv2.imread('Lena.jpg')
cv2.imwrite('Mypic.jpg',image)
I could solve this problem by simply adding the extension '.jpg' etc at the end of the image and it worked for me!

Categories