If I'm using an image and I want to know the image's size in the file.
There is a function to get the picture's height and width ?
EDIT: Of course I loaded the image to the program with pygame.image.load(PATH).
I believe you need to load the image as a Surface before you can get its width and height. You do that with foo = pygame.image.load(PATHNAME).
Then you can get the width and height by creating a Rectangle with foo.get_rect() and asking the rectangle.
import pygame
foo = pygame.image.load(PATH).get_rect().size
print(foo)
Return the size in a tupple
Related
We are using the tkinter library and Image class to display images from a file. In the program we change the pixels (so we have an array with new pixels) and want to display it in the tkinter window as well. (we can't use plt.show() or smth like this, we need to change pixels in Image, because it works only with it)
image = Image.open(files_name)
img = ImageTk.PhotoImage(image)
disp_img.config(image=img)
disp_img.image = img
The best option we've seen is .putpixel. But 1) changing each pixel separately is too long 2) it has a strange parameters, and we are not sure about using it
I'm looking for a library that enables to "create pictures" (or even videos) with the following functions:
Accepting picture inputs
Resizing said inputs to fit given template / scheme
Positioning the pictures in pre-set up layers or coordinates
A rather schematic approach to look at this:
whereas the red spots are supposed to represent e.g. text, picture (or if possible video) elements.
The end goal would be to give the .py script multiple input pictures and the .py creating a finished version like mentioned above.
Solutions I tried were looking into Python PIL, but I wasn't able to find what I was looking for.
Yes, it is possible to do this with Python.
The library you are looking for is OpenCV([https://opencv.org][1]/).
Some basic OpenCV python tutorials (https://docs.opencv.org/master/d9/df8/tutorial_root.html).
1) You can use imread() function to read images from files.
2) You can use resize() function to resize the images.
3) You can create a empty master numpy array matching the size and depth(color depth) of the black rectangle in the figure you have shown, resize your image and copy the contents into the empty array starting from the position you want.
Below is a sample code which does something close to what you might need, you can modify this to suit your actual needs. (Since your requirements are not clear I have written the code like this so that it can at least guide you.)
import numpy as np
import cv2
import matplotlib.pyplot as plt
# You can store most of these values in another file and load them.
# You can modify this to set the dimensions of the background image.
BG_IMAGE_WIDTH = 100
BG_IMAGE_HEIGHT = 100
BG_IMAGE_COLOR_DEPTH = 3
# This will act as the black bounding box you have shown in your figure.
# You can also load another image instead of creating empty background image.
empty_background_image = np.zeros(
(BG_IMAGE_HEIGHT, BG_IMAGE_WIDTH, BG_IMAGE_COLOR_DEPTH),
dtype=np.int
)
# Loading an image.
# This will be copied later into one of those red boxes you have shown.
IMAGE_PATH = "./image1.jpg"
foreground_image = cv2.imread(IMAGE_PATH)
# Setting the resize target and top left position with respect to bg image.
X_POS = 4
Y_POS = 10
RESIZE_TARGET_WIDTH = 30
RESIZE_TARGET_HEIGHT = 30
# Resizing
foreground_image= cv2.resize(
src=foreground_image,
dsize=(RESIZE_TARGET_WIDTH, RESIZE_TARGET_HEIGHT),
)
# Copying this into background image
empty_background_image[
Y_POS: Y_POS + RESIZE_TARGET_HEIGHT,
X_POS: X_POS + RESIZE_TARGET_WIDTH
] = foreground_image
plt.imshow(empty_background_image)
plt.show()
I need to resize an image. The original is 1024x768. My laptop screen is set to 1366x768. When I go to view the image the bottom is always cut off. I'm guessing it's because the image is 1024x768 but the image size doesn't take into account the box/window the image sits in, so the bottom of the image gets cuts off as a result.
What is the size, pixelwise, of the box/window and how do I determine the size of my screen, codewise, so I can reset the size of the image so the entire image will fit on the screen and none of it will get cut off. Or is there a way of having the image autoscale so it will fit the screen height resolution? I'm using PIL.
I know I can in the end just
new_image = old_image.resize(x, 768-box_height)
I just need to know the box height.
The most environment-agnostic way is likely to just ask tkinter:
import tkinter #python 3 syntax
root = tkinter.Tk()
root.withdraw()
width, height = root.winfo_screenwidth(), root.winfo_screenheight()
I'm working with the python graphics module. What I am trying to do is save the current window as an image. In the module there is an option to save an "image" as an image (image.save()). But that isn't helpful because it just saves an image you have already loaded. OR if you load a blank image like I did in hopes drawing over it would change that, surprise, surprise: you get a blank image saved. Here is my code:
from graphics import *
w = 300
h = 300
anchorpoint=Point(150,150)
height=300
width=300
image=Image(anchorpoint, height, width) #creates a blank image in the background
win = GraphWin("Red Circle", w, h)
# circle needs center x, y coordinates and radius
center = Point(150, 150)
radius = 80
circle = Circle(center, radius)
circle.setFill('red')
circle.setWidth(2)
circle.draw(win)
point= circle.getCenter()
print point
pointx= point.getX()
pointy= point.getY()
print pointx
print pointy
findPixel=image.getPixel(150,150)
print findPixel
image.save("blank.gif")
# wait, click mouse to go on/exit
win.getMouse()
win.close()
#######that's it#####
so again here is my problem: How do I save what is now on the screen as "blank.gif"
Thanks!
The objects you are drawing are based on Tkinter. I don't believe you are actually drawing on the base image, but rather simply creating Tkinter objects by using the "graphics" library. I also don't believe you can save a Tkinter to a "gif" file, though you can definitely save them in postscript format, then covert them to a gif format.
In order to do this, you will need python's PIL library.
If all of your objects are actually TKinter objeccts, you can simply save the objects.
Start by replacing this line of code:
image.save("blank.gif")
With the following:
# saves the current TKinter object in postscript format
win.postscript(file="image.eps", colormode='color')
# Convert from eps format to gif format using PIL
from PIL import Image as NewImage
img = NewImage.open("image.eps")
img.save("blank.gif", "gif")
If you need additional information, please check out http://www.daniweb.com/software-development/python/code/216929 - which is where I got the suggested code.
I'm sure there are more elegant solutions available than save/convert, but since I don't know a lot about TKinter - this is the only way I've found.
Hope it helps!
How to know the width and the height of image using app-engine image API wit python, someone adviesed me to use the composite function - pass your image in as the sole input image, and specify the canvas size as the desired size of the output image.
but i didn't understand that!!
any simple example will be very helpful ..
and thanks in advance !
If i understand your question, you just need to use the attributes width and height from a reference of google.appengine.api.images.Image.
An example:
flux = img.read() # From your request
image_ref = Image(image_data=flux)
w = image_ref.width
h = image_ref.height