Python Forum
PIL Image im.show() no show!
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
PIL Image im.show() no show!
#1
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])

Attached Files

Thumbnail(s)
   
Reply
#2
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)
Reply
#3
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!!
__________________
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  encode/decode to show correct country letters in a CTk combobox janeik 2 717 Sep-02-2023, 09:46 AM
Last Post: janeik
  PDF properties doesn't show created or modified date Pedroski55 4 1,091 Jun-19-2023, 08:09 AM
Last Post: Pedroski55
  Multiple.ui windows showing at the same time when I only want to show at a time eyavuz21 4 1,032 Dec-20-2022, 05:14 AM
Last Post: deanhystad
  How to get cell(x,y).value = "\'=name(p)" to show correctly ?? ozlevia 9 3,456 Oct-31-2022, 06:47 PM
Last Post: deanhystad
  Why doesn't it show me anything in print? Melcu54 2 923 Oct-01-2022, 12:07 AM
Last Post: snippsat
Question Trouble installing modules/libraries and getting Notepad++ to show cyrillic letters Dragiev 6 2,260 Jul-24-2022, 12:55 PM
Last Post: Dragiev
  python basemap, cant show the right coordinates dbsr 0 967 Jun-08-2022, 01:55 PM
Last Post: dbsr
  Make console show after script was built with Pyinstaller --NOCONSOLE? H84Gabor 0 1,211 May-05-2022, 12:32 PM
Last Post: H84Gabor
  PIL Image im.show() no show! Pedroski55 6 4,928 Feb-08-2022, 06:32 AM
Last Post: Pedroski55
  I cannot install this library. Can someone show me how? Led_Zeppelin 8 5,894 Mar-12-2021, 05:17 PM
Last Post: snippsat

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020