I want to make something that you can compare images and choose the one you like the best in pygame, how can I display multiple images in one screen?
Load a second image and reposition it next to the first image object.
Here is a tip on how to work with positioning: Pygame Image position
Related
I am trying to make a game in python 3 which requires a background image. I already have the image. The problem here is my image looks small compared to the big screen. I don't want to change the size of the screen because then I would have to redo all the coordinates of the other objects on the screen. Is there any way I can increase the size of the background image?
Ps. I'm using Python 3 and on VS code.
Thanks in advance!
This is the picture of what the small picture looks like.enter image description here
In order to increase the size of the image, you would need too increase the resolution.
Assuming that you are using windows, open the png with Microsoft Photos, and click on the three horizontal dots at the top right.
A dropdown menu will open, press the third option from the top labeled "Resize"
After this, press on "Define custom dimensions" and you may manipulate the dimensions of the image as you like.
Then, simply save the resized image, and use it in your project.
you can also do this using CV2 library
import cv2
image=cv2.imread("image.png")
scale_percent=1.5
width=int(image.shape[1]*scale_percent)
height=int(image.shape[0]*scale_percent)
dimension=(width,height)
resized = cv2.resize(image,dimension, interpolation = cv2.INTER_AREA)
print(resized.shape)
cv2.imwrite("output.png",resized)
OR you can use PIL library as well
from PIL import Image
image = Image.open("image.png")
image.save("output.png", dpi=(image.size[0]*1.5,image.size[1]*1.5))
I am working on a program to crop a image around a rectangle in OpenCV. How could I go about doing this. I also need it to be able to turn multiple rectangles into cropped images.
I've tried using this tutorial: https://www.pyimagesearch.com/2016/02/08/opencv-shape-detection/, but I dont know how to get the borders of the shape and crop around it.
I hope to get an output of multiple images, that have pictures of the contents of the triangle.
Thank you in advance
I have just recently done this for one of my projects, and it worked perfectly.
Here is the technique I use to implement this in Python OpenCV:
Display the image using OpenCV's cv2.imshow() function.
Select 2 points (x, y) on an image. This can be done by capturing mouse click events with OpenCV. One way to do this is to click with your mouse where the first point is, while still clicking move towards the second points, and let go from the mouse click once the cursor is over the correct point. This selects the 2 points for you. In OpenCV, you can do this with cv2.EVENT_LBUTTONDOWN and cv2.EVENT_LBUTTONUP. You can write a function to record the two points using the mouse capture events and pass it to cv2.setMouseCallback().
Once you have your 2 coordinates, you can draw a rectangle using OpenCV's cv2.rectangle() function, where you can pass the image, the 2 points and additional parameters such as the colour of the rectangle to draw.
Once you're happy with those results, you can crop the results using something like this:
image = cv2.imread("path_to_image")
cv2.setMouseCallback("image", your_callback_function)
cropped_img = image[points[0][1]:points[1][1], points[0][0]:points[1][0]]
cv2.imshow("Cropped Image", cropped_img)
cv2.waitKey(0)
Here is one of the results I get on one of my images.
Before (original image):
Region of interest selected with a rectangle drawn around it:
After (cropped image):
I started by following this excellent tutorial on how to implement it before further improving it on my own, so you can get started here: Capturing mouse click events with Python and OpenCV. You should also read the comments at the bottom of the attached tutorial to easily improve the code.
You can get co-ordinates of box using 'BoundedRect' function. Then use slicing operation, to extract required part of image.
I take part in a project in which we are making a sudoku solver. I want to print the image of the solved sudoku grid on the screen while our drawing table is drawing the solution on the paper grid.
But I can't find a way to display an image and my code keeps running.
I have looked into - I think - all of the opencv and matplotlib.pyplot functions to display images but every time the code stops when the image is displayed and continues once the image is closed (plt.show() or using cv2.waitKey()).
So if anyone has an idea of a way to display an image while the python code keeps running, I'd be glad to hear it.
Thanks
The PIL/Pillow Image.show() method will leave your image showing on the screen and your code will continue to run.
If you have a black and white image in a Numpy/OpenCV array, you can make it into a PIL Image and display it like this:
from PIL import Image
Image.fromarray(NumpyImg).show()
If your image is colour, you'll need to go from BGR to RGB either using cv2.cvtColor(...BGR2RGB..) or by reversing your 3rd channel something like (untested):
Image.fromarray(NumpyImg[:,:,::-1]).show()
I'm testing out pygame (in python 2.7) before I try to use it in any programs, what I am trying to do is make an image appear in the display, wait for half a second, then cover the previous image with a new one, but the second image is not placed on top of the old image, but underneath it. I tried switching the image order, using different images, and even tried using 3 images.
import time as t
import pygame
from pygame.locals import *
pygame.init()
screen = pygame.display.set_mode((1000,1000),0,32)
image1 = pygame.image.load("image1.png").convert_alpha()
image2 = pygame.image.load("image2.png").convert()
while True:
screen.blit(image1,(0,0))
pygame.display.update()
t.sleep(0.5)
screen.blit(image2,(0,0))
pygame.display.update()
Your image2 does in fact appearing in front of your image1. But only for a split second.
Immediately after you update the display, you go back to the start of the while loop, which immediately blits another copy of image1 in front of image2.
If you wanted these to alternate back and forth every half second, you just need to add another sleep at the end of the loop.
If you wanted it to just draw image1, wait, draw image2, and be done with it, just get rid of the while loop.
I am coding for a mouse drag and drop effect on images. Meanwhile, I want to take record of the upper-left point of image each time I dragged and dropped it, are there any ways to get it?
What methods are you using to draw the images? It's hard to answer this question without that.
If you aren't already doing this, you could use a class to hold data about your image, such as position and geometry.
If you derive your classes from pygame.sprite.Sprite , you can get the position by guy.rect. Depending on if you want center, or toplef, or the full rect:
guy.rect.topleft or guy.rect.center or guy.rect