I have an image in JPEG which contains a solid black circle on top of a solid black inverted equilateral triangle.
Upon application of Python PIL Image class, function thumbnail, resizing from 500*500 to 100*100 the shape of the image components change from (circle+triangle) to (rectangle+rectangle), almost looks like the android icon now.
Any ideas why this is happening? I'm guessing this is an algorithm issue.
Here's the code:
im = Image.open('E:/asdsa/aret/sdfssdf/media/edasdaas1d0-1.jpeg')
im.thumbnail((100,100), Image.ANTIALIAS)
im.save('E:/asdsa/aret/sdfssdf/media/11.jpeg')
Try
im.save('E:/asdsa/aret/sdfssdf/media/11.jpeg', quality=90)
Anyway, it's better to use Pillow.
Also, for solid colors jpeg format might not be the best choice. PNG is a better solution.
Related
It is quite hard to explain what I want in the title. I'll show what I need with an example or input/output. I have an image called 'image.jpg' that'll be an image with random strokes of different shades of black and dark-blackish gray on a white background and on one area it'll be a light grey background
I want to use python's opencv and maybe PIL if it's useful and crop the image so I'll only see the gray area. So after whatever code I'll do it'll look like this image here
That's it, appreciate all help here. I don't seem to know where to start here so I can't include whatever code I tried.
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.
I'm very new to Python and am exploring it's use to allow users to build custom images. The idea is that the client would select a few options and the image would be created on the server then downloaded (or used for other things on the server side).
The image is composed of many images, most of which are small icon type of images that are irregular shapes and have transparency. All layers are .png files.
I've tried using Pillow but it seems the image needs to be the same size as the overall image to properly use the transparency of the top layers.
Here's what I've tried so far:
from PIL import Image
background = Image.open("Background.png")
foreground = Image.open("Trim.png")
fire = Image.open("Type_Fire_Large.png")
background = Image.alpha_composite(background, foreground)
background.paste(fire, (150, 150))
background.show()
The image looks like this:
Background.png is the shaded "noise" and Trim.png is the grey diagonal lines. The best part: Trim.png has the center transparent and is able to show Background.png in the middle. But it's also the same size as the image.
The problem is Fire; notice how theres that black border (and odd fuchsia dot). The documentation states that the overlay image needs to be the same size. But it seems like a common scenario where someone would want to place a smaller icon with transparency on top of another image and compose them into one image.
I'm not attached to any particular library, I'm wide open to ideas and alternatives. The only thing I'm trying to do is keep it simple, so creating an entire game engine or the like to produce an image would probably be too much.
To just paste one png on top of another, respecting transparency, try
background.paste(fire, (x,y), fire.convert("RGBA"))
First I'd say Johannes Holmberg already answered your main concern: The missing transparency.
But I can hopefully explain what it's about with that odd fuchsia dot:
A transparent color image is usually stored as RGBA (RGB for Red, Green, Blue and A for Alpha). Here Alpha defines the transparency, from no transparency to full transparency.
Overlaying the images the correct way we see the GIMP Logo but the color stripe is almost invisible - because it's (almost) transparent.
But - as every pixel could be possibly visible - every pixel does still have a color. Even those pixels with 100% transparency. Thus, if we do not take Alpha into consideration we see each pixel color without transparency. This way we might have disturbing colors that are usually not disturbing at all - as long as they are fully transparent.
I am trying to paste many small grayscale images into a bigger one. All images are jpegs. The small images had been previously rotated, so they have black background. What I wanted to do is to paste them without a background color, in other words, I need the background color to be transparent.
Thank you for your suggestions,
to my knowledge, jpg does not support transparency, you probably want your output to be a png, and you will need to set the alpha channel to be nothing
http://www.talkgraphics.com/showthread.php?22385-How-do-I-make-jpeg-image-background-transparent
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.