Poor quality jpeg resize on GD library use. Wrong steps? - jpeg

So I have the following script to resize an incoming jpg and save it to the server in a new size. The created jpg file has terrible quality, even when I am making the quality 90 on the imagejpeg. I am wondering if I am messing it up earlier in my efforts.
// Get new sizes
list($width, $height) = getimagesize($filename);
$percentW=298/$width;
$newwidth = $width * $percentW;
$newheight = $height * $percentW;
// Creating a blank canvas to put the new file. Is this an extra step?
$thumb = imagecreatetruecolor($newwidth, $newheight);
$source = imagecreatefromjpeg($filename);
// Now I create the resized photo with the needed width and height on the blank canvas with the source file resized
imagecopyresampled($thumb, $source, 0, 0, 0, 0, $newwidth, $newheight, $width, $height);
// Output to server, define 90% quality. My guess is the quality is destroyed before now.
imagejpeg($thumb,"../../uploads/buckets/" . $_POST["bucket"] . "/" .$fileNameBucket,90);
imagedestroy($thumb);
Am I messing up the quality before I even output the file to the server? Should I use resample instead of resize? I am stuck with using the GD library so ImageMagick is not an option. Thanks.

Related

NodeJS using package Sharp to resize images. How do I resize based on the long side of the image?

In Lightroom, I can resize images to a pixel value for the long side. That way if, for example, an image is five times as tall as it is wide, and I asked for a 2,000px (long side) image, the result will be 2000x400px. Not 10,000x2,000px.
In other words, the aspect ratio is maintained, and 2,000px is a limiter.
Can sharp do this, and maintain the aspect ratio -- when I don't know in advance whether the height or the width will be greater?
Otherwise I will have to pull the exif data from the images so that I know in advance which is greater, and can resize based on the greater side (width or height). I am trying to avoid this step.
Below is my current code and it creates 300x300 images, ignoring the aspect ratio. However the desired result is:
300x200 images, when given images whose aspect ratio is 3:2
200x300 images, when given images whose aspect ratio is 2:3.
etc.
exports.resizeResolutions = {
mapThumbnail: {x: 300, y: 300},
}
exports.fitMethods = {
inside: "inside",
}
proc = sharp(path.join(pathToLocalFSGalleries, folder, file),
{ fit: fitMethods.inside }).resize(resizeResolutions.mapThumbnail.x, resizeResolutions.mapThumbnail.y);
await proc.toFile(path.join(pathToThumbnails, folder, file));

Photoshop Script Resize Image, does not update the resolution

I have an image when manually resized to width 1000pts the resolution comes to 261.5pixels/inch
Original Image Dimensions
Manual Update width to 1000pts
The resolution downgraded to 261.5 px/in
When i try the same programmatically with js script the width changes but resolution is same as of the original image
JS Code
document.resizeImage(UnitValue(parseInt(row.width),"pt"), null, null); //working
where row.width = 1000
Image dimensions after executing the js script
How to calculate the resolution of the image automatically and set to 261.5px/inch
The new resolution should be manually calculated.
newResolution = document.resolution * (originalWidth / parseInt(row.width));
document.resizeImage(null, null, newResolution, ResampleMethod.NONE);
document.resizeImage(UnitValue(parseInt(row.width), "pt"), null, null);
If the width is changed, the height will be changed programatically, which caused an impression, ResampleMethod.NONE will set the resolution too.
Not sure if you want to shrink the image or the canvas. I've got with the former.
Use app.activeDocument.resizeImage with a width, height and resolution of your image.
resizemeth = ResampleMethod.BICUBIC;
res = 72;
w = 1000;
h = 673;
app.activeDocument.resizeImage(w, h, res, resizemeth);
Also check what units you are using.
alert(app.preferences.rulerUnits);

(npm package) gm only edits the last frame of GIF

I'm trying to overlay text on top of a GIF using the gm package. It does overlay the text on top of regular images, however on GIFs it just overlays the last 2 frames. I don't have much experience using this package and Imagemagick.
Here's the code that edits the image.
gm(request(image)) //any image url
.command('convert')
.coalesce()
.font("./impact.ttf")
.dither(false)
.fontSize(Math.round(width/11)) //Width is defined earlier in the code
.stroke("#000000")
.strokeWidth(width/450)
.out('-gravity', 'north')
.out('-size',width+'x','-fill','white','-background', 'transparent', '0')
.out('caption:' + meme[0].toUpperCase().substr(meme[0].indexOf(" ")+1))
.out('-layers','optimize')
.out('-gravity', 'south')
.out('-size',width+'x','caption:' + meme[1].toUpperCase())
.out('-layers','optimize')
.strip()
// If i use these two, it does append all the layers but the text does not wrap like it does using caption
// .drawText(0,1, meme[0].toUpperCase().substr(meme[0].indexOf(" ") + 1), 'North')
// .drawText(0,0, meme[1].toUpperCase(), 'South')
.stream((error, stdout) => {...}
Result of the code
Any help would be appreciated!

OpenCV Seamless Cloning shift position after finish the process

I am trying to used the seamsless cloning to blend to image together.
but I notice that after using the seamsless clone function the area in the
mask that I want to transfer is shift upward. So I have a question that
is this a normal behaviour of the seamsless clone function or it is a bug
on my implementation.
Here are the Source photo
Here are the destination photo
Here are the result photo
I encountered similar situation. Moreover, like #JoshuaCWebDeveloper noted, this shift disappeared when all one mask is used. Nevertheless, I got a fix for this. What I did is this. I cropped valid mask (non-zero sub-section) out using cv2.boundingRect. So my source image and mask image are reduced to a smaller size, while center is now calculated from boundingRect outputs (Since reference point is marked on destination image). This way, error got solved/shift got ridden.
(Based on the answer posted by Fractalic Forieu) You can achieve the same result without reducing the image size.
Instead of using the image center:
center = (width // 2, height // 2)
poissonImage = cv2.seamlessClone(srcImage, dstImage, maskImage, center)
use the center of the bounding rect:
monoMaskImage = cv2.split(maskImage)[0] # reducing the mask to a monochrome
br = cv2.boundingRect(monoMaskImage) # bounding rect (x,y,width,height)
centerOfBR = (br[0] + br[2] // 2, br[1] + br[3] // 2)
poissonImage = cv2.seamlessClone(srcImage, dstImage, maskImage, centerOfBR )

view RGBA image

Can someone tell me how can I view an RGBA image? I just want a tool that I can display an RGBA image with!
I have written a code, which outputs only RGBA format. I just want to verify if my code worked, and just want to find a simple tool to view this image.
I wasn't able to come across a software to be able to display a RGBA image.
Thanks in advance.
RGBA files only contain raw channel data. The binary data will not have enough information to display an image (ie. width,height,depth, &tc).
If you know the image dimensions of each .rgba file, you should be able to work with the data. Here's an example of viewing raw date in javascript.
var fr = new FileReader(),
myWidth = 200,
myHeight = 200;
fr.onload = function(frEvent) {
var canvasElement = document.getElementById("myCanvas"),
ctx = canvasElement.getContext("2d"),
blob = ctx.createImageData(myWidth,myHeight),
index = 0;
while(index < frEvent.target.result.length)
blob.data[index] = frEvent.target.result.charCodeAt(index++);
ctx.putImageData(blob,0,0);
}
Imagemagick will be able to display raw RGBA data. Assuming that each color sample is 8 bits.
display -size 200x200 -depth 8 mySimpleData.rgba

Resources