combine two pictures(different size) horizontally? - numpy

I have 2 pictures, need to combine them horizontally. I know numpy and cv2(opencv) should help me to do this. But don't know how.
I used img1 = cv2.imread(file1), img2 = cv2.imread(file2)
the 2 images' shape are (2048, 1334, 3) and (720, 1200, 3)
How could I do this? when I open These 2 images, they have similar height, different width.
I only know if the 2 pics have the same size, then just use concate, but my 2 pics are different sizes.
For the final output, I want to have them keep their own width, height choose the biggest/smallest...
So I imagine the final output should maybe 2/3 width one picture, 1/3 width the other pic which is totally good. I don't need these 2 are evenly distributed. Just keep their own width. Thanks!

You need to either trim a bit of the bottom of the taller image or add some black pixels.
In order to trim a part of the image, you can do:
trimmed = image2[:image1.shape[0],:,:]
This keeps only the lines from 0 up to the height of image1.
Or, you can add some black pixels:
black = np.zeros(image1.shape[0] - image2.shape[0], image1.shape[1])
image2 = np.hstack(image2, black)
And then you vertically concatenate.

I just solved my question.
Basically use cv2.resize()function to resize the images
Then simply concatenate them horizontally or vertically.
Just change the axis.
img1 = cv2.imread('xxx.png')
img2 = cv2.imread('yyy.jpg')
then compare img1.shape() and img2.shape()
Use resize()function to make them width the same or height the same.
vis = np.concatenate((img1, img2), axis=1)
cv2.imwrite('out.png', vis)

Related

How to Zero Pad RGB Image?

I want to Pad an RGB Image of size 500x500x3 to 512x512x3. I understand that I need to add 6 pixels on each border but I cannot figure out how. I have read numpy.pad function docs but couldn't understand how to use it. Code snippets would be appreciated.
If you need to pad 0:
RGB = np.pad(RGB, pad_width=[(6, 6),(6, 6),(0, 0)], mode='constant')
Use constant_values argument to pad different values (default is 0):
RGB = np.pad(RGB, pad_width=[(6, 6),(6, 6),(0, 0)], mode='constant', constant_values=0, constant_values=[(3,3),(5,5),(0,0)]))
We can try to get a solution by adding border padding, but it would get a bit complex. I would like to suggest you can alternate approach. First we can create a canvas of size 512x512 and then we place your original image inside this canvas. You can get help from the following code:
import numpy as np
# Create a larger black colored canvas
canvas = np.zeros(512, 512, 3)
canvas[6:506, 6:506] = your_500_500_img
Obviously you can convert 6 and 506 to a more generalized variable and use it as padding, 512-padding, etc. but this code illustrates the concept.

PyGObject: How to do a Grid

Ok, I have a ScrolledWindow with inside a Viewport and a Fixed inside of that. I'm using the Builder so I'm not gonna post ALL the code if is not necessary.
I'm using a function that multiplies the given coordinates per 50, so i have a grid with 50 x 50 pixel's squares (the number of squares can variate in the config).
The real question is very simple, how I can put a background of a grid of 50 per 50 pixels? And that should be """infinite""". Preferly the lines should be of 1 px.
Note: I'm not using a grid because I need only to put Images or Icons
I used that. Mainport is the fixed element, and wres and hres are the number of squares
for i in range(self.wres):
image = gtk.Image.new_from_file("resources/Back.png")
self.mainport.put(image, i*50, 0)
for z in range(self.hres):
image2 = gtk.Image.new_from_file("resources/Back.png")
self.mainport.put(image2, i*50, z*50)

Convert Image into Flag

I have to change any given picture into the Pride flag.
To do so, my current code is:
pixels = getPixels(picture)
width = getWidth(picture)
height = getHeight(picture)
for index in range(0,len(pixels)/7):
pixel = pixels[index]
setColor(pixel,red)
for index in range(len(pixels)/7,len(pixels)):
pixel = pixels[index]
setColor(pixel,orange)
for index in range(2*len(pixels)/7,len(pixels)):
pixel = pixels[index]
setColor(pixel,yellow)
Note: I have not included the entire code snippet, it continues on in that same manner.
The problem is that the lines colours are interjecting into each other and it shows up as this:
What could be causing this and how do I go about fixing it?
It would seem there is an uneven about of pixels for the line divisions you are doing. Find out how many pixels there are for each line then use that in your for loops. For example...
no_pixels_per_line = len(pixels) / height
for index in range(0, (height/7) * no_pixels_per_line):
pixel = pixels[index]
setColor(pixel,red)
etc...

Add horizontal padding if the original image is less than the width specified

I don't think this is possible out of the box but wanted to make sure.
We'd like to do the following.
Take any image input and force the output width to be a fixed size. If the width is less than the output width, we'd like to center the image and add horizontal padding to the image but not add vertical padding.
For example
Original image is 700px x 400px
Final output size of 1000px width x 400px. This would include 150px padding left and 150px padding right (no top / bottom padding).
I know that we can upscale the image (scale=both) or set the canvas scale (but that adds top / bottom padding) or we could add padding to the image but none are really what we want.
Thanks for any help
Response to Nathanael
Your comments are exactly correct.
I expected http://z.zr.io/ri/red-leaf.jpg?width=1000&scale=canvas&bgcolor=gray to work exactly as it does
Yes, our problem is that the image heights are not known beforehand, but it's good to know that this works with a known height
I think it would be great if there was a command for scale=padwidth that would work with variable heights. Or a setting for padwidth=true and padheight=false that could be used in conjunction with scale=canvas.
So, let's say that you're given an 800x600px image, and you apply ?width=1000&scale=canvas. You were expecting that this would produce a 1000x600px image, but instead it produced a 1000x750px image, right?
http://z.zr.io/ri/red-leaf.jpg?width=1000&scale=canvas&bgcolor=gray
If you specify the height explicitly, the padding goes away - but you may not know the image height beforehand, correct?
http://z.zr.io/ri/red-leaf.jpg?width=1000&scale=canvas&bgcolor=gray&height=600
What would be the least surprising behavior - maintaining aspect ratio, or only adding the minimum padding required? How would you expect this to behave, or be exposed as a command?

Objective C - Adjust height of image in image view automatically after original size

I am developing an app that will show flags for countries some places, but after looking at flags I realized that the flags format was different for almost every country. Therefor I would like the height of the image view to automatically adjust it self to the width i set. Example:
Standard width for all flags: 100 px
USA : Height: 50px
UK: Height 56 px
Russia: Height 34px
I have no idea how to solve this, thanks for help! The best would be if this could be done automatically - without me needing to create example arrays or something for every flag to adjust size.
This is a ratio problem. Suppose your English flag is 120x80px. You want it to be 100px wide. How tall will it be? You have to scale the width and height by the same ratio.
First of all, calculate the ratio between the desired width of the flag and its actual width:
CGFloat ratio = 100.0f / 120.0f;
That gives us a ratio of 0.83. Now we can calculate the display height by multiplying the actual height by the ratio:
CGFloat height = 80.0f * 0.83;
The display height is 66.4px.
Here's the neat thing: UIImageView does this for you. If you specify the width of the image view and set its content mode to UIViewContentModeScaleAspectFit it does the hard work automatically.
See this question:
How to scale a UIImageView proportionally?
You could set the size of the image view from the size of the image as follows:
UIImage* flagImage = [UIImage imageNamed:"flagimage.png"];
CGRect flagImageRect = flagImageView.frame;
flagImageRect.size = flagImage.size;
flagImageView.frame = flagImageRect;
flagImageView.image = flagImage;
I guess you would do the above in a loop where you are setting the flag images for all your image views. You could also take the opportunity to scale your images (if desired).