I have one transparent SVG image, I am trying to generate the thumbnail of that image. It generates a thumbnail of the image properly, but it removes the transparency of the image and adds a black background to the image. I want to generate thumbnail without losing the transparency of the image.
Note - The same problem with jpg and jpeg images also, but I added white background to the image in svg case it's not make the transparent background
I am using PHP vips library.
Here is my code for that
$im = Vips\Image::thumbnail($filename, 180, ['height' => 225, 'size' => 'both']);
$alpha = $im->hasAlpha();
if($alpha) {
$im = $im->flatten(['background' => [255, 255, 255]]);
}
Just write to a format that supports transparency, like PNG.
For example:
$im = Vips\Image::thumbnail("x.svg", 180, ['height' => 225, 'size' => 'both']);
$im->WriteToFile("x.png");
WEBP, GIF, HEIC, AVIF, TIFF etc. also support transparency. JPEG does not.
Related
Using NetVips,
using var per = Image.Perlin(800,800,33,true,3434);
using var per2 = Image.Perlin(800,800,13,true,3435);
using var per3 = Image.Perlin(800,800,13,true,3435);
using var comp = per.Composite(new []{per2, per3},new []{Enums.BlendMode.ColourBurn, Enums.BlendMode.Lighten},compositingSpace:Enums.Interpretation.Rgb);
yeilds
Exception has occurred: CLR/NetVips.VipsException
An unhandled exception of type 'NetVips.VipsException' occurred in NetVips.dll: 'unable to call composite
vips_colourspace: no known route from 'b-w' to 'rgb''
not sure how to get image from perlin, b-w to rgb basically.
Try using sRGB instead of RGB, you should get an image.
Though it'll be black and white since you are compositing black and white images. Did you want to join the three perlin images together as RGB? You need bandjoin, eg. in Python:
#!/usr/bin/python3
import pyvips
per1 = pyvips.Image.perlin(800, 800, cell_size=33, uchar=True, seed=3434)
per2 = pyvips.Image.perlin(800, 800, cell_size=13, uchar=True, seed=3435)
per3 = pyvips.Image.perlin(800, 800, cell_size=13, uchar=True, seed=3435)
# join the three one-band images up as a three-band image, tag as sRGB
image = per1.bandjoin([per2, per3]).copy(interpretation="srgb")
image.write_to_file("x.png")
To make:
I was successful in taking Chart.js output and sending it to an image file (png) and to a pdf. The issue is that with both outputs the image quality is much lower than the chart in the browser. The png file is marginally acceptable, while the pdf is very grainy. Is there another way to produce output from Chart.js that has the same visual quality that it has in the browser?
Here's the way I output to png:
var a = document.createElement('a');
a.href = myChart.toBase64Image();
a.download = 'Site 1 - Users Per Department.png';
a.click();
And here's the way I output to pdf:
$('#create_PDF_btn').on('click', function() {
var canvas = document.querySelector("#myChart");
var canvas_img = canvas.toDataURL("image/png",1.0);
var pdf = new jsPDF('landscape','in', 'letter');
pdf.addImage(canvas_img, 'png', .5, 1.75, 10, 5);
pdf.save('Site 1 - Users Per Department.pdf')
});
One additional observation of the PDF is that this code produces a much larger file than I expected - 4,700 KB vs 21 KB (output to pdf from my current solution in MS Access). I can't add the pdf, so I'm including a copy of the image here. Notice the graininess in the text and legend points:
I'm using a relatively new version of Chart.js (3.4.1) and jsPDF (1.5.3).
Thanks for any input.
Am trying to create transparent mat but am getting black background image in return so any idea how to do it as i have to overlay this image with some content on camera video feed.
code i tried.
cv::Mat comp = cv::Mat::zeros( currentImage.size(), CV_8UC4 );
comp.setTo(cv::Scalar(0,0,0,0));
imshow( "transparent", comp ); // show black background image.
thanks to #HansHire and #Scheff we find out that problem is in cv::imshow() it not render transparency so to check Mat for transparency it need to dumped to disk to be checked using system image viewer which render transparency very well
cv::Mat comp = cv::Mat::zeros( currentImage.size(), CV_8UC4 );
comp.setTo(cv::Scalar(0,0,0,0));
// imshow( "transparent", comp ); // show black background image because it cant render transparency .
imwrite( "C:/opencv_dump.png", comp ); // transparent upon opening , thumbnail may look black.
Hey im a newbie in python in my code there is a animated gif that play whenever i run the code. The gif image is actually transparent when open in adobe etc. but the problem is when i run the code.The frame which is the color gray included in the gif.I want to remove the color gray so that the only thing that can see is my only animated gif
This is my code:
# mimic an animated GIF displaying a series of GIFs
# an animated GIF was used to create the series of GIFs
# with a common GIF animator utility
import time
from Tkinter import *
root = Tk()
imagelist = ["dog001.gif","dog002.gif","dog003.gif"]
# extract width and height info
photo = PhotoImage(file=imagelist[0])
width = photo.width()
height = photo.height()
canvas = Canvas(width=width, height=height)
canvas.create_line(0,240,640,240, fill='blue')
canvas.pack()
# create a list of image objects
giflist = []
for imagefile in imagelist:
photo = PhotoImage(file=imagefile)
giflist.append(photo)
# loop through the gif image objects for a while
for k in range(0, 10):
for gif in giflist:
canvas.delete(ALL)
canvas.create_image(width/2.0, height/2.0, image=gif)
canvas.update()
time.sleep(0.1)
root.mainloop()[![enter image description here][1]][1]
dog001.gif
dog002.gif
dog003.gif
As described here, making a Tkinter window transparent is possible, but depends on your OS.
If you are on Windows, just add the following lines after creating the root:
root = Tk()
# Hide the root window drag bar and close button
root.overrideredirect(True)
# Make the root window always on top
root.wm_attributes("-topmost", True)
# Define a transparent color
root.wm_attributes("-transparentcolor", '#eeefff')
Then set your canvas' background color as the transparent color defined above:
canvas = Canvas(width=width, height=height, bg='#eeefff', highlightthickness=0)
I've been scratching my head for a few days on how to complete the task of making the edges rounded on an image taken from picamera using python-wand. I have it setup now to where it grabs the image and composites it over the banner/background image with the following:
img = Image(filename=Picture)
img.resize(1200, 800)
bimg = Image(filename=Background)
bimg.composite(img, left=300, top=200)
bimg.save(filename=BPicture)
Any help is appreciated!
You can use wand.drawing.Drawing.rectangle to generate rounded corners, and overlay it with composite channels.
from wand.image import Image
from wand.color import Color
from wand.drawing import Drawing
with Image(filename='rose:') as img:
img.resize(240, 160)
with Image(width=img.width,
height=img.height,
background=Color("white")) as mask:
with Drawing() as ctx:
ctx.fill_color = Color("black")
ctx.rectangle(left=0,
top=0,
width=mask.width,
height=mask.height,
radius=mask.width*0.1) # 10% rounding?
ctx(mask)
img.composite_channel('all_channels', mask, 'screen')
img.save(filename='/tmp/out.png')
Now if I understand your question, you can apply the same technique, but composite Picture in the drawing context.
with Image(filename='rose:') as img:
img.resize(240, 160)
with Image(img) as nimg:
nimg.negate() # For fun, let's negate the image for the background
with Drawing() as ctx:
ctx.fill_color = Color("black")
ctx.rectangle(left=0,
top=0,
width=nimg.width,
height=nimg.height,
radius=nimg.width*0.3) # 30% rounding?
ctx.composite('screen', 0, 0, nimg.width, nimg.height, img)
ctx(nimg)
nimg.save(filename='/tmp/out2.png')