PIL shows black image when attempting to view image - python

The current goal of what I am writing is to download an image from S3, open it using Pillow, and show the image. I am able to download the image fine; it can be viewed properly from my photos. However, when I try to use the image with PIL, all of the pixel values are black. It does however, retain the shape of the image, which leads me to know that the image is at least being read. the code is shown below:
s3.Bucket(bucket).download_file(key, key) # downloaded perfectly fine
img = Image.open(key)
img.show() # shows all black with the Images's dimensions
I know I can read from bytes, but that will give me a 1d array of all the bytes instead of the dimensions needed for an image.

Related

How to Increase image quality in streamlit

I am viewing images by using streamlit without converting to RGB below is the line of code which I am using
st.image("image.jpg")
But the problem is I need to read some numbers inside the image but the image is blurred which means that I am not able to read the numbers present in the image.
Increasing the size of the image does not work.

When loading and writing a PNG image to PDF using Pillow (PIL), how do I fix the size of the image in the PDF?

I am reading in a list of PNG images from a directory and I want to maintain the image sizes.
However, when I go to save them, each image is automatically resized to fit the entire page AND it automatically rotates the images to landscape rather than keeping them portrait.
Example of what I am doing below:
from PIL import Image
x = []
# get list of images
for i in [list of image files in my directory]:
im = Image.open(i)
im = im.convert('RGB') # will not save as RGBA
x.append(im)
im.save('my_doc.pdf', save_all=True, append_images=x)
Yet, the images which are all the same dimensions and sized to fit neatly onto a single page in portrait orientation, are being rotated to landscape orientation and stretched to the limits of the page making them blurry as a result.
I want one image per page, using the native dimensions of the images, and in portrait.
Is there a way to prevent PIL from doing this?
Thanks in advance!

Open CV cv.imread modifying image

I use cv.imread to read a png file in python. When I then use cv.imwrite function to immediately save the image i then find that the colours in the image have changed slightly. I am trying to perform character recognition on this image and the OCR performs far less well on the image in python than the original image.
The first image is the original, and the second is the saved one with OpenCV.
We can see that the green has changed slightly and whilst this does not seem important it affects the OCR and I therefore imagine that other changes are happening to the png. Does anyone know why this might be and how i can resolve this.
The code is as follows
img = cv2.imread('file.png')
cv2.imwrite('out.png', img)
When I run file.png in tesseract for character recognition I get great results but when I run out.png in tesseract far less words get recognised correctly.
When you have a .png image file you ought to read as a .png file.
I downloaded your image and did some analysis myself.
First, I read the image as you did:
img = cv2.imread('file.png')
img.shape returns (446, 864, 3) i.e an image with 3 channels.
Next I read the same image using cv2.IMREAD_UNCHANGED:
img = cv2.imread('file.png', cv2.IMREAD_UNCHANGED)
img.shape returns (446, 864, 4) i.e an image with 4 channels.
.png files have an additional transparency channel. So next you come accross a .png file read it using cv2.IMREAD_UNCHANGED flag
UPDATE:
Enlisting the various ways to read an image:
for var in dir(cv2):
if var.startswith('IMREAD'):
print(var)
returns:
IMREAD_ANYCOLOR
IMREAD_ANYDEPTH
IMREAD_COLOR
IMREAD_GRAYSCALE
IMREAD_LOAD_GDAL
IMREAD_UNCHANGED

Python PIL thumbnail with Image.ANTIALIAS. How to preserve quality

I'm trying to resize images while keeping aspect ratio. I use PIL's thumbnail method for this. I use Image.ANTIALIAS filter.
You can check my code here:
image = Image.open(design.design.path)
format = image.format
image = ImageOps.mirror(image)
new_size = (241, 241)
image.thumbnail(new_size, Image.ANTIALIAS)
image.save(response, format)
This code works perfectly however quality is lost after thumbnail. I can see it by zooming in on the saved image. I can see pixels at the corners of the image while I don't on the original image. This is seen even better when I print out resized image.
You can check out sample images here: http://imgur.com/a/ifZoU
Please tell me if you need anything else
Image.save has options for you:
img.save(fpath, 'image/png', quality=100, optimize=True)
If you are making thumbnails of a pixel-based image, of course you will loose quality. Re-sizing this kind of image (as opposed to vector images) simply throw information away - there is no way to recover it.
IF you need to view the image in full size, you have to preserve the original image, and keep the scaled-down version to be shown only where it is needed - use the original for everything else.
https://en.wikipedia.org/wiki/Raster_graphics

Strange bug while combining images in Python

I have a hundred 10x10 px images, and I want to combine them into a big 100x100 image. I'm using the Image library to first create a blank image and then paste in the smaller images:
blank = Image.new('P',(100,100))
blank.paste(im,box)
The smaller images are in color, but the resulting image turns out in all grayscale. Is there a fix or workaround for this?
It's probably something to do with using a palette type image (mode P). Is there a specific reason you are doing this? If not, try passing 'RGB' as the first argument.

Categories