Mapping List Images into container to view them shows the same pic in all of the containers
You're using the same name for all the images, change your code to this:
finalCroppedImage.isNotEmpty
? finalCroppedImage.map((image) { //Instance of image from Image package
print(image.height); //here i have different values for the images
print(image.width);
File imageFinal = File(image!.path);
File(imageFinal!.path).writeAsBytesSync(encodePng(image)); // I'm trying to decode it to display in the container
return Container(
height: 300,
decoration: BoxDecoration(
image: DecorationImage(
fit: BoxFit.fitWidth,
image: FileImage(File(imageFinal!.path)))),
);
}).toList()
: [Text('Empty')],
Stretch and scale a CSS image in the background - with CSS only
CSS3 has a nice little attribute called background-size:cover
.
This scales the image so that the background area is completely covered by the background image while maintaining the aspect ratio. The entire area will be covered. However, part of the image may not be visible if the width/height of the resized image is too large.
CSS Display an Image Resized and Cropped
You could use a combination of both methods eg.
.crop { width: 200px; height: 150px; overflow: hidden; }
.crop img { width: 400px; height: 300px; margin: -75px 0 0 -100px; }
<div class="crop"> <img src="https://i.stack.imgur.com/wPh0S.jpg" alt="Donald Duck"> </div>
Resize image to maintain aspect ratio in Python, OpenCv
Using OpenCV
You can use resize()
in OpenCV to resize the image up/down to the size you need. However, resize()
requires that you put in either the destination size (in both dimensions) or the scaling (in both dimensions), so you can't just put one or the other in for 1000 and let it calculate the other for you. So the most robust way to do this is to find the aspect ratio and calculate what the smaller dimension would be when the bigger one is stretched to 1000. Then you can resize.
h, w = img.shape[:2]
aspect = w/h
Note that if aspect
is greater than 1, then the image is oriented horizontally, while if it's less than 1, the image is oriented vertically (and is square if aspect = 1
).
Different interpolation methods will look better depending on whether you're stretching the image to a larger resolution, or scaling it down to a lower resolution. From the resize()
docs:
To shrink an image, it will generally look best with CV_INTER_AREA interpolation, whereas to enlarge an image, it will generally look best with CV_INTER_CUBIC (slow) or CV_INTER_LINEAR (faster but still looks OK).
So, after resizing we'll end up with a 1000xN
or Nx1000
image (where N<=1000
) and we'll need to pad it with whatever background color you want on both sides to fill the image to 1000x1000
. For this you can use copyMakeBorder()
for a pure OpenCV implementation, or since you're using Python you can use numpy.pad()
. You'll need to decide what to do in case an odd number of pixels needs to be added in order to make it 1000x1000
, like whether the additional pixel goes to the left or right (or top or bottom, depending on the orientation of your image).
Here's a script that defines a resizeAndPad()
function which automatically calculates the aspect ratio, scales accordingly, and pads as necessary, and then uses it on a horizontal, vertical, and square image:
import cv2
import numpy as np
def resizeAndPad(img, size, padColor=0):
h, w = img.shape[:2]
sh, sw = size
# interpolation method
if h > sh or w > sw: # shrinking image
interp = cv2.INTER_AREA
else: # stretching image
interp = cv2.INTER_CUBIC
# aspect ratio of image
aspect = w/h # if on Python 2, you might need to cast as a float: float(w)/h
# compute scaling and pad sizing
if aspect > 1: # horizontal image
new_w = sw
new_h = np.round(new_w/aspect).astype(int)
pad_vert = (sh-new_h)/2
pad_top, pad_bot = np.floor(pad_vert).astype(int), np.ceil(pad_vert).astype(int)
pad_left, pad_right = 0, 0
elif aspect < 1: # vertical image
new_h = sh
new_w = np.round(new_h*aspect).astype(int)
pad_horz = (sw-new_w)/2
pad_left, pad_right = np.floor(pad_horz).astype(int), np.ceil(pad_horz).astype(int)
pad_top, pad_bot = 0, 0
else: # square image
new_h, new_w = sh, sw
pad_left, pad_right, pad_top, pad_bot = 0, 0, 0, 0
# set pad color
if len(img.shape) is 3 and not isinstance(padColor, (list, tuple, np.ndarray)): # color image but only one color provided
padColor = [padColor]*3
# scale and pad
scaled_img = cv2.resize(img, (new_w, new_h), interpolation=interp)
scaled_img = cv2.copyMakeBorder(scaled_img, pad_top, pad_bot, pad_left, pad_right, borderType=cv2.BORDER_CONSTANT, value=padColor)
return scaled_img
v_img = cv2.imread('v.jpg') # vertical image
scaled_v_img = resizeAndPad(v_img, (200,200), 127)
h_img = cv2.imread('h.jpg') # horizontal image
scaled_h_img = resizeAndPad(h_img, (200,200), 127)
sq_img = cv2.imread('sq.jpg') # square image
scaled_sq_img = resizeAndPad(sq_img, (200,200), 127)
And this gives the images:
Using ImageMagick
ImageMagick
is a simple, but well-built command-line interface to do basic image processing. It's very easy to do what you want with a single command. See here for descriptions of the resizing commands.
$ convert v.jpg -resize 200x200 -background skyblue -gravity center -extent 200x200 scaled-v-im.jpg
$ convert h.jpg -resize 200x200 -background skyblue -gravity center -extent 200x200 scaled-h-im.jpg
$ convert sq.jpg -resize 200x200 -background skyblue -gravity center -extent 200x200 scaled-sq-im.jpg
Producing the images:
Related Topics
CSS: :Before: :After Pseudo-Element of Class Not Working
Placing a Div in Front of a Flash Embed
iPhone Viewport Height Too Large in Landscape Mode
Load Different CSS Rule Depending on The Browser in an Angular 4 Component
What to Use Instead of Contains() in CSS3
How to Align One Element to The Right and One Element to The Left Inside a Containing Element
Making an Unordered List Span 100% The Width of a Div
How to Gzip @Font-Face Example
How to Style a Svg Using CSS in Javafx Fxml
How to Stretch a Header Across a Web Page Background with CSS
Border-Radius on <Th>. No Border Rounding
Position Fixed on Chrome Mobile Causing Issues When Scrolling
Adding CSS Styling to React Native Webview
Ionic-Item Color and Href Not Working
How to Turn Off View Encapsulation for One Property in Angular 2