extending an image with Python Imaging Library - python

I am trying to increase the height of an image using PIL but I don't want the image to be resized; I actually want a strip of blank pixels at the bottom of the image. Any way of doing this with PIL?
I guess one way would be to make a new image of the required size and copy the old image into it but I can't seem to find the right function to do this.

Oops, just realized you can do image.crop() and it will resize the image for you.

Related

Make a frame to an image in python

Images are represented as matrices. Is there a practical way to make sort of frame around the content of the image? (in a monoton color)
Theres a lot of ways to do that. I think the easiest way is just to add an image with everything transparent except for the borders and then draw it on top of the screen every frame.
Like Miguel Pereira says, the easiest way is paste a frame previously made in the image that you want set the frame. You can use PIL like this.
from PIL import Image
img_frame = Image.open("frame_name.png")
img_bg = Image.open("bg_name.png")
img_bg.paste(img_frame, (cord_x, cord_y))
img_bg.show()
You may need to resize one of the images to fit

Enlarge picture using Pillow package keep ratio

I am trying to use python pillow package to enlarge picture and here is my try
from PIL import Image
image = Image.open('Sample.jpg')
new_image = image.resize((1080, 1080))
new_image.save('Output.jpg')
The code is working but the output image loses completely the ratio. How can I enlarge the image without losing the quality?
What you're talking about is image upscaling, which is a more complex problem just than enlarging the image. I recommend looking into image upscaling packages. A lot of them use CNNs for this.

How can you crop an SVG Image in Python?

With a program, I am producing an SVG image with dimensions of 400px x 400px. However, I would like to crop the bottom of this SVG image off, based off of a variable that dictates how much of the bottom of the image should be cropped in pixels.
This SVG image is being generated with pyCairo with surface = cairo.SVGSurface("output.svg", WIDTH, HEIGHT) and ctx = cairo.Context(surface).
Although the HEIGHT variable is a constant and isn't changed, after I perform some operations on the surface object, I would like to be able to resize it once more. I can use the Pillow Image object to crop PNGs, but it does not support SVGs.
I have also tried to open the svg file with open("output.svg"). However, if I try to read it, I am unable to and it shows up as blank, thus making it unmodifiable.
Is there any way in Python to either crop an SVG image or modify its size after it has been modified with pycairo?
The answer above is incomplete and at least for me doesn't solve the problem.
A SVG can simply be cropped (trimmed, clipped, cut) using vpype with the crop or trim and translate commands.
import vpype_cli as vp
#vp.excute("read test.svg translate 300 400 trim 30 20 write output.svg")
vpype_cli.execute("read test.svg crop 0cm 0cm 10cm 20cm write output.svg")
Playing around with the parameters should lead to the desired crop.
Took some time to find this, as most answers say it cant be done, which is ridiculous.
You cannot crop SVG like you crop PNG because in the latter you can just drop pixels, while for the former you have defined paths that can't be easily recomputed.
If you're sure there's nothing in the part you are about to "crop", you can use set_context_size to make the svg context/canvas smaller while preserving ratio and size inside.

the pillow make the image being more size than before

I use python pillow to do a easy gif image reverse,but I found that the image has become more size(10m) than before(1m). Anyone know how to make it?
And here is my code:
from PIL import Image, ImageSequence
from PIL import ImagePalette
with Image.open('sd.gif') as im:
if im.is_animated:
frames = [f.copy() for f in ImageSequence.Iterator(im)]
frames.reverse()
frames[0].save('out.gif', save_all=True, append_images = frames[1:])
I can't tell for sure without examining the actual images, but I can guess what likely happened:
Some gifs are optimized with a method that finds pixels in each frame where nothing is changing (or changing only very slightly) from frame to frame, and make them transparent instead of storing the color for each pixel, to reduce the amount of data. For some gifs with large static areas in many consecutive frames this can be very efficient way to reduce file size.
When you are reversing the GIF, the frames must be unoptimized first, otherwise there would be transparent areas without any data. This can increase file size quite a bit. The difference may vary from one image to another.
You probably can solve this by running some gif optimization algorithm after the new image is created.

How do you display a 2D numpy array in glade-3 ?

I'm making live video GUI using Python and Glade-3, but I'm finding it hard to convert the Numpy array that I have into something that can be displayed in Glade. The images are in black and white with just a single value giving the brightness of each pixel. I would like to be able to draw over the images in the GUI so I don't know whether there is a specific format I should use (bitmap/pixmap etc) ?
Any help would be much appreciated!
In the end i decided to create a buffer for the pixels using:
self.pixbuf = gtk.gdk.Pixbuf(gtk.gdk.COLORSPACE_RGB,0,8,1280,1024)
I then set the image from the pixel buffer:
self.liveImage.set_from_pixbuf(self.pixbuf)
I think these are the steps you need:
use scipy.misc.toimage to convert your array to a PIL image
check out the answer to this question to convert your PIL image to a cairo surface
use gdk_pixbuf_get_from_surface to convert this to a pixbuf (I don't know it's name in the python api)
make a Gtk.Image out of this using Gtk.Image.new_from_pixbuf
I'm sorry it needs so many conversion steps.

Categories

Resources