2

I'm using PIL to resize a JPG. I'm expecting the same image, resized as output, but instead I get a correctly sized black box. The new image file is completely devoid of any information, just an empty file. Here is an excerpt for my script:

basewidth = 300
img = Image.open(path_to_image)
wpercent = (basewidth/float(img.size[0]))
hsize = int((float(img.size[1])*float(wpercent)))
img = img.resize((basewidth,hsize))
img.save(dir + "/the_image.jpg")

I've tried resizing with Image.LANCZOS as the second argument, (defaults to Image.NEAREST with 1 argument), but it didn't make a difference. I'm running Python3 on Ubunutu 16.04. Any ideas on why the image file is empty?

Vadim Kotov
  • 8,084
  • 8
  • 48
  • 62
Asgeir
  • 593
  • 6
  • 21
  • code gives me correct image on Linux Mint 19.1 based on Ubuntu. Maybe you use black image or you check wrong image. Check `print(path_to_image, dir + "/the_image.jpg")` and open both images. – furas Jul 16 '19 at 08:01
  • @furas That is odd. I've checked the image paths, and they're correct. The input image looks fine, but the output image has a bit of information in it now. Still displays completely black. – Asgeir Jul 16 '19 at 08:22
  • delete output image and see if you get it again. You could also rename input filename and change its name in code to see if you read from correct folder. – furas Jul 16 '19 at 08:30
  • or you can save `img` before changes with different name and see if you get this file in expected folder and see what you have in this file before changes. – furas Jul 16 '19 at 08:32
  • @furas Immediately after opening the image, I tried saving it. It retains the original images size, but comes out completely black. – Asgeir Jul 16 '19 at 08:39
  • so your problem is in original file which you have in `path_to_image`. You can try to open and save different image to see if problem is with all images or only this one makes problem. – furas Jul 16 '19 at 08:53
  • @furas I first place a list of image paths in an excel file, and they all look fine. (I just added this to test if the paths and images are correct) When I try to save these exact same image paths, they come out as black squares. – Asgeir Jul 16 '19 at 09:04
  • I would create minimal code without excel to test only one or two images. You could also check if [img.show()](https://pillow.readthedocs.io/en/stable/reference/Image.html) will display image. You can also check in [documentation](https://pillow.readthedocs.io/en/stable/installation.html) maybe there is information what C/C++ libraries it may need to work. maybe you wil have to install something with `apt-get` – furas Jul 16 '19 at 10:02
  • @furas I only have access to a terminal. I've come to the conclusion that the script is fine, but (hurts my heart so say this) there has to be some compatibility or dependency issues. What exactly, I will try to figure out now. – Asgeir Jul 16 '19 at 10:12
  • first check if you have all libraries from [documentation](https://pillow.readthedocs.io/en/stable/installation.html#external-libraries). If you use `JGP` images then `Pillow` needs library `libjpeg`, if you use `PNG` then `libpng`, etc. `apt-get install libjpeg libpng` – furas Jul 16 '19 at 10:15
  • @furas After accidentally breaking my entire project, I got it fixed again. I've checked that `libjpeg` is installed, however the completely black image issue still remains. I have a simple script that takes an image I know isn't broken, opens it with PIL, then saves it. The new saved image is completely black. It only retains the size/dimensions of the originally opened image. The original image is 4 kilobytes (I think, Ubuntu says 4.0 K), while the saved, broken image is about 2000 bytes. – Asgeir Jul 16 '19 at 12:32

2 Answers2

4

I also encountered the same issue when trying to resize an image with transparent background. The "resize" works after I add a white background to the image.

Code to add a white background then resize the image:

from PIL import Image

im = Image.open("path/to/img")

if im.mode == 'RGBA':
    alpha = im.split()[3]
    bgmask = alpha.point(lambda x: 255-x)
    im = im.convert('RGB')
    im.paste((255,255,255), None, bgmask)

im = im.resize((new_width, new_height), Image.ANTIALIAS)

ref:

  1. Other's code for making thumbnail

  2. Python: Image resizing: keep proportion - add white background

TSLsun
  • 106
  • 2
  • 6
0

The simplest way to get to the bottom of this is to post your image! Failing that, we can check the various aspects of your image.

So, import Numpy and PIL, open your image and convert it to a Numpy ndarray, you can then inspect its characteristics:

import numpy as np
from PIL import Image

# Open image
img = Image.open('unhappy.jpg')

# Convert to Numpy Array
n = np.array(img) 

Now you can print and inspect the following things:

n.shape       # we are expecting something like (1580, 1725, 3)

n.dtype       # we expect dtype('uint8')

n.max()       # if there's white in the image, we expect 255

n.min()       # if there's black in the image, we expect 0

n.mean()      # we expect some value between 50-200 for most images
Mark Setchell
  • 191,897
  • 31
  • 273
  • 432