0

I have made a couple of images which I need for a CNN using matplotlib. But I would like to read only the base of the image (without the axis and the whitespace), because I found out that my CNN which uses keras and tensordlow reads the whole image (meaning that the numbers on the axis have a big impact on the prediction). Any ideas on how can I do that?

Note that I already have an image with the axis and the whitespace. I am just wondering how can I edit them out.

This is the full image:

enter image description here

And this is what I need to use:

enter image description here

tdy
  • 36,675
  • 19
  • 86
  • 83
  • 1
    [Store the image in matplotlib without axis and whitespace](https://stackoverflow.com/a/11847260/8881141)? – Mr. T Mar 19 '22 at 20:02
  • _"But what if I already have an image with the axis and the whitespace? How can I edit it then?"_ In that case you need some kind of automatic cropping, e.g. [OpenCV/Python for auto-cropping](https://stackoverflow.com/q/37803903/13138364) or [Automatically cropping image in python](https://stackoverflow.com/q/41746416/13138364) or [Automatically cropping image with PIL](https://stackoverflow.com/q/14211340/13138364) – tdy Mar 19 '22 at 21:20
  • 1
    Or if you know every image will have the exact same margins, you can [crop to a certain bounding box](https://stackoverflow.com/q/6496394/13138364) (or even [crop externally with imagemagick `convert`](https://superuser.com/q/1161340/1283608) for example) – tdy Mar 19 '22 at 21:25

1 Answers1

0

You have to crop the images, because they already have the axis and whitespace on them.

Firtstly resize them to same dimensions:

from PIL import Image

image = Image.open('sunset.jpg')
print(f"Original size : {image.size}") # 5464x3640

sunset_resized = image.resize((400, 400))
sunset_resized.save('sunset_400.jpeg')

Than crop them (link to code explanation-https://www.geeksforgeeks.org/python-pil-image-crop-method/):

from PIL import Image
 
# Opens a image in RGB mode
im = Image.open(r"C:\Users\Admin\Pictures\network.png")
 
# Setting the points for cropped image
left = 155
top = 65
right = 360
bottom = 270
 
# Cropped image of above dimension
# (It will not change original image)
im1 = im.crop((left, top, right, bottom))
 
# Shows the image in image viewer
im1.show()