Python Forum

Full Version: PIL Image im.show() no show!
You're currently viewing a stripped down version of our content. View the full version with proper formatting.
I had this problem before, but when I moved to a virtual environment, it went away.

Now, I can't do img.show() in either of my VENVs nor outside of VENV.

Anyone know what's wrong?

I have a little home-made OMR programme, the multichoice answer forms are made as PDFs. When I mark them, I scan the answer sheets to PDF, then split the PDF to .jpgs.

Each jpg needs to be cropped to just the size of a column of multichoice answers.

To do that, when I make the answers forms, I take 1 page of the PDF and crop it to get the crop coordinates.

Of course, I need to look and check that the crop coodinates are OK for any particular page.

Now, im.show() won't work anywhere and I don't know why!

Any tips please? Previously, this worked fine, now im.show() can't display!

from PIL import Image, ImageOps

image = '/home/pedro/winter2022/OMR/21BE/pdfs/21BE1_wWeek3_Answer_Forms1.jpg'

def cropImage(image):
    print('\n running cropImage() ... \n')
    print('\n you need crop coordinates for each column \n')
    # Opens an image in RGB mode 
    im = Image.open(image)
    im.show()
    # Size of the image in pixels (size of orginal image) 
    # Just for information
    width, height = im.size
    print('This image is: ', width, ' wide and ', height, ' high')      
    accept = ['yes']
    answer = 'X'
    while not answer in accept:
        print('enter the crop image top left and bottom right coordinates.')
        print('get rid of the question numbers too, don\'t need them')
        print('try something like 100 290 600 2250 for the first column')
        print('try something like 650 290 1400 2250 for the second column')
        print('this is for a second column with A to I answers ... ')
        corners = input('Enter your 4 numbers, separated by a space ... ')
        # later should be able to use fixed values for the y coordinate because it won't change
        # work on that!!
        coords = corners.split()
        for i in range(len(coords)):
            coords[i] = int(coords[i])
        # Cropped image of above dimension 
        # (It will not change orginal image) 
        im1 = im.crop((coords[0], coords[1], coords[2], coords[3])) 
          
        # Shows the image in image viewer 
        im1.show()
        print('Do you accept this image')
        answer = input('Enter yes to accept, anything else to try again ')
        if answer == 'yes':            
            print('image crop coordinates saved to tup ... ')
            return (coords[0], coords[1], coords[2], coords[3])
You don't call you function in code you show and error message make no sense.
The code dos work,could be nicer i clean it up a bit,i would have spilt it in two function.
I did answer about virtual environment in your other post

from PIL import Image, ImageOps

def cropImage(image):
    print('\n running cropImage() ... \n')
    print('\n you need crop coordinates for each column \n')
    # Opens an image in RGB mode
    im = Image.open(image)
    im.show()
    width, height = im.size
    print('This image is: ', width, ' wide and ', height, ' high')
    accept = ['yes']
    answer = 'X'
    while not answer in accept:
        corners = input('Enter your 4 numbers, separated by a space ... ')
        left, top, right, bottom  = [int(i) for i in corners.split()]
        im1 = im.crop((left, top, right, bottom))
        # Shows the image in image viewer
        im1.show()
        print('Do you accept this image')
        answer = input('Enter yes to accept, anything else to try again ')
        if answer == 'yes':
            print('image crop coordinates saved to tup ... ')
            return (left, top, right, bottom)

if __name__ == '__main__':
    image = 'forest.png'
    crop_tup = cropImage(image)
    print(crop_tup)
Test,it show image have to close image to continue to user input.
G:\div_code\image_env
λ python cro1.py

 running cropImage() ...


 you need crop coordinates for each column

This image is:  800  wide and  292  high
Enter your 4 numbers, separated by a space ... 50 100 200 250
Do you accept this image
Enter yes to accept, anything else to try again yes
image crop coordinates saved to tup ...
(50, 100, 200, 250)
Thanks for your reply!

For testing individual functions, I just paste the function in Idle and run it.

Weird! This morning, it's 05:45 here, I just tried it again, not in VENV.

The cropped image showed up in an imagemagick window, just like it should.

I didn't change anything!

So ran my OMR programme to generate multi-choice answer forms in bash, also no problem, the cropped image showed in imagemagick, just like it should!

I adjusted the coordinates a couple of times just to test it.

Mysteriously working again!

One thing I did was try to uninstall pillow. But I don't think it uninstalled, because when I tried to reinstall with pip, it just said "is already the latest version"

But it works again! I would like to know why!!
__________________